Skip to content

Commit

Permalink
fix: 스크롤 조회에 전체 sizecount 응답 추가 (#66)
Browse files Browse the repository at this point in the history
* feat: 셀럽 페이지 내 전체 음식점 수량 조회 유즈케이스 구현

* feat: 관심 음식점 개수 조회 유즈케이스 구현

* feat: 음식점의 리뷰 개수 조회 유즈케이스 구현

* feat: 주간 업데이트 음식점 개수 조회 유즈케이스 구현

* feat: 조건에 맞는 음식점 개수 조회 유즈케이스 구현

* style: ktlint
  • Loading branch information
TaeyeonRoyce authored Sep 24, 2024
1 parent afd199d commit f57a563
Show file tree
Hide file tree
Showing 25 changed files with 307 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ interface CelebrityRestaurantJpaRepository : JpaRepository<CelebrityRestaurantJp
pageable: Pageable,
): Slice<RestaurantJpaEntity>

@Query(
"""
SELECT COUNT(cr)
FROM CelebrityRestaurantJpaEntity cr
WHERE cr.celebrity.id = :celebrityId
""",
)
fun countRestaurantsByCelebrityId(celebrityId: Long): Long

@Query(
"""
SELECT cr.restaurant
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.celuveat.common.adapter.out.persistence

import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class JdslConfig {
@Bean
fun jpqlRenderer(): JpqlRenderer {
return JpqlRenderer()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ interface RestaurantApi {
@PageableDefault(size = 10, page = 0) pageable: Pageable,
): SliceResponse<RestaurantPreviewResponse>

@SecurityRequirement(name = "JWT")
@Operation(summary = "관심 음식점 개수 조회")
@GetMapping("/interested/count")
fun getAmountOfInterestedRestaurants(
@Auth auth: AuthContext,
): Int

@SecurityRequirement(name = "JWT")
@Operation(summary = "관심 음식점 추가")
@PostMapping("/interested/{restaurantId}")
Expand Down Expand Up @@ -71,6 +78,18 @@ interface RestaurantApi {
@PageableDefault(size = 10, page = 0) pageable: Pageable,
): SliceResponse<RestaurantPreviewResponse>

@Operation(summary = "셀럽이 다녀간 음식점 개수 조회")
@GetMapping("/celebrity/{celebrityId}/count")
fun readAmountOfRestaurantsByCelebrity(
@Parameter(
`in` = ParameterIn.PATH,
description = "셀럽 ID",
example = "1",
required = true,
)
@PathVariable celebrityId: Long,
): Int

@Operation(summary = "셀럽 추천 음식점 조회")
@GetMapping("/celebrity/recommend")
fun readCelebrityRecommendRestaurants(
Expand All @@ -85,13 +104,25 @@ interface RestaurantApi {
@PageableDefault(size = 10, page = 0) pageable: Pageable,
): SliceResponse<RestaurantPreviewResponse>

@Operation(summary = "음식점 개수 조회")
@GetMapping("/count")
fun readAmountOfRestaurants(
@ModelAttribute request: ReadRestaurantsRequest,
): Int

@Operation(summary = "이번주 업데이트된 음식점 조회")
@GetMapping("/weekly")
fun readWeeklyUpdatedRestaurants(
@Auth auth: AuthContext,
@PageableDefault(size = 10, page = 0) pageable: Pageable,
): SliceResponse<RestaurantPreviewResponse>

@Operation(summary = "이번주 업데이트된 음식점 개수 조회")
@GetMapping("/weekly/count")
fun readAmountOfWeeklyUpdatedRestaurants(
@Auth auth: AuthContext,
): Int

@Operation(summary = "주변 음식점 조회")
@GetMapping("/nearby/{restaurantId}")
fun readNearByRestaurants(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package com.celuveat.restaurant.adapter.`in`.rest
import com.celuveat.auth.adapter.`in`.rest.Auth
import com.celuveat.auth.adapter.`in`.rest.AuthContext
import com.celuveat.common.adapter.`in`.rest.response.SliceResponse
import com.celuveat.common.utils.geometry.SquarePolygon
import com.celuveat.restaurant.adapter.`in`.rest.request.ReadRestaurantsRequest
import com.celuveat.restaurant.adapter.`in`.rest.response.RestaurantDetailResponse
import com.celuveat.restaurant.adapter.`in`.rest.response.RestaurantPreviewResponse
import com.celuveat.restaurant.application.port.`in`.AddInterestedRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.DeleteInterestedRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.ReadAmountOfInterestedRestaurantUseCase
import com.celuveat.restaurant.application.port.`in`.ReadAmountOfRestaurantByCelebrityUseCase
import com.celuveat.restaurant.application.port.`in`.ReadAmountOfRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.ReadAmountOfWeeklyUpdateRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.ReadCelebrityRecommendRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.ReadCelebrityVisitedRestaurantUseCase
import com.celuveat.restaurant.application.port.`in`.ReadInterestedRestaurantsUseCase
Expand All @@ -18,6 +23,7 @@ import com.celuveat.restaurant.application.port.`in`.ReadRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.ReadWeeklyUpdateRestaurantsUseCase
import com.celuveat.restaurant.application.port.`in`.command.AddInterestedRestaurantCommand
import com.celuveat.restaurant.application.port.`in`.command.DeleteInterestedRestaurantCommand
import com.celuveat.restaurant.application.port.`in`.query.CountRestaurantsQuery
import com.celuveat.restaurant.application.port.`in`.query.ReadCelebrityRecommendRestaurantsQuery
import com.celuveat.restaurant.application.port.`in`.query.ReadCelebrityVisitedRestaurantQuery
import com.celuveat.restaurant.application.port.`in`.query.ReadInterestedRestaurantsQuery
Expand All @@ -39,15 +45,19 @@ import org.springframework.web.bind.annotation.RestController
@RestController
class RestaurantController(
private val readInterestedRestaurantsUseCase: ReadInterestedRestaurantsUseCase,
private val readAmountOfInterestedRestaurantUseCase: ReadAmountOfInterestedRestaurantUseCase,
private val addInterestedRestaurantsUseCase: AddInterestedRestaurantsUseCase,
private val deleteInterestedRestaurantsUseCase: DeleteInterestedRestaurantsUseCase,
private val readCelebrityVisitedRestaurantUseCase: ReadCelebrityVisitedRestaurantUseCase,
private val readCelebrityRecommendRestaurantsUseCase: ReadCelebrityRecommendRestaurantsUseCase,
private val readRestaurantsUseCase: ReadRestaurantsUseCase,
private val readAmountOfRestaurantsUseCase: ReadAmountOfRestaurantsUseCase,
private val readWeeklyUpdateRestaurantsUseCase: ReadWeeklyUpdateRestaurantsUseCase,
private val readAmountOfWeeklyUpdateRestaurantsUseCase: ReadAmountOfWeeklyUpdateRestaurantsUseCase,
private val readNearbyRestaurantsUseCase: ReadNearbyRestaurantsUseCase,
private val readRestaurantDetailUseCase: ReadRestaurantDetailUseCase,
private val readPopularRestaurantsUseCase: ReadPopularRestaurantsUseCase,
private val readAmountOfRestaurantByCelebrityUseCase: ReadAmountOfRestaurantByCelebrityUseCase,
) : RestaurantApi {
@GetMapping("/interested")
override fun getInterestedRestaurants(
Expand All @@ -67,6 +77,14 @@ class RestaurantController(
)
}

@GetMapping("/interested/count")
override fun getAmountOfInterestedRestaurants(
@Auth auth: AuthContext,
): Int {
val memberId = auth.memberId()
return readAmountOfInterestedRestaurantUseCase.readAmountOfInterestedRestaurant(memberId)
}

@PostMapping("/interested/{restaurantId}")
override fun addInterestedRestaurant(
@Auth auth: AuthContext,
Expand Down Expand Up @@ -113,6 +131,13 @@ class RestaurantController(
)
}

@GetMapping("/celebrity/{celebrityId}/count")
override fun readAmountOfRestaurantsByCelebrity(
@PathVariable celebrityId: Long,
): Int {
return readAmountOfRestaurantByCelebrityUseCase.readAmountOfRestaurantByCelebrity(celebrityId)
}

@GetMapping("/celebrity/recommend")
override fun readCelebrityRecommendRestaurants(
@Auth auth: AuthContext,
Expand Down Expand Up @@ -141,6 +166,23 @@ class RestaurantController(
)
}

@GetMapping("/count")
override fun readAmountOfRestaurants(
@ModelAttribute request: ReadRestaurantsRequest,
): Int {
val query = CountRestaurantsQuery(
category = request.category,
region = request.region,
searchArea = SquarePolygon.ofNullable(
lowLongitude = request.lowLongitude,
highLongitude = request.highLongitude,
lowLatitude = request.lowLatitude,
highLatitude = request.highLatitude,
),
)
return readAmountOfRestaurantsUseCase.readAmountOfRestaurants(query)
}

@GetMapping("/weekly")
override fun readWeeklyUpdatedRestaurants(
@Auth auth: AuthContext,
Expand All @@ -159,6 +201,10 @@ class RestaurantController(
)
}

override fun readAmountOfWeeklyUpdatedRestaurants(auth: AuthContext): Int {
return readAmountOfWeeklyUpdateRestaurantsUseCase.readAmountOfWeeklyUpdateRestaurants()
}

@GetMapping("/nearby/{restaurantId}")
override fun readNearByRestaurants(
@Auth auth: AuthContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class RestaurantPersistenceAdapter(
)
}

override fun countRestaurantByCelebrity(celebrityId: Long): Int {
return celebrityRestaurantJpaRepository.countRestaurantsByCelebrityId(celebrityId).toInt()
}

override fun readById(id: Long): Restaurant {
val restaurant = restaurantJpaRepository.getById(id)
val images = restaurantImageJpaRepository.findByRestaurant(restaurant)
Expand Down Expand Up @@ -88,6 +92,14 @@ class RestaurantPersistenceAdapter(
)
}

override fun countRestaurantsByCondition(
category: String?,
region: String?,
searchArea: SquarePolygon?,
): Int {
return restaurantJpaRepository.countAllByFilter(RestaurantFilter(category, region, searchArea)).toInt()
}

override fun readByCreatedAtBetween(
startOfWeek: LocalDate,
endOfWeek: LocalDate,
Expand All @@ -114,6 +126,16 @@ class RestaurantPersistenceAdapter(
)
}

override fun countByCreatedAtBetween(
startOfWeek: LocalDate,
endOfWeek: LocalDate,
): Int {
return restaurantJpaRepository.countByCreatedAtBetween(
startOfWeek.atStartOfDay(),
endOfWeek.atTime(LocalTime.MAX),
).toInt()
}

override fun readNearby(id: Long): List<Restaurant> {
val centralRestaurant = restaurantJpaRepository.getById(id)
val restaurants = restaurantJpaRepository.findTop5NearestInDistance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface CustomRestaurantRepository {
filter: RestaurantFilter,
pageable: Pageable,
): Slice<RestaurantJpaEntity>

fun countAllByFilter(filter: RestaurantFilter): Long
}

data class RestaurantFilter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.celuveat.restaurant.adapter.out.persistence.entity

import com.linecorp.kotlinjdsl.dsl.jpql.jpql
import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderContext
import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderer
import com.linecorp.kotlinjdsl.support.spring.data.jpa.repository.KotlinJdslJpqlExecutor
import jakarta.persistence.EntityManager
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.data.domain.SliceImpl
Expand All @@ -9,6 +13,9 @@ import org.springframework.stereotype.Repository
@Repository
class CustomRestaurantRepositoryImpl(
private val executor: KotlinJdslJpqlExecutor,
private val entityManager: EntityManager,
private val context: JpqlRenderContext,
private val renderer: JpqlRenderer,
) : CustomRestaurantRepository {
override fun findAllByFilter(
filter: RestaurantFilter,
Expand All @@ -25,7 +32,7 @@ class CustomRestaurantRepositoryImpl(
filter.searchArea?.let {
path(RestaurantJpaEntity::longitude).between(
it.lowLongitude,
it.highLongitude
it.highLongitude,
)
},
filter.searchArea?.let { path(RestaurantJpaEntity::latitude).between(it.lowLatitude, it.highLatitude) },
Expand All @@ -38,4 +45,29 @@ class CustomRestaurantRepositoryImpl(
findSlice.hasNext(),
)
}

override fun countAllByFilter(filter: RestaurantFilter): Long {
val query = jpql {
select(
count(entity(RestaurantJpaEntity::class)),
).from(
entity(RestaurantJpaEntity::class),
).whereAnd(
filter.category?.let { path(RestaurantJpaEntity::category).eq(it) },
filter.region?.let { path(RestaurantJpaEntity::roadAddress).like("%$it%") },
filter.searchArea?.let {
path(RestaurantJpaEntity::longitude).between(
it.lowLongitude,
it.highLongitude,
)
},
filter.searchArea?.let { path(RestaurantJpaEntity::latitude).between(it.lowLatitude, it.highLatitude) },
)
}

val rendered = renderer.render(query, context)
return entityManager.createQuery(rendered.query, Long::class.java)
.apply { rendered.params.forEach { (name, value) -> setParameter(name, value) } }
.singleResult
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ interface RestaurantJpaRepository : JpaRepository<RestaurantJpaEntity, Long>, Cu
pageable: Pageable,
): Slice<RestaurantJpaEntity>

fun countByCreatedAtBetween(
startOfWeek: LocalDateTime,
endOfWeek: LocalDateTime,
): Long

@Query(
"""
SELECT r.*
Expand Down
Loading

0 comments on commit f57a563

Please sign in to comment.