Skip to content

Commit

Permalink
[Feature] - 여행 계획 코드 리팩토링 (#67)
Browse files Browse the repository at this point in the history
* feat: MethodArgumentNotValidException handler 추가

* fix: TravelPlan Request에서 order 제거 후 index 사용으로 변경
  • Loading branch information
eunjungL authored Jul 19, 2024
1 parent 872557a commit 6b6bbcb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import woowacourse.touroot.global.exception.dto.ExceptionResponse;
Expand All @@ -13,8 +14,24 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ExceptionResponse> handleBadRequestException(BadRequestException exception) {
log.info("BAD_REQUEST_EXCEPTION :: message = {}", exception.getMessage());

ExceptionResponse data = new ExceptionResponse(exception.getMessage());
return ResponseEntity.badRequest()
.body(data);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse> handleMethodArgumentNotValidException(
MethodArgumentNotValidException exception
) {
log.info("METHOD_ARGUMENT_NOT_VALID_EXCEPTION :: message = {}", exception.getMessage());

String message = exception.getBindingResult()
.getAllErrors()
.get(0)
.getDefaultMessage();
ExceptionResponse data = new ExceptionResponse(message);
return ResponseEntity.badRequest()
.body(data);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package woowacourse.touroot.travelplan.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import woowacourse.touroot.travelplan.domain.TravelPlan;
import woowacourse.touroot.travelplan.domain.TravelPlanDay;

import java.util.List;

public record PlanDayCreateRequest(
@Schema(description = "여행 계획 날짜", example = "1")
@NotNull(message = "날짜는 비어있을 수 없습니다.")
@Min(value = 0, message = "날짜는 1 이상이어야 합니다.")
int day,
@Schema(description = "여행 장소 정보")
@NotNull(message = "여행 장소 정보는 비어있을 수 없습니다.") List<PlanPlaceCreateRequest> places
@Valid
@Size(min = 1, message = "여행 장소는 한 개 이상이어야 합니다.")
@NotNull(message = "여행 장소 정보는 비어있을 수 없습니다.")
List<PlanPlaceCreateRequest> places
) {

public TravelPlanDay toPlanDay(TravelPlan plan) {
return new TravelPlanDay(day, plan);
public TravelPlanDay toPlanDay(int order, TravelPlan plan) {
return new TravelPlanDay(order, plan);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package woowacourse.touroot.travelplan.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
Expand All @@ -15,14 +15,12 @@ public record PlanPlaceCreateRequest(
@NotBlank(message = "장소명은 비어있을 수 없습니다.") String placeName,
@Schema(description = "여행 장소 설명", example = "신나는 여행 장소")
String description,
@Schema(description = "여행 장소 순서", example = "1")
@NotNull
@Min(value = 0, message = "순서는 1 이상이어야 합니다.")
int order,
@NotNull PlanLocationCreateRequest location
@Valid
@NotNull(message = "위치는 비어있을 수 없습니다.")
PlanLocationCreateRequest location
) {

public TravelPlanPlace toPlanPlace(TravelPlanDay day, Place place) {
public TravelPlanPlace toPlanPlace(int order, TravelPlanDay day, Place place) {
return new TravelPlanPlace(description, order, day, place);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package woowacourse.touroot.travelplan.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import woowacourse.touroot.travelplan.domain.TravelPlan;

Expand All @@ -18,6 +20,8 @@ public record TravelPlanCreateRequest(
@NotNull(message = "시작일은 비어있을 수 없습니다.")
LocalDate startDate,
@Schema(description = "여행 날짜 정보")
@Valid
@Size(min = 1, message = "여행 날짜는 하루 이상 있어야 합니다.")
@NotNull(message = "여행 날짜 정보는 비어있을 수 없습니다.")
List<PlanDayCreateRequest> days
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ public TravelPlanCreateResponse createTravelPlan(TravelPlanCreateRequest request
travelPlan.validateStartDate();

TravelPlan savedTravelPlan = travelPlanRepository.save(travelPlan);
createPlanDay(request, savedTravelPlan);
createPlanDay(request.days(), savedTravelPlan);

return new TravelPlanCreateResponse(savedTravelPlan.getId());
}

private void createPlanDay(TravelPlanCreateRequest request, TravelPlan savedTravelPlan) {
for (PlanDayCreateRequest dayRequest : request.days()) {
// TODO: order는 배열 index로 변경
TravelPlanDay travelPlanDay = travelPlanDayRepository.save(dayRequest.toPlanDay(savedTravelPlan));
private void createPlanDay(List<PlanDayCreateRequest> request, TravelPlan savedTravelPlan) {
for (int order = 0; order < request.size(); order++) {
PlanDayCreateRequest dayRequest = request.get(order);
TravelPlanDay travelPlanDay = travelPlanDayRepository.save(dayRequest.toPlanDay(order, savedTravelPlan));
createPlanPlace(dayRequest.places(), travelPlanDay);
}
}

private void createPlanPlace(List<PlanPlaceCreateRequest> request, TravelPlanDay travelPlanDay) {
for (PlanPlaceCreateRequest planRequest : request) {
// TODO: order는 배열 index로 변경
for (int order = 0; order < request.size(); order++) {
PlanPlaceCreateRequest planRequest = request.get(order);
Place place = getPlace(planRequest);
travelPlanPlaceRepository.save(planRequest.toPlanPlace(travelPlanDay, place));
travelPlanPlaceRepository.save(planRequest.toPlanPlace(order, travelPlanDay, place));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ void createTravelPlan() {
PlanPlaceCreateRequest planPlaceCreateRequest = PlanPlaceCreateRequest.builder()
.placeName("잠실한강공원")
.description("신나는 여행 장소")
.order(0)
.location(locationRequest)
.build();
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(0, List.of(planPlaceCreateRequest));
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(List.of(planPlaceCreateRequest));
TravelPlanCreateRequest request = TravelPlanCreateRequest.builder()
.title("신나는 한강 여행")
.startDate(LocalDate.MAX)
Expand All @@ -78,10 +77,9 @@ void createTravelPlanWithInvalidStartDate() {
PlanPlaceCreateRequest planPlaceCreateRequest = PlanPlaceCreateRequest.builder()
.placeName("잠실한강공원")
.description("신나는 여행 장소")
.order(0)
.location(locationRequest)
.build();
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(0, List.of(planPlaceCreateRequest));
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(List.of(planPlaceCreateRequest));
TravelPlanCreateRequest request = TravelPlanCreateRequest.builder()
.title("신나는 한강 여행")
.startDate(LocalDate.MIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ void createTravelPlan() {
PlanPlaceCreateRequest planPlaceCreateRequest = PlanPlaceCreateRequest.builder()
.placeName("잠실한강공원")
.description("신나는 여행 장소")
.order(0)
.location(locationRequest)
.build();
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(0, List.of(planPlaceCreateRequest));
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(List.of(planPlaceCreateRequest));
TravelPlanCreateRequest request = TravelPlanCreateRequest.builder()
.title("신나는 한강 여행")
.startDate(LocalDate.MAX)
Expand All @@ -75,10 +74,9 @@ void createTravelPlanWithInvalidStartDate() {
PlanPlaceCreateRequest planPlaceCreateRequest = PlanPlaceCreateRequest.builder()
.placeName("잠실한강공원")
.description("신나는 여행 장소")
.order(0)
.location(locationRequest)
.build();
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(0, List.of(planPlaceCreateRequest));
PlanDayCreateRequest planDayCreateRequest = new PlanDayCreateRequest(List.of(planPlaceCreateRequest));
TravelPlanCreateRequest request = TravelPlanCreateRequest.builder()
.title("신나는 한강 여행")
.startDate(LocalDate.MIN)
Expand Down

0 comments on commit 6b6bbcb

Please sign in to comment.