Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.fastcampus.common.dto.APIResponseDTO
import org.fastcampus.common.dto.CursorDTO
import org.fastcampus.common.dto.CursorDTOString
import org.fastcampus.store.entity.Store
import org.fastcampus.store.enums.OrderType
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
Expand Down Expand Up @@ -127,8 +128,8 @@ class StoreController(
@RequestParam(defaultValue = "5") size: Int,
@RequestParam(required = false) category: Store.Category?,
@RequestParam(required = false) keyword: String?,
@RequestParam(required = false) cursor: String?, // "distance_storeId"
// @RequestParam(required = false) order: OrderType?, // "distance_storeId"
@RequestParam(required = false) cursor: String?,
@RequestParam(required = false) orderType: OrderType?,
): ResponseEntity<APIResponseDTO<CursorDTOString<StoreInfo>>> {
logger.info("category: $category, searchCondition: $keyword")
val result = storeService.getStoresByNearByAndConditionCursor(
Expand All @@ -138,6 +139,7 @@ class StoreController(
category = category,
keyword = keyword,
cursor = cursor,
orderType = orderType,
)
return ResponseEntity.ok(
APIResponseDTO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum class OrderType {
DISTANCE,
RATING,
REVIEW,
ORDER_COUNT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.fastcampus.common.dto.CursorDTO
import org.fastcampus.common.dto.CursorDTOString
import org.fastcampus.review.repository.ReviewRepository
import org.fastcampus.store.entity.Store
import org.fastcampus.store.enums.OrderType
import org.fastcampus.store.exception.StoreException
import org.fastcampus.store.redis.Coordinates
import org.fastcampus.store.redis.StoreRedisRepository
Expand Down Expand Up @@ -186,7 +187,8 @@ class StoreService(
size: Int,
category: Store.Category?,
keyword: String?,
cursor: String?, // "distance_storeId" 형태
cursor: String?,
orderType: OrderType?,
): CursorDTOString<StoreInfo>? {
// 1) cursor 파싱 (ex: "13.23_STORE1234")
val (cursorDistance, cursorStoreId) = parseCursor(cursor)
Expand All @@ -199,6 +201,7 @@ class StoreService(
cursorDistance = cursorDistance,
cursorStoreId = cursorStoreId,
size = size,
orderType = orderType,
)

// 3) 결과를 StoreInfo로 변환
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ data class Store(
val roadAddress: String?,
val storeMenuCategory: List<StoreMenuCategory>?,
val minimumOrderAmount: Long,
var orderCount: Int?,
var rating: Float?,
var reviewCount: Int?,
) {
fun getCategories(): List<StoreMenuCategory> {
return storeMenuCategory ?: emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.fastcampus.store.enums

/**
* Created by brinst07 on 25. 3. 5.
*/
enum class OrderType {
DISTANCE,
RATING,
REVIEW,
ORDER_COUNT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.fastcampus.store.repository

import org.fastcampus.store.entity.Store
import org.fastcampus.store.entity.StoreWithDistance
import org.fastcampus.store.enums.OrderType

/**
* Created by brinst07 on 25. 1. 11..
Expand Down Expand Up @@ -35,6 +36,7 @@ interface StoreRepository {
cursorDistance: Double?,
cursorStoreId: String?,
size: Int,
orderType: OrderType?,
): Pair<List<StoreWithDistance>, String?>

fun existsStoreNearBy(storeId: String, latitude: Double, longitude: Double, distanceKM: Double): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class StoreDocument(
val minimumOrderAmount: Long,
@Transient
var distance: Double? = null, // geoNear로 임시 추가되는 필드
var orderCount: Int?,
var rating: Float?,
var reviewCount: Int?,
)

fun StoreDocument.toModel() =
Expand All @@ -59,4 +62,8 @@ fun StoreDocument.toModel() =
roadAddress,
storeMenuCategoryDocument?.map { it.toModel() },
minimumOrderAmount,
orderCount ?: 0,
rating ?: 0.0F,
reviewCount ?: 0,
)

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.fastcampus.store.mongo.repository
import org.bson.Document
import org.fastcampus.store.entity.Store
import org.fastcampus.store.entity.StoreWithDistance
import org.fastcampus.store.enums.OrderType
import org.fastcampus.store.exception.StoreException
import org.fastcampus.store.mongo.document.StoreDocument
import org.fastcampus.store.mongo.document.toModel
Expand Down Expand Up @@ -129,6 +130,7 @@ internal class StoreMongoRepositoryCustom(
cursorDistance: Double?,
cursorStoreId: String?,
size: Int,
orderType: OrderType?,
): Pair<List<StoreWithDistance>, String?> {
val pipeline = mutableListOf<AggregationOperation>()

Expand Down Expand Up @@ -162,14 +164,7 @@ internal class StoreMongoRepositoryCustom(
pipeline.add(Aggregation.match(Criteria().orOperator(c1, c2)))
}

pipeline.add(
Aggregation.sort(
Sort.by(
Sort.Order.asc("distance"),
Sort.Order.asc("id"),
),
),
)
addOrderCondition(pipeline, orderType)

pipeline.add(Aggregation.limit(size.toLong() + 1))

Expand Down Expand Up @@ -263,6 +258,33 @@ internal class StoreMongoRepositoryCustom(
}
return dbCategoryString
}

private fun addOrderCondition(pipeline: MutableList<AggregationOperation>, orderType: OrderType?): List<AggregationOperation> {
// orderType이 null이면 distance로 기본 정렬
if (orderType == null) {
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.asc("distance"))))
} else {
// orderType에 따라 다른 정렬 추가
when (orderType) {
OrderType.DISTANCE -> {
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.asc("distance"))))
}

OrderType.REVIEW -> {
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.desc("reviewCount"))))
}

OrderType.RATING -> {
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.desc("rating"))))
}

OrderType.ORDER_COUNT -> {
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.desc("orderCount"))))
}
}
}
return pipeline
}
}

private fun buildGeoNearOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ spring:
database: o2o
username: root
password: root