Skip to content

Commit 13d5e47

Browse files
committed
feat: '최신 응원 조회 API'의 검색 조건 추가 서비스 로직 구현
1 parent 3b52e95 commit 13d5e47

File tree

6 files changed

+133
-12
lines changed

6 files changed

+133
-12
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package eatda.controller.cheer;
22

3+
import eatda.controller.store.SearchDistrict;
34
import eatda.controller.web.auth.LoginMember;
45
import eatda.domain.ImageDomain;
56
import eatda.domain.ImageKey;
7+
import eatda.domain.cheer.CheerTagName;
8+
import eatda.domain.store.StoreCategory;
69
import eatda.domain.store.StoreSearchResult;
710
import eatda.service.cheer.CheerService;
811
import eatda.service.image.ImageService;
912
import eatda.service.store.StoreSearchService;
1013
import jakarta.validation.constraints.Max;
1114
import jakarta.validation.constraints.Min;
15+
import java.util.List;
1216
import lombok.RequiredArgsConstructor;
1317
import org.springframework.http.HttpStatus;
1418
import org.springframework.http.ResponseEntity;
@@ -44,8 +48,12 @@ public ResponseEntity<CheerResponse> registerCheer(@RequestPart("request") Cheer
4448

4549
@GetMapping("/api/cheer")
4650
public ResponseEntity<CheersResponse> getCheers(@RequestParam(defaultValue = "0") @Min(0) int page,
47-
@RequestParam(defaultValue = "5") @Min(1) @Max(50) int size) {
48-
CheersResponse response = cheerService.getCheers(page, size);
51+
@RequestParam(defaultValue = "5") @Min(1) @Max(50) int size,
52+
@RequestParam(required = false) StoreCategory category,
53+
@RequestParam(required = false) List<CheerTagName> tag,
54+
@RequestParam(required = false) List<SearchDistrict> location) {
55+
CheerSearchParameters searchParameters = new CheerSearchParameters(page, size, category, tag, location);
56+
CheersResponse response = cheerService.getCheers(searchParameters);
4957
return ResponseEntity.ok(response);
5058
}
5159

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package eatda.controller.cheer;
2+
3+
import eatda.controller.store.SearchDistrict;
4+
import eatda.domain.cheer.CheerTagName;
5+
import eatda.domain.store.District;
6+
import eatda.domain.store.StoreCategory;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import lombok.Getter;
10+
import org.springframework.lang.Nullable;
11+
12+
public class CheerSearchParameters {
13+
14+
@Getter
15+
private final int page;
16+
@Getter
17+
private final int size;
18+
@Nullable
19+
private final StoreCategory category;
20+
private final List<CheerTagName> tag;
21+
private final List<SearchDistrict> location;
22+
23+
public CheerSearchParameters(int page,
24+
int size,
25+
@Nullable StoreCategory category,
26+
@Nullable List<CheerTagName> tag,
27+
@Nullable List<SearchDistrict> location) {
28+
this.page = page;
29+
this.size = size;
30+
this.category = category;
31+
this.tag = tag != null ? tag : Collections.emptyList();
32+
this.location = location != null ? location : Collections.emptyList();
33+
}
34+
35+
@Nullable
36+
public StoreCategory getCategory() {
37+
return category;
38+
}
39+
40+
public List<CheerTagName> getCheerTagNames() {
41+
return tag;
42+
}
43+
44+
public List<District> getDistricts() {
45+
return location.stream()
46+
.flatMap(district -> district.getDistricts().stream())
47+
.distinct()
48+
.toList();
49+
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package eatda.controller.store;
2+
3+
import eatda.domain.store.District;
4+
import java.util.List;
5+
import lombok.Getter;
6+
7+
@Getter
8+
public enum SearchDistrict {
9+
10+
GANGNAM("강남/역삼/선릉", List.of(District.GANGNAM)),
11+
KONDAE("건대/성수/서울숲/왕십리", List.of(District.SEONGDONG, District.GWANGJIN)),
12+
GEUMHO("금호/옥수/신당", List.of(District.JUNG)),
13+
HAPJEONG("합정/망원/홍대", List.of(District.MAPO)),
14+
SINCHON("신촌/이대", List.of(District.SEODAEMUN)),
15+
MYEONGDONG("명동/을지로/충무로", List.of(District.DONGDAEMUN, District.SEONGBUK)),
16+
SEOCHON("서촌/북촌/삼청", List.of(District.JONGNO)),
17+
DAECHI("대치/논현/서초", List.of(District.SEOCHO)),
18+
YONGSAN("용산/이태원/한남", List.of(District.YONGSAN, District.DONGJAK)),
19+
GEUMCHEON("금천/도봉/노원", List.of(District.GEUMCHEON, District.DOBONG, District.NOWON)),
20+
YEONGDEUNGPO("영등포/여의도", List.of(District.YEONGDEUNGPO)),
21+
JAMSIL("잠실/송파", List.of(District.SONGPA)),
22+
JONGRO("종로/광화문", List.of(District.JONGNO)),
23+
MAGOK("마곡/목동/강서", List.of(District.GANGSEO, District.YANGCHEON)),
24+
GURO("구로/서울대입구", List.of(District.GURO, District.GWANAK)),
25+
;
26+
27+
private String displayName;
28+
private List<District> districts;
29+
30+
SearchDistrict(String displayName, List<District> districts) {
31+
this.displayName = displayName;
32+
this.districts = districts;
33+
}
34+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import eatda.controller.cheer.CheerPreviewResponse;
55
import eatda.controller.cheer.CheerRegisterRequest;
66
import eatda.controller.cheer.CheerResponse;
7+
import eatda.controller.cheer.CheerSearchParameters;
78
import eatda.controller.cheer.CheersInStoreResponse;
89
import eatda.controller.cheer.CheersResponse;
910
import eatda.domain.ImageKey;
@@ -21,6 +22,8 @@
2122
import java.util.List;
2223
import lombok.RequiredArgsConstructor;
2324
import org.springframework.data.domain.PageRequest;
25+
import org.springframework.data.domain.Sort;
26+
import org.springframework.data.domain.Sort.Direction;
2427
import org.springframework.stereotype.Service;
2528
import org.springframework.transaction.annotation.Transactional;
2629

@@ -62,8 +65,13 @@ private void validateRegisterCheer(Member member, String storeKakaoId) {
6265
}
6366

6467
@Transactional(readOnly = true)
65-
public CheersResponse getCheers(int page, int size) {
66-
List<Cheer> cheers = cheerRepository.findAllByOrderByCreatedAtDesc(PageRequest.of(page, size));
68+
public CheersResponse getCheers(CheerSearchParameters parameters) {
69+
List<Cheer> cheers = cheerRepository.findAllByConditions(
70+
parameters.getCategory(),
71+
parameters.getCheerTagNames(),
72+
parameters.getDistricts(),
73+
PageRequest.of(parameters.getPage(), parameters.getSize(), Sort.by(Direction.DESC, "createdAt"))
74+
);
6775
return toCheersResponse(cheers);
6876
}
6977

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class GetCheers {
172172
new CheerPreviewResponse(1L, null, "석관동떡볶이", "성북구", "석관동", "기타", 1L,
173173
"너무 매워요! 하지만 맛있어요!", List.of(), 8L, "찬커")
174174
));
175-
doReturn(responses).when(cheerService).getCheers(page, size);
175+
doReturn(responses).when(cheerService).getCheers(any());
176176

177177
var document = document("cheer/get-many", 200)
178178
.request(requestDocument)
@@ -191,7 +191,7 @@ class GetCheers {
191191
void 음식점_검색_실패(BusinessErrorCode errorCode) {
192192
int page = 0;
193193
int size = 2;
194-
doThrow(new BusinessException(errorCode)).when(cheerService).getCheers(page, size);
194+
doThrow(new BusinessException(errorCode)).when(cheerService).getCheers(any());
195195

196196
var document = document("cheer/get-many", errorCode)
197197
.request(requestDocument)

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
import eatda.controller.cheer.CheerRegisterRequest;
88
import eatda.controller.cheer.CheerResponse;
9+
import eatda.controller.cheer.CheerSearchParameters;
910
import eatda.controller.cheer.CheersInStoreResponse;
1011
import eatda.controller.cheer.CheersResponse;
12+
import eatda.controller.store.SearchDistrict;
1113
import eatda.domain.ImageKey;
1214
import eatda.domain.cheer.Cheer;
1315
import eatda.domain.cheer.CheerTagName;
@@ -180,10 +182,9 @@ class GetCheers {
180182
Cheer cheer1 = cheerGenerator.generateAdmin(member, store1, startAt);
181183
Cheer cheer2 = cheerGenerator.generateAdmin(member, store1, startAt.plusHours(1));
182184
Cheer cheer3 = cheerGenerator.generateAdmin(member, store2, startAt.plusHours(2));
183-
int page = 0;
184-
int size = 2;
185+
CheerSearchParameters parameters = new CheerSearchParameters(0, 2, null, null, null);
185186

186-
CheersResponse response = cheerService.getCheers(page, size);
187+
CheersResponse response = cheerService.getCheers(parameters);
187188

188189
assertAll(
189190
() -> assertThat(response.cheers()).hasSize(2),
@@ -201,16 +202,36 @@ class GetCheers {
201202
Cheer cheer1 = cheerGenerator.generateAdmin(member, store1, startAt);
202203
Cheer cheer2 = cheerGenerator.generateAdmin(member, store1, startAt.plusHours(1));
203204
Cheer cheer3 = cheerGenerator.generateAdmin(member, store2, startAt.plusHours(2));
204-
int page = 1;
205-
int size = 2;
205+
CheerSearchParameters parameters = new CheerSearchParameters(1, 2, null, null, null);
206206

207-
CheersResponse response = cheerService.getCheers(page, size);
207+
CheersResponse response = cheerService.getCheers(parameters);
208208

209209
assertAll(
210210
() -> assertThat(response.cheers()).hasSize(1),
211211
() -> assertThat(response.cheers().get(0).cheerId()).isEqualTo(cheer1.getId())
212212
);
213213
}
214+
215+
@Test
216+
void 요청한_응원을_지역으로_필터링하여_최신순으로_반환한다() {
217+
Member member = memberGenerator.generate("123");
218+
Store store1 = storeGenerator.generate("123", "서울시 강남구 역삼동 123-45", District.GANGNAM);
219+
Store store2 = storeGenerator.generate("456", "서울시 성북구 석관동 123-45", District.SEONGBUK);
220+
LocalDateTime startAt = LocalDateTime.of(2025, 7, 26, 1, 0, 0);
221+
Cheer cheer1 = cheerGenerator.generateAdmin(member, store1, startAt);
222+
Cheer cheer2 = cheerGenerator.generateAdmin(member, store1, startAt.plusHours(1));
223+
Cheer cheer3 = cheerGenerator.generateAdmin(member, store2, startAt.plusHours(2));
224+
CheerSearchParameters parameters = new CheerSearchParameters(
225+
0, 2, null, null, List.of(SearchDistrict.GANGNAM));
226+
227+
CheersResponse response = cheerService.getCheers(parameters);
228+
229+
assertAll(
230+
() -> assertThat(response.cheers()).hasSize(2),
231+
() -> assertThat(response.cheers().get(0).cheerId()).isEqualTo(cheer2.getId()),
232+
() -> assertThat(response.cheers().get(1).cheerId()).isEqualTo(cheer1.getId())
233+
);
234+
}
214235
}
215236

216237
@Nested

0 commit comments

Comments
 (0)