Skip to content

Commit

Permalink
Feat: Add sort option for notice search.
Browse files Browse the repository at this point in the history
  • Loading branch information
huGgW committed Apr 6, 2024
1 parent 1e50a07 commit 301224b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wafflestudio.csereal.core.notice.api

import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.common.enums.ContentSearchSortType
import com.wafflestudio.csereal.common.utils.getUsername
import com.wafflestudio.csereal.core.notice.dto.*
import com.wafflestudio.csereal.core.notice.service.NoticeService
Expand Down Expand Up @@ -29,6 +30,7 @@ class NoticeController(
@RequestParam(required = false) keyword: String?,
@RequestParam(required = false) pageNum: Int?,
@RequestParam(required = false, defaultValue = "20") pageSize: Int,
@RequestParam(required = false, defaultValue = "DATE") sortBy: String,
authentication: Authentication?
): ResponseEntity<NoticeSearchResponse> {
val username = getUsername(authentication)
Expand All @@ -40,7 +42,10 @@ class NoticeController(
val usePageBtn = pageNum != null
val page = pageNum ?: 1
val pageRequest = PageRequest.of(page - 1, pageSize)
return ResponseEntity.ok(noticeService.searchNotice(tag, keyword, pageRequest, usePageBtn, isStaff))

val sortType = ContentSearchSortType.fromJsonValue(sortBy)

return ResponseEntity.ok(noticeService.searchNotice(tag, keyword, pageRequest, usePageBtn, sortType, isStaff))
}

@GetMapping("/totalSearch")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.wafflestudio.csereal.core.notice.database
import com.querydsl.core.BooleanBuilder
import com.querydsl.core.types.Projections
import com.querydsl.jpa.impl.JPAQueryFactory
import com.wafflestudio.csereal.common.enums.ContentSearchSortType
import com.wafflestudio.csereal.common.repository.CommonRepository
import com.wafflestudio.csereal.common.utils.FixedPageRequest
import com.wafflestudio.csereal.core.notice.database.QNoticeEntity.noticeEntity
Expand Down Expand Up @@ -35,6 +36,7 @@ interface CustomNoticeRepository {
keyword: String?,
pageable: Pageable,
usePageBtn: Boolean,
sortBy: ContentSearchSortType,
isStaff: Boolean
): NoticeSearchResponse

Expand Down Expand Up @@ -95,21 +97,13 @@ class NoticeRepositoryImpl(
keyword: String?,
pageable: Pageable,
usePageBtn: Boolean,
sortBy: ContentSearchSortType,
isStaff: Boolean
): NoticeSearchResponse {
val keywordBooleanBuilder = BooleanBuilder()
val tagsBooleanBuilder = BooleanBuilder()
val isPrivateBooleanBuilder = BooleanBuilder()

if (!keyword.isNullOrEmpty()) {
val booleanTemplate = commonRepository.searchFullDoubleTextTemplate(
keyword,
noticeEntity.title,
noticeEntity.plainTextDescription
)
keywordBooleanBuilder.and(booleanTemplate.gt(0.0))
}

if (!tag.isNullOrEmpty()) {
tag.forEach {
val tagEnum = TagInNoticeEnum.getTagEnum(it)
Expand All @@ -125,6 +119,20 @@ class NoticeRepositoryImpl(
)
}

val scoreOrNull = if (!keyword.isNullOrEmpty()) {
commonRepository.searchFullDoubleTextTemplate(
keyword,
noticeEntity.title,
noticeEntity.plainTextDescription
)
} else {
null
}

if (scoreOrNull != null) {
keywordBooleanBuilder.and(scoreOrNull.gt(0.0))
}

val jpaQuery = queryFactory.select(
Projections.constructor(
NoticeSearchDto::class.java,
Expand All @@ -133,7 +141,8 @@ class NoticeRepositoryImpl(
noticeEntity.createdAt,
noticeEntity.isPinned,
noticeEntity.attachments.isNotEmpty,
noticeEntity.isPrivate
noticeEntity.isPrivate,
scoreOrNull
)
).from(noticeEntity)
.leftJoin(noticeTagEntity).on(noticeTagEntity.notice.eq(noticeEntity))
Expand All @@ -151,13 +160,24 @@ class NoticeRepositoryImpl(
total = (10 * pageable.pageSize).toLong() + 1 // 10개 페이지 고정
}

val noticeSearchDtoList = jpaQuery
.orderBy(noticeEntity.isPinned.desc())
.orderBy(noticeEntity.createdAt.desc())
val noticeSearchQuery = jpaQuery
.offset(pageRequest.offset)
.limit(pageRequest.pageSize.toLong())
.distinct()
.fetch()

val noticeSearchDtoList = noticeSearchQuery
.orderBy(noticeEntity.isPinned.desc())
.let {
when {
sortBy == ContentSearchSortType.DATE || scoreOrNull == null -> {
it.orderBy(noticeEntity.createdAt.desc())
}

else -> {
it.orderBy(scoreOrNull.desc())
}
}
}.fetch()

return NoticeSearchResponse(total, noticeSearchDtoList)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import com.querydsl.core.annotations.QueryProjection
import com.wafflestudio.csereal.core.notice.database.NoticeEntity
import java.time.LocalDateTime

data class NoticeSearchDto @QueryProjection constructor(
data class NoticeSearchDto(
val id: Long,
val title: String,
val createdAt: LocalDateTime?,
val isPinned: Boolean,
val hasAttachment: Boolean,
val isPrivate: Boolean
) {
@QueryProjection constructor(
id: Long,
title: String,
createdAt: LocalDateTime?,
isPinned: Boolean,
hasAttachment: Boolean,
isPrivate: Boolean,
score: Double?
) : this(id, title, createdAt, isPinned, hasAttachment, isPrivate)

constructor(entity: NoticeEntity, hasAttachment: Boolean) : this(
entity.id,
entity.title,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wafflestudio.csereal.core.notice.service

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.common.enums.ContentSearchSortType
import com.wafflestudio.csereal.common.utils.cleanTextFromHtml
import com.wafflestudio.csereal.core.notice.database.*
import com.wafflestudio.csereal.core.notice.dto.*
Expand All @@ -21,6 +22,7 @@ interface NoticeService {
keyword: String?,
pageable: Pageable,
usePageBtn: Boolean,
sortBy: ContentSearchSortType,
isStaff: Boolean
): NoticeSearchResponse

Expand Down Expand Up @@ -55,9 +57,10 @@ class NoticeServiceImpl(
keyword: String?,
pageable: Pageable,
usePageBtn: Boolean,
sortBy: ContentSearchSortType,
isStaff: Boolean
): NoticeSearchResponse {
return noticeRepository.searchNotice(tag, keyword, pageable, usePageBtn, isStaff)
return noticeRepository.searchNotice(tag, keyword, pageable, usePageBtn, sortBy, isStaff)
}

@Transactional(readOnly = true)
Expand Down

0 comments on commit 301224b

Please sign in to comment.