Skip to content

Commit

Permalink
[diary] search with tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Noverish committed May 26, 2024
1 parent a15ff72 commit a733afd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DiarySearchBo(
userId = userId,
dates = params.dates,
query = params.query,
tagId = params.tagId,
page = page,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import kim.hyunsub.diary.repository.condition.DiaryTagPreviewCondition
import kim.hyunsub.diary.repository.entity.DiaryTagPreview
import kim.hyunsub.diary.repository.mapper.DiaryTagPreviewMapper
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

@Service
class DiaryTagSearchBo(
private val diaryTagPreviewMapper: DiaryTagPreviewMapper,
) {
fun search(userId: String, params: DiaryTagSearchParams): ApiPageResult<ApiDiaryTagPreview> {
val page = PageRequest.of(params.page ?: 0, 10)
val page = determinePage(params)
val condition = DiaryTagPreviewCondition(userId = userId, query = params.query, page = page)

val total = diaryTagPreviewMapper.count(condition)
Expand All @@ -28,6 +29,15 @@ class DiaryTagSearchBo(
)
}

private fun determinePage(params: DiaryTagSearchParams): Pageable {
val pageSize = params.pageSize ?: 10
if (pageSize < 0) {
return PageRequest.of(0, Int.MAX_VALUE)
}

return PageRequest.of(params.page ?: 0, params.pageSize ?: 10)
}

private fun DiaryTagPreview.toApi() = ApiDiaryTagPreview(
id = id,
name = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ data class DiarySearchParams(
val dates: List<LocalDate>? = null,
val page: Int? = null,
val pageSize: Int? = null,
val tagId: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kim.hyunsub.diary.model.dto

data class DiaryTagSearchParams(
val query: String? = null,
val page: Int? = null,
val query: String?,
val page: Int?,
val pageSize: Int?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class DiaryCondition(
val dates: List<LocalDate>? = null,
val dateRange: LocalDateRange? = null,
val query: String? = null,
val tagId: String? = null,
val page: Pageable? = null,
)
1 change: 1 addition & 0 deletions hyunsub-diary/src/main/resources/mapper/diary-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</sql>

<sql id="DiaryCondition">
<if test="tagId != null">INNER JOIN diary_tag_mapping dtm ON dtm.tag_id = #{tagId} AND dtm.date = d.date</if>
<where>
<if test="userId != null">AND d.user_id = #{userId}</if>
<if test="dates != null">
Expand Down

0 comments on commit a733afd

Please sign in to comment.