Skip to content

Commit 4f238d9

Browse files
authored
[Feat] 응원 조회 API 구현
2 parents 453bacf + 0be77d8 commit 4f238d9

24 files changed

+533
-38
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
implementation 'org.springframework.boot:spring-boot-starter-actuator'
5757
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
5858
implementation 'org.springframework.boot:spring-boot-starter-web'
59+
implementation 'org.springframework.boot:spring-boot-starter-validation'
5960

6061
// Lombok
6162
compileOnly 'org.projectlombok:lombok'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package eatda.controller.store;
2+
3+
import eatda.service.store.CheerService;
4+
import jakarta.validation.constraints.Max;
5+
import jakarta.validation.constraints.Min;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequiredArgsConstructor
14+
public class CheerController {
15+
16+
private final CheerService cheerService;
17+
18+
@GetMapping("/api/cheer")
19+
public ResponseEntity<CheersResponse> getCheers(@RequestParam @Min(1) @Max(50) int size) {
20+
CheersResponse response = cheerService.getCheers(size);
21+
return ResponseEntity.ok(response);
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package eatda.controller.store;
2+
3+
import eatda.domain.store.Cheer;
4+
import eatda.domain.store.Store;
5+
6+
public record CheerPreviewResponse(
7+
long storeId,
8+
String imageUrl,
9+
String storeName,
10+
String storeDistrict,
11+
String storeNeighborhood,
12+
String storeCategory,
13+
long cheerId,
14+
String cheerDescription
15+
) {
16+
17+
public CheerPreviewResponse(Cheer cheer, Store store, String imageUrl) {
18+
this(
19+
store.getId(),
20+
imageUrl,
21+
store.getName(),
22+
store.getAddressDistrict(),
23+
store.getAddressNeighborhood(),
24+
store.getCategory().getCategoryName(),
25+
cheer.getId(),
26+
cheer.getDescription()
27+
);
28+
}
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package eatda.controller.store;
2+
3+
import java.util.List;
4+
5+
public record CheersResponse(List<CheerPreviewResponse> cheers) {
6+
7+
}

src/main/java/eatda/domain/store/Cheer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import jakarta.persistence.JoinColumn;
1414
import jakarta.persistence.ManyToOne;
1515
import jakarta.persistence.Table;
16-
import java.time.LocalDateTime;
1716
import lombok.AccessLevel;
1817
import lombok.Getter;
1918
import lombok.NoArgsConstructor;
@@ -56,6 +55,11 @@ public Cheer(Member member, Store store, String description, String imageKey) {
5655
this.isAdmin = false;
5756
}
5857

58+
public Cheer(Member member, Store store, String description, String imageKey, boolean isAdmin) {
59+
this(member, store, description, imageKey);
60+
this.isAdmin = isAdmin;
61+
}
62+
5963
private void validateDescription(String description) {
6064
if (description == null || description.isBlank()) {
6165
throw new BusinessException(BusinessErrorCode.INVALID_CHEER_DESCRIPTION);

src/main/java/eatda/domain/store/Store.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,20 @@ private Store(String kakaoId,
7070
this.lotNumberAddress = lotNumberAddress;
7171
this.coordinates = new Coordinates(latitude, longitude);
7272
}
73+
74+
public String getAddressDistrict() {
75+
String[] addressParts = lotNumberAddress.split(" ");
76+
if (addressParts.length < 2) {
77+
return "";
78+
}
79+
return addressParts[1];
80+
}
81+
82+
public String getAddressNeighborhood() {
83+
String[] addressParts = lotNumberAddress.split(" ");
84+
if (addressParts.length < 3) {
85+
return "";
86+
}
87+
return addressParts[2];
88+
}
7389
}

src/main/java/eatda/exception/EtcErrorCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public enum EtcErrorCode {
1515
NO_COOKIE_FOUND("CLIENT007", "필수 쿠키 값이 존재하지 않습니다.", HttpStatus.BAD_REQUEST),
1616
NO_HEADER_FOUND("CLIENT008", "필수 헤더 값이 존재하지 않습니다.", HttpStatus.BAD_REQUEST),
1717
NO_PARAMETER_FOUND("CLIENT009", "필수 파라미터 값이 존재하지 않습니다.", HttpStatus.BAD_REQUEST),
18+
VALIDATION_ERROR("CLIENT010", "요청 데이터 값이 범위를 벗어났습니다", HttpStatus.BAD_REQUEST),
1819

19-
INTERNAL_SERVER_ERROR("SERVER001", "서버 내부 에러가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
20+
INTERNAL_SERVER_ERROR("SERVER001", "서버 내부 에러가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR),
21+
;
2022

2123
private final String code;
2224
private final String message;

src/main/java/eatda/exception/GlobalExceptionHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.web.bind.MissingServletRequestParameterException;
1414
import org.springframework.web.bind.annotation.ExceptionHandler;
1515
import org.springframework.web.bind.annotation.RestControllerAdvice;
16+
import org.springframework.web.method.annotation.HandlerMethodValidationException;
1617
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
1718
import org.springframework.web.servlet.resource.NoResourceFoundException;
1819

@@ -77,6 +78,12 @@ public ResponseEntity<ErrorResponse> handleMissingServletRequestParameterExcepti
7778
return toErrorResponse(EtcErrorCode.NO_PARAMETER_FOUND);
7879
}
7980

81+
@ExceptionHandler(HandlerMethodValidationException.class)
82+
public ResponseEntity<ErrorResponse> handleHandlerMethodValidationException(
83+
HandlerMethodValidationException exception) {
84+
return toErrorResponse(EtcErrorCode.VALIDATION_ERROR);
85+
}
86+
8087
@ExceptionHandler(BusinessException.class)
8188
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException exception) {
8289
log.error("[BusinessException] handled: {}", exception.getErrorCode());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package eatda.repository.store;
2+
3+
import eatda.domain.store.Cheer;
4+
import java.util.List;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.repository.Repository;
7+
8+
public interface CheerRepository extends Repository<Cheer, Long> {
9+
10+
Cheer save(Cheer cheer);
11+
12+
List<Cheer> findAllByOrderByCreatedAtDesc(Pageable pageable);
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package eatda.repository.store;
2+
3+
import eatda.domain.store.Store;
4+
import org.springframework.data.repository.Repository;
5+
6+
public interface StoreRepository extends Repository<Store, Long> {
7+
8+
Store save(Store store);
9+
}

0 commit comments

Comments
 (0)