Skip to content

Commit

Permalink
fix: 그룹 전체 조회 query 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
leeheefull committed Dec 3, 2024
1 parent 97f0c7f commit 9f2cabd
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun NumberPath<Long>.isEquals(parameter: Long?): BooleanExpression? {
}

fun StringPath.isContains(parameter: String?): BooleanExpression? {
return parameter?.takeIf { param -> param.isNotEmpty() }?.let { param -> this.contains(param) }
return parameter?.let { param -> this.contains(param) }
}

fun NumberPath<Long>.isIn(parameters: Set<Long>?): BooleanExpression? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ class GroupFacade(
}
}

suspend fun searchGroup(user: AuthUser, tagName: String?, pageRequest: HeroPageRequest): Page<SearchGroupResponse> {
val groups = groupService.findByTagNameAndPage(tagName, pageRequest.toDefault())
suspend fun searchGroup(user: AuthUser, keyword: String?, pageRequest: HeroPageRequest): Page<SearchGroupResponse> {
val groups = groupService.findByKeywordAndPage(keyword, pageRequest.toDefault())

val groupUserByUid = groups.content.map { group -> group.id }
.run { groupUserService.findByUidAndGroupIdIn(user.uid, this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class GroupService(
return groupRepository.save(group)
}

suspend fun findByTagNameAndPage(tagName: String?, pageable: Pageable): Page<Group> {
suspend fun findByKeywordAndPage(keyword: String?, pageable: Pageable): Page<Group> {
return withContext(Dispatchers.IO) {
groupRepository.findByTagNameAndPage(tagName, pageable)
groupRepository.findByKeywordAndPage(keyword, pageable)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable

interface GroupQRepository {
fun findByTagNameAndPage(tagName: String?, pageable: Pageable): Page<Group>
fun findByKeywordAndPage(keyword: String?, pageable: Pageable): Page<Group>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import org.springframework.data.domain.Pageable
class GroupQRepositoryImpl(
private val queryFactory: JPAQueryFactory,
) : GroupQRepository {
override fun findByTagNameAndPage(tagName: String?, pageable: Pageable): Page<Group> {
override fun findByKeywordAndPage(keyword: String?, pageable: Pageable): Page<Group> {
val groups = queryFactory
.selectDistinct(group)
.from(group)
.leftJoin(groupTagMap).on(group.id.eq(groupTagMap.groupId))
.leftJoin(groupTag).on(groupTagMap.tagId.eq(groupTag.id))
.where(groupTag.name.isContains(tagName))
.where(group.name.isContains(keyword)?.or(groupTag.name.isContains(keyword)))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(getGroupOrderSpecifier(pageable))
Expand All @@ -32,19 +32,19 @@ class GroupQRepositoryImpl(
.from(group)
.leftJoin(groupTagMap).on(group.id.eq(groupTagMap.groupId))
.leftJoin(groupTag).on(groupTagMap.tagId.eq(groupTag.id))
.where(groupTag.name.isContains(tagName))
.where(group.name.isContains(keyword)?.or(groupTag.name.isContains(keyword)))
.orderBy(getGroupOrderSpecifier(pageable))
.fetchOne() ?: 0L

return PageImpl(groups, pageable, count)
}

private fun getGroupOrderSpecifier(pageable: Pageable): OrderSpecifier<out Comparable<*>?>? {
val order = pageable.sort.firstOrNull() ?: return null
val order = pageable.sort.firstOrNull() ?: return group.createdAt.desc()
val orderSpecifier: ComparableExpressionBase<out Comparable<*>> = when (order.property) {
"createdAt" -> group.createdAt
"name" -> group.name
"ownerUid" -> group.ownerUid
"userCount" -> group.userCount
else -> group.createdAt
}
return if (order.isDescending) orderSpecifier.desc() else orderSpecifier.asc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class GroupResource(
@GetMapping("/api/v1/groups")
suspend fun searchGroups(
user: AuthUser,
@RequestParam(required = false) tagName: String?,
@RequestParam(required = false) keyword: String?,
@ParameterObject pageRequest: HeroPageRequest,
): PageResponse<SearchGroupResponse> {
return groupFacade.searchGroup(user, tagName, pageRequest).wrapPage()
return groupFacade.searchGroup(user, keyword, pageRequest).wrapPage()
}

@Operation(summary = "그룹 생성")
Expand Down

0 comments on commit 9f2cabd

Please sign in to comment.