Skip to content

Commit

Permalink
Implement searching journal entries (#18)
Browse files Browse the repository at this point in the history
Resolved: #6
  • Loading branch information
zionhann authored Jul 30, 2024
1 parent 3616508 commit ad02d23
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ class JournalController(
@GetMapping("/v1/goals/{goalId}/journals")
fun displayJournals(
@PathVariable goalId: String,
@RequestParam(required = false) sort: JournalSortType = JournalSortType.NEWEST
@RequestParam(required = false) sort: JournalSortType = JournalSortType.NEWEST,
@RequestParam(required = false) q: String?
): ResponseEntity<JournalResponseWrapper> {
// Handle query as null if it is an empty string
val searchKeyword = q?.takeIf(String::isNotBlank)

val goal = goalService.goalInfo(goalId)
val journals = journalService.journalsOf(goal, sort)
val journals = journalService.journalsOf(goal, sort, searchKeyword)

val responseBody = JournalResponseWrapper.of(goal, journals)
return ResponseEntity.ok(responseBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ package com.likelionhgu.stepper.journal;
import com.likelionhgu.stepper.goal.Goal
import org.springframework.data.domain.Sort
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface JournalRepository : JpaRepository<Journal, Long> {
fun findAllByGoal(goal: Goal, sort: Sort): List<Journal>

@Query(
"select j from Journal j where j.goal = :goal " +
"and ((:searchKeyword is null or j.title like %:searchKeyword%) " +
"or (:searchKeyword is null or j.content like %:searchKeyword%))"
)
fun findAllByGoalWithQuery(goal: Goal, sort: Sort, searchKeyword: String?): List<Journal>
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JournalService(
* @param sortType The sorting type of the journals.
* @return A list of journals for the goal ordered by the sorting type.
*/
fun journalsOf(goal: Goal, sortType: JournalSortType): List<Journal> {
return journalRepository.findAllByGoal(goal, sortType.toSort())
fun journalsOf(goal: Goal, sortType: JournalSortType, searchKeyword: String?): List<Journal> {
return journalRepository.findAllByGoalWithQuery(goal, sortType.toSort(), searchKeyword)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class JournalControllerTest(

given("a member who has created a goal and written some journal entries") {
every { goalService.goalInfo(any()) } returns Goal(title = "title", member = mockk())
every { journalService.journalsOf(any(), any()) } returns listOf(
every { journalService.journalsOf(any(), any(), any()) } returns listOf(
Journal(
"title1",
"content",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.likelionhgu.stepper.member.Member
import com.likelionhgu.stepper.member.MemberRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.extensions.spring.SpringExtension
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest

Expand All @@ -26,7 +25,7 @@ class JournalRepositoryTest(

`when`("the member requests to see the journal entries with default order") {
then("the journal entries should be returned ordered by created date in descending order") {
val actual = journalRepository.findAllByGoal(goal, JournalSortType.NEWEST.toSort())
val actual = journalRepository.findAllByGoalWithQuery(goal, JournalSortType.NEWEST.toSort(), null)

actual[0].journalId shouldBe j2.journalId
actual[1].journalId shouldBe j1.journalId
Expand All @@ -35,12 +34,37 @@ class JournalRepositoryTest(

`when`("the member requests to see the journal entries with oldest order") {
then("the journal entries should be returned ordered by created date in ascending order") {
val actual = journalRepository.findAllByGoal(goal, JournalSortType.OLDEST.toSort())
val actual = journalRepository.findAllByGoalWithQuery(goal, JournalSortType.OLDEST.toSort(), null)

actual[0].journalId shouldBe j1.journalId
actual[1].journalId shouldBe j2.journalId
}
}

`when`("the member searches for a journal entry with a keyword 'title1'") {
then("the journal entries should be returned that contain the keyword") {
val actual = journalRepository.findAllByGoalWithQuery(goal, JournalSortType.NEWEST.toSort(), "title1")

actual.size shouldBe 1
actual[0].journalId shouldBe j1.journalId
}
}

`when`("the member searches for a journal entry with a keyword 'content'") {
then("the journal entries should be returned that contain the keyword") {
val actual = journalRepository.findAllByGoalWithQuery(goal, JournalSortType.NEWEST.toSort(), "content")

actual.size shouldBe 2
}
}

`when`("the member searches for a journal entry with a keyword that does not exist") {
then("no journal entries should be returned") {
val actual = journalRepository.findAllByGoalWithQuery(goal, JournalSortType.NEWEST.toSort(), "keyword")

actual.size shouldBe 0
}
}
}
}) {
override fun extensions() = listOf(SpringExtension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.kotest.extensions.spring.SpringExtension
import io.kotest.matchers.shouldNotBe
import io.mockk.every
import io.mockk.mockk
import org.springframework.data.domain.Sort

class JournalServiceTest : BehaviorSpec({
val journalRepository = mockk<JournalRepository>()
Expand All @@ -27,7 +26,7 @@ class JournalServiceTest : BehaviorSpec({
}

given("a member who has written some journal entries") {
every { journalRepository.findAllByGoal(any(), any()) } returns listOf(
every { journalRepository.findAllByGoalWithQuery(any(), any(), any()) } returns listOf(
Journal(
"title1",
"content",
Expand All @@ -44,7 +43,7 @@ class JournalServiceTest : BehaviorSpec({

`when`("the member requests to see the journal entries") {
then("the journal entries should be returned") {
val journals = journalService.journalsOf(mockk(), JournalSortType.NEWEST)
val journals = journalService.journalsOf(mockk(), JournalSortType.NEWEST, null)

journals.size shouldNotBe 0
}
Expand Down

0 comments on commit ad02d23

Please sign in to comment.