Skip to content

Commit

Permalink
[MERGE] 주문 접수 요청 유효성 검증 추가
Browse files Browse the repository at this point in the history
[FEAT/#22] 주문 접수 요청 유효성 검증 추가
  • Loading branch information
seokbeom00 authored Oct 1, 2024
2 parents 2b67c35 + c5e672f commit 86a45fe
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
// JPA
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

// Validation
implementation 'org.springframework.boot:spring-boot-starter-validation'

// WEB
implementation 'org.springframework.boot:spring-boot-starter-web'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rootandfruit.server.dto.OrderRequestDto;
import com.rootandfruit.server.dto.OrderResponseDto;
import com.rootandfruit.server.service.OrdersService;
import jakarta.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,7 +28,7 @@ public class OrdersController implements OrdersControllerDocs {

@PostMapping("order")
public ResponseEntity<Integer> order(
@RequestBody OrderRequestDto orderRequestDto
@Valid @RequestBody OrderRequestDto orderRequestDto
) {
return ResponseEntity.ok(ordersService.order(orderRequestDto));
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/rootandfruit/server/dto/OrderRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.rootandfruit.server.dto;

import jakarta.persistence.Column;
import java.time.LocalDate;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

public record OrderRequestDto(
@NotBlank(message = "발신자명은 필수입니다.")
String senderName,
@NotBlank(message = "발신자 전화번호는 필수입니다.")
String senderPhone,
boolean isMarketingConsent,
List<RecipientDto> recipientInfo
@NotEmpty(message = "수신자 정보는 빈 값일 수 없습니다.")
@Valid List<RecipientDto> recipientInfo
) {
}
5 changes: 5 additions & 0 deletions src/main/java/com/rootandfruit/server/dto/ProductDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.rootandfruit.server.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record ProductDto(
@NotNull(message = "상품ID는 null값일 수 없습니다.")
Long productId,
@NotBlank(message = "상품명은 빈 값일 수 없습니다.")
String productName,
int productCount
) {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/rootandfruit/server/dto/RecipientDto.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package com.rootandfruit.server.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

public record RecipientDto(
@NotBlank(message = "수신자명은 필수입니다.")
String recipientName,
@NotBlank(message = "수신자 전화번호는 필수입니다.")
String recipientPhone,
@NotBlank(message = "수신자 주소는 필수입니다.")
String recipientAddress,
@NotBlank(message = "수신자 상세주소는 필수입니다.")
String recipientAddressDetail,
@NotBlank(message = "수신자 우편번호는 필수입니다.")
String recipientPostCode,
List<ProductDto> productInfo,
@NotEmpty(message = "상품정보는 빈 값일 수 없습니다.")
@Valid List<ProductDto> productInfo,
@NotNull(message = "배송날짜는 null값일 수 없습니다.")
LocalDate deliveryDate
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public static <T> ResponseDto<T> success(final T data) {
public static <T> ResponseDto<T> fail(ErrorType errorType) {
return new ResponseDto<>(errorType.getCode(), null, errorType.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public ResponseEntity<ErrorType> handleBusinessException(CustomException e) {
.status(e.getErrorType().getHttpStatus())
.body(e.getErrorType());
}

// valid에서 발생한 예외
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorType> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return ResponseEntity
.status(e.getStatusCode())
.body(ErrorType.INVALID_HTTP_REQUEST_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@RestControllerAdvice(
basePackages = "com.rootandfruit.server"
)

// TODO: 구현을 숨기고 이해를 어렵게한다는 이유로 곧 disable될 예정
public class ResponseDtoAdvice implements ResponseBodyAdvice<Object> {

@Override
Expand Down

0 comments on commit 86a45fe

Please sign in to comment.