Skip to content

Commit e165c37

Browse files
authored
Merge pull request #310 from FC-InnerCircle-ICD2/feature/store-list-order
Feature/store list order
2 parents 74da477 + 594a527 commit e165c37

File tree

9 files changed

+62
-12
lines changed

9 files changed

+62
-12
lines changed

application-client/src/main/kotlin/org/fastcampus/applicationclient/store/controller/StoreController.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.fastcampus.common.dto.APIResponseDTO
1010
import org.fastcampus.common.dto.CursorDTO
1111
import org.fastcampus.common.dto.CursorDTOString
1212
import org.fastcampus.store.entity.Store
13+
import org.fastcampus.store.enums.OrderType
1314
import org.slf4j.Logger
1415
import org.slf4j.LoggerFactory
1516
import org.springframework.http.HttpStatus
@@ -127,8 +128,8 @@ class StoreController(
127128
@RequestParam(defaultValue = "5") size: Int,
128129
@RequestParam(required = false) category: Store.Category?,
129130
@RequestParam(required = false) keyword: String?,
130-
@RequestParam(required = false) cursor: String?, // "distance_storeId"
131-
// @RequestParam(required = false) order: OrderType?, // "distance_storeId"
131+
@RequestParam(required = false) cursor: String?,
132+
@RequestParam(required = false) orderType: OrderType?,
132133
): ResponseEntity<APIResponseDTO<CursorDTOString<StoreInfo>>> {
133134
logger.info("category: $category, searchCondition: $keyword")
134135
val result = storeService.getStoresByNearByAndConditionCursor(
@@ -138,6 +139,7 @@ class StoreController(
138139
category = category,
139140
keyword = keyword,
140141
cursor = cursor,
142+
orderType = orderType,
141143
)
142144
return ResponseEntity.ok(
143145
APIResponseDTO(

application-client/src/main/kotlin/org/fastcampus/applicationclient/store/controller/dto/request/StoreRequest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ enum class OrderType {
66
DISTANCE,
77
RATING,
88
REVIEW,
9+
ORDER_COUNT,
910
}

application-client/src/main/kotlin/org/fastcampus/applicationclient/store/service/StoreService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.fastcampus.common.dto.CursorDTO
1717
import org.fastcampus.common.dto.CursorDTOString
1818
import org.fastcampus.review.repository.ReviewRepository
1919
import org.fastcampus.store.entity.Store
20+
import org.fastcampus.store.enums.OrderType
2021
import org.fastcampus.store.exception.StoreException
2122
import org.fastcampus.store.redis.Coordinates
2223
import org.fastcampus.store.redis.StoreRedisRepository
@@ -186,7 +187,8 @@ class StoreService(
186187
size: Int,
187188
category: Store.Category?,
188189
keyword: String?,
189-
cursor: String?, // "distance_storeId" 형태
190+
cursor: String?,
191+
orderType: OrderType?,
190192
): CursorDTOString<StoreInfo>? {
191193
// 1) cursor 파싱 (ex: "13.23_STORE1234")
192194
val (cursorDistance, cursorStoreId) = parseCursor(cursor)
@@ -199,6 +201,7 @@ class StoreService(
199201
cursorDistance = cursorDistance,
200202
cursorStoreId = cursorStoreId,
201203
size = size,
204+
orderType = orderType,
202205
)
203206

204207
// 3) 결과를 StoreInfo로 변환

domains/store/src/main/kotlin/org/fastcampus/store/entity/Store.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ data class Store(
2323
val roadAddress: String?,
2424
val storeMenuCategory: List<StoreMenuCategory>?,
2525
val minimumOrderAmount: Long,
26+
var orderCount: Int?,
27+
var rating: Float?,
28+
var reviewCount: Int?,
2629
) {
2730
fun getCategories(): List<StoreMenuCategory> {
2831
return storeMenuCategory ?: emptyList()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.fastcampus.store.enums
2+
3+
/**
4+
* Created by brinst07 on 25. 3. 5.
5+
*/
6+
enum class OrderType {
7+
DISTANCE,
8+
RATING,
9+
REVIEW,
10+
ORDER_COUNT,
11+
}

domains/store/src/main/kotlin/org/fastcampus/store/repository/StoreRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.fastcampus.store.repository
22

33
import org.fastcampus.store.entity.Store
44
import org.fastcampus.store.entity.StoreWithDistance
5+
import org.fastcampus.store.enums.OrderType
56

67
/**
78
* Created by brinst07 on 25. 1. 11..
@@ -35,6 +36,7 @@ interface StoreRepository {
3536
cursorDistance: Double?,
3637
cursorStoreId: String?,
3738
size: Int,
39+
orderType: OrderType?,
3840
): Pair<List<StoreWithDistance>, String?>
3941

4042
fun existsStoreNearBy(storeId: String, latitude: Double, longitude: Double, distanceKM: Double): Boolean

infrastructure/store-mongo/src/main/kotlin/org/fastcampus/store/mongo/document/StoreDocument.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class StoreDocument(
3737
val minimumOrderAmount: Long,
3838
@Transient
3939
var distance: Double? = null, // geoNear로 임시 추가되는 필드
40+
var orderCount: Int?,
41+
var rating: Float?,
42+
var reviewCount: Int?,
4043
)
4144

4245
fun StoreDocument.toModel() =
@@ -59,4 +62,8 @@ fun StoreDocument.toModel() =
5962
roadAddress,
6063
storeMenuCategoryDocument?.map { it.toModel() },
6164
minimumOrderAmount,
65+
orderCount ?: 0,
66+
rating ?: 0.0F,
67+
reviewCount ?: 0,
6268
)
69+

infrastructure/store-mongo/src/main/kotlin/org/fastcampus/store/mongo/repository/StoreMongoRepositoryCustom.kt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.fastcampus.store.mongo.repository
33
import org.bson.Document
44
import org.fastcampus.store.entity.Store
55
import org.fastcampus.store.entity.StoreWithDistance
6+
import org.fastcampus.store.enums.OrderType
67
import org.fastcampus.store.exception.StoreException
78
import org.fastcampus.store.mongo.document.StoreDocument
89
import org.fastcampus.store.mongo.document.toModel
@@ -129,6 +130,7 @@ internal class StoreMongoRepositoryCustom(
129130
cursorDistance: Double?,
130131
cursorStoreId: String?,
131132
size: Int,
133+
orderType: OrderType?,
132134
): Pair<List<StoreWithDistance>, String?> {
133135
val pipeline = mutableListOf<AggregationOperation>()
134136

@@ -162,14 +164,7 @@ internal class StoreMongoRepositoryCustom(
162164
pipeline.add(Aggregation.match(Criteria().orOperator(c1, c2)))
163165
}
164166

165-
pipeline.add(
166-
Aggregation.sort(
167-
Sort.by(
168-
Sort.Order.asc("distance"),
169-
Sort.Order.asc("id"),
170-
),
171-
),
172-
)
167+
addOrderCondition(pipeline, orderType)
173168

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

@@ -263,6 +258,33 @@ internal class StoreMongoRepositoryCustom(
263258
}
264259
return dbCategoryString
265260
}
261+
262+
private fun addOrderCondition(pipeline: MutableList<AggregationOperation>, orderType: OrderType?): List<AggregationOperation> {
263+
// orderType이 null이면 distance로 기본 정렬
264+
if (orderType == null) {
265+
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.asc("distance"))))
266+
} else {
267+
// orderType에 따라 다른 정렬 추가
268+
when (orderType) {
269+
OrderType.DISTANCE -> {
270+
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.asc("distance"))))
271+
}
272+
273+
OrderType.REVIEW -> {
274+
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.desc("reviewCount"))))
275+
}
276+
277+
OrderType.RATING -> {
278+
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.desc("rating"))))
279+
}
280+
281+
OrderType.ORDER_COUNT -> {
282+
pipeline.add(Aggregation.sort(Sort.by(Sort.Order.desc("orderCount"))))
283+
}
284+
}
285+
}
286+
return pipeline
287+
}
266288
}
267289

268290
private fun buildGeoNearOperation(

infrastructure/store-mongo/src/main/resources/application.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ spring:
66
database: o2o
77
username: root
88
password: root
9-

0 commit comments

Comments
 (0)