Skip to content

Commit

Permalink
[KAN-88] 음식점 조회 API에 정렬로직 추가 - 실제 작업 전
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok committed May 25, 2024
1 parent 259f9fb commit e6284c8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantRes
import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantsRequest
import com.restaurant.be.restaurant.presentation.controller.dto.GetRestaurantsResponse
import com.restaurant.be.restaurant.repository.RestaurantEsRepository
import com.restaurant.be.restaurant.repository.RestaurantLikeRepository
import com.restaurant.be.restaurant.repository.RestaurantRepository
import com.restaurant.be.user.repository.UserRepository
import org.springframework.data.domain.PageImpl
Expand All @@ -19,7 +20,8 @@ class GetRestaurantService(
private val restaurantEsRepository: RestaurantEsRepository,
private val redisRepository: RedisRepository,
private val userRepository: UserRepository,
private val restaurantRepository: RestaurantRepository
private val restaurantRepository: RestaurantRepository,
private val restaurantLikeRepository: RestaurantLikeRepository
) {

@Transactional(readOnly = true)
Expand All @@ -28,26 +30,36 @@ class GetRestaurantService(
pageable: Pageable,
email: String
): GetRestaurantsResponse {
val user = userRepository.findByEmail(email) ?: throw NotFoundUserEmailException()
val userId = userRepository.findByEmail(email)?.id ?: throw NotFoundUserEmailException()
val restaurantIds =
if (request.like != null) {
restaurantLikeRepository.findAllByUserId(userId)
.map { it.restaurantId }
} else {
null
}

val restaurants = restaurantEsRepository.searchRestaurants(request, pageable)
val restaurants = restaurantEsRepository.searchRestaurants(
request,
pageable,
restaurantIds,
request.like
)

if (!request.query.isNullOrEmpty()) {
redisRepository.addSearchQuery(user.id ?: 0, request.query)
redisRepository.addSearchQuery(userId, request.query)
}

val restaurantProjections = restaurantRepository.findDtoByIds(
restaurants.map { it.id },
user.id ?: 0,
request.like,
pageable
userId
)

return GetRestaurantsResponse(
PageImpl(
restaurantProjections.content.map { it.toDto() },
restaurantProjections.map { it.toDto() },
pageable,
restaurantProjections.content.size.toLong()
restaurantProjections.size.toLong()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,31 @@ class RestaurantEsRepository(

private val searchIndex = "restaurant"

fun searchRestaurants(request: GetRestaurantsRequest, pageable: Pageable): List<RestaurantEsDocument> {
fun searchRestaurants(
request: GetRestaurantsRequest,
pageable: Pageable,
restaurantIds: List<Long>?,
like: Boolean?
): List<RestaurantEsDocument> {
val dsl = SearchDSL()
val termQueries: MutableList<ESQuery> = mutableListOf()

if (restaurantIds != null) {
if (like == true) {
termQueries.add(
dsl.terms("id", *restaurantIds.map { it.toString() }.toTypedArray())
)
} else {
termQueries.add(
dsl.bool {
mustNot(
dsl.terms("id", *restaurantIds.map { it.toString() }.toTypedArray())
)
}
)
}
}

if (!request.categories.isNullOrEmpty()) {
termQueries.add(
dsl.terms("category", *request.categories.toTypedArray())
Expand Down Expand Up @@ -139,8 +161,8 @@ class RestaurantEsRepository(
}
}
},
size = 500,
from = 0
size = pageable.pageSize,
from = pageable.offset.toInt()
).parseHits<RestaurantEsDocument>()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository

interface RestaurantLikeRepository : JpaRepository<RestaurantLike, Long> {
fun deleteByUserIdAndRestaurantId(userId: Long, restaurantId: Long)
fun findAllByUserId(userId: Long): List<RestaurantLike>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ interface RestaurantRepositoryCustom {

fun findDtoByIds(
restaurantIds: List<Long>,
userId: Long,
isLikeFilter: Boolean?,
pageable: Pageable
): Page<RestaurantProjectionDto>
userId: Long
): List<RestaurantProjectionDto>

fun findMyLikeRestaurants(userId: Long, pageable: Pageable): Page<RestaurantProjectionDto>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.restaurant.be.restaurant.repository

import com.querydsl.jpa.JPAExpressions
import com.querydsl.jpa.impl.JPAQueryFactory
import com.restaurant.be.restaurant.domain.entity.QCategory.category
import com.restaurant.be.restaurant.domain.entity.QMenu.menu
Expand Down Expand Up @@ -67,45 +66,16 @@ class RestaurantRepositoryCustomImpl(

override fun findDtoByIds(
restaurantIds: List<Long>,
userId: Long,
isLikeFilter: Boolean?,
pageable: Pageable
): Page<RestaurantProjectionDto> {
userId: Long
): List<RestaurantProjectionDto> {
if (restaurantIds.isEmpty()) {
return PageImpl(emptyList())
return emptyList()
}

val restaurantQuery = queryFactory
val restaurantInfos = queryFactory
.select(restaurant)
.from(restaurant)
.where(
restaurant.id.`in`(restaurantIds)
.and(
if (isLikeFilter == true) {
restaurant.id.`in`(
JPAExpressions
.select(restaurantLike.restaurantId)
.from(restaurantLike)
.where(restaurantLike.userId.eq(userId))
)
} else if (isLikeFilter == false) {
restaurant.id.`in`(
JPAExpressions
.select(restaurantLike.restaurantId)
.from(restaurantLike)
.where(restaurantLike.userId.eq(userId))
).not()
} else {
null
}
)
)

val total = restaurantQuery.fetchCount()

val restaurantInfos = restaurantQuery
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.where(restaurant.id.`in`(restaurantIds))
.fetch()

val likedUsers = queryFactory
Expand Down Expand Up @@ -151,7 +121,7 @@ class RestaurantRepositoryCustomImpl(
)
}

return PageImpl(restaurantDtos, pageable, total)
return restaurantDtos
}

override fun findMyLikeRestaurants(
Expand Down

0 comments on commit e6284c8

Please sign in to comment.