Skip to content

Commit 3ba6a46

Browse files
committed
feat: storeId를 통한 Cheer 조회 API 에 페이지네이션 추가
1 parent c76e3a0 commit 3ba6a46

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

src/main/java/eatda/controller/cheer/CheerController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public ResponseEntity<CheersResponse> getCheers(@RequestParam(defaultValue = "0"
4949

5050
@GetMapping("/api/shops/{storeId}/cheers")
5151
public ResponseEntity<CheersInStoreResponse> getCheersByStoreId(@PathVariable Long storeId,
52-
@RequestParam @Min(1) @Max(50) int size) {
53-
CheersInStoreResponse response = cheerService.getCheersByStoreId(storeId, size);
52+
@RequestParam(defaultValue = "0") @Min(0) int page,
53+
@RequestParam(defaultValue = "5") @Min(1) @Max(50) int size) {
54+
CheersInStoreResponse response = cheerService.getCheersByStoreId(storeId, page, size);
5455
return ResponseEntity.ok(response);
5556
}
5657
}

src/main/java/eatda/service/cheer/CheerService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ public CheersInStoreResponse getCheersByStoreId(Long storeId, int size) {
8181
.toList(); // TODO N+1 문제 해결
8282
return new CheersInStoreResponse(cheersResponse);
8383
}
84+
85+
@Transactional(readOnly = true)
86+
public CheersInStoreResponse getCheersByStoreId(Long storeId, int page, int size) {
87+
Store store = storeRepository.getById(storeId);
88+
List<Cheer> cheers = cheerRepository.findAllByStoreOrderByCreatedAtDesc(store, PageRequest.of(page, size));
89+
90+
List<CheerInStoreResponse> cheersResponse = cheers.stream()
91+
.map(CheerInStoreResponse::new)
92+
.toList(); // TODO N+1 문제 해결
93+
return new CheersInStoreResponse(cheersResponse);
94+
}
8495
}

src/test/java/eatda/controller/cheer/CheerControllerTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@ class GetCheersByStoreId {
102102
Cheer cheer1 = cheerGenerator.generateCommon(member1, store);
103103
Thread.sleep(5);
104104
Cheer cheer2 = cheerGenerator.generateCommon(member2, store);
105+
int page = 0;
106+
int size = 2;
105107

106108
CheersInStoreResponse response = given()
107109
.when()
108-
.queryParam("size", 2)
110+
.queryParam("page", page)
111+
.queryParam("size", size)
109112
.get("/api/shops/{storeId}/cheers", store.getId())
110113
.then()
111114
.statusCode(200)

src/test/java/eatda/document/store/CheerDocumentTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package eatda.document.store;
22

33
import static org.mockito.ArgumentMatchers.any;
4-
import static org.mockito.ArgumentMatchers.anyInt;
54
import static org.mockito.ArgumentMatchers.anyLong;
65
import static org.mockito.ArgumentMatchers.eq;
76
import static org.mockito.Mockito.doReturn;
@@ -208,7 +207,8 @@ class GetCheersByStoreId {
208207
parameterWithName("storeId").description("가게 ID")
209208
)
210209
.queryParameter(
211-
parameterWithName("size").description("조회 개수 (최소 1, 최대 50)")
210+
parameterWithName("page").description("조회 페이지 (최소 0, 기본값 0)").optional(),
211+
parameterWithName("size").description("조회 개수 (기본값 5, 최소 1, 최대 50)").optional()
212212
);
213213

214214
RestDocsResponse responseDocument = response()
@@ -223,12 +223,13 @@ class GetCheersByStoreId {
223223
@Test
224224
void 가게별_응원_검색_성공() {
225225
Long storeId = 1L;
226+
int page = 0;
226227
int size = 2;
227228
CheersInStoreResponse responses = new CheersInStoreResponse(List.of(
228229
new CheerInStoreResponse(20L, 5L, "커찬", "너무 맛있어요!"),
229230
new CheerInStoreResponse(10L, 3L, "찬커", "너무 매워요! 하지만 맛있어요!")
230231
));
231-
doReturn(responses).when(cheerService).getCheersByStoreId(storeId, size);
232+
doReturn(responses).when(cheerService).getCheersByStoreId(storeId, page, size);
232233

233234
var document = document("cheer/get-store-id", 200)
234235
.request(requestDocument)
@@ -237,6 +238,7 @@ class GetCheersByStoreId {
237238

238239
given(document)
239240
.contentType(ContentType.JSON)
241+
.queryParam("page", page)
240242
.queryParam("size", size)
241243
.when().get("/api/shops/{storeId}/cheers", storeId)
242244
.then().statusCode(200);
@@ -246,8 +248,9 @@ class GetCheersByStoreId {
246248
@ParameterizedTest
247249
void 가게별_응원_검색_실패(BusinessErrorCode errorCode) {
248250
Long storeId = 1L;
251+
int page = 0;
249252
int size = 2;
250-
doThrow(new BusinessException(errorCode)).when(cheerService).getCheersByStoreId(eq(storeId), anyInt());
253+
doThrow(new BusinessException(errorCode)).when(cheerService).getCheersByStoreId(storeId, page, size);
251254

252255
var document = document("cheer/get-store-id", errorCode)
253256
.request(requestDocument)
@@ -256,6 +259,7 @@ class GetCheersByStoreId {
256259

257260
given(document)
258261
.contentType(ContentType.JSON)
262+
.queryParam("page", page)
259263
.queryParam("size", size)
260264
.when().get("/api/shops/{storeId}/cheers", storeId)
261265
.then().statusCode(errorCode.getStatus().value());

src/test/java/eatda/service/cheer/CheerServiceTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,38 @@ class GetCheersByStoreId {
192192
Cheer cheer2 = cheerGenerator.generateCommon(member2, store);
193193
Thread.sleep(5);
194194
Cheer cheer3 = cheerGenerator.generateCommon(member3, store);
195+
int page = 0;
196+
int size = 2;
195197

196-
CheersInStoreResponse response = cheerService.getCheersByStoreId(store.getId(), 2);
198+
CheersInStoreResponse response = cheerService.getCheersByStoreId(store.getId(), page, size);
197199

198200
assertAll(
199201
() -> assertThat(response.cheers()).hasSize(2),
200202
() -> assertThat(response.cheers().get(0).id()).isEqualTo(cheer3.getId()),
201203
() -> assertThat(response.cheers().get(1).id()).isEqualTo(cheer2.getId())
202204
);
203205
}
206+
207+
@Test
208+
void 요청한_가게의_응원을_페이지네이션하여_최신순으로_반환한다() throws InterruptedException {
209+
Member member1 = memberGenerator.generateRegisteredMember("123", "[email protected]", "1234", "01012341234");
210+
Member member2 = memberGenerator.generateRegisteredMember("124", "[email protected]", "1235", "01012341235");
211+
Member member3 = memberGenerator.generateRegisteredMember("125", "[email protected]", "1236", "01012341236");
212+
Store store = storeGenerator.generate("123", "서울시 강남구 역삼동 123-45");
213+
Cheer cheer1 = cheerGenerator.generateCommon(member1, store);
214+
Thread.sleep(5);
215+
Cheer cheer2 = cheerGenerator.generateCommon(member2, store);
216+
Thread.sleep(5);
217+
Cheer cheer3 = cheerGenerator.generateCommon(member3, store);
218+
int page = 1;
219+
int size = 2;
220+
221+
CheersInStoreResponse response = cheerService.getCheersByStoreId(store.getId(), page, size);
222+
223+
assertAll(
224+
() -> assertThat(response.cheers()).hasSize(1),
225+
() -> assertThat(response.cheers().get(0).id()).isEqualTo(cheer1.getId())
226+
);
227+
}
204228
}
205229
}

0 commit comments

Comments
 (0)