Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] - 여행 계획 코드 리팩토링 #67

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저희 Logging 정책에 대해서도 이야기를 나눠봐야겠네요.
로그 메시지 컨벤션도 정해야 할 것 같습니다.


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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 순서를 외부에서 주입해준다.. 더 나은 방법이 있지 않을까요..
개발자가 오용하기 쉬운 코드인 것 같아요!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 착각했네요! Approve 하겠습니다!

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
Loading