Skip to content

Commit

Permalink
[BSVR-169] 리뷰 생성 API req validation 업데이트: 키워드 개수 조건, 날짜 포맷 (#90)
Browse files Browse the repository at this point in the history
* feat: CreateReviewRequestDto에 키워드 최소 1개 검증하는 메서드 구현

* feat: CreateReviewRequestDto의 datetime format yyyy-MM-dd HH:mm로 변경

* feat: CreateReviewRequestDto의 content필드도 null값 허용되게 업데이트

* feat: 리뷰 생성 키워드 검증 custom exception 생성 및 적용
  • Loading branch information
pminsung12 authored Jul 29, 2024
1 parent f424626 commit 33d7134
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public BaseReviewResponse create(
@PathVariable @Positive @NotNull final Long blockId,
@PathVariable @Positive @NotNull final Integer seatNumber,
@Parameter(hidden = true) Long memberId,
@RequestBody @Valid @NotNull CreateReviewRequest request) {
@RequestBody @Valid CreateReviewRequest request) {
CreateReviewUsecase.CreateReviewResult result =
createReviewUsecase.create(blockId, seatNumber, memberId, request.toCommand());
return BaseReviewResponse.from(result.review());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package org.depromeet.spot.application.review.dto.request;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

import jakarta.validation.constraints.NotNull;

import org.depromeet.spot.common.exception.review.ReviewException.InvalidReviewDateTimeFormatException;
import org.depromeet.spot.common.exception.review.ReviewException.InvalidReviewKeywordsException;
import org.depromeet.spot.usecase.port.in.review.CreateReviewUsecase.CreateReviewCommand;

public record CreateReviewRequest(
List<String> images, List<String> good, List<String> bad, String content, String dateTime) {
@NotNull List<String> images,
List<String> good,
List<String> bad,
String content,
@NotNull String dateTime) {

public CreateReviewCommand toCommand() {
validateGoodAndBad();
return CreateReviewCommand.builder()
.images(images)
.good(good)
Expand All @@ -23,12 +29,16 @@ public CreateReviewCommand toCommand() {
.build();
}

private LocalDateTime toLocalDateTime(String dateStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy.MM.dd");
private void validateGoodAndBad() {
if ((good == null || good.isEmpty()) && (bad == null || bad.isEmpty())) {
throw new InvalidReviewKeywordsException();
}
}

private LocalDateTime toLocalDateTime(String dateTimeStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
try {
LocalDate date = LocalDate.parse(dateStr, formatter);
// 시간 정보가 없으므로 자정(00:00)으로 설정
return LocalDateTime.of(date, LocalTime.MIDNIGHT);
return LocalDateTime.parse(dateTimeStr, formatter);
} catch (DateTimeParseException e) {
throw new InvalidReviewDateTimeFormatException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public enum ReviewErrorCode implements ErrorCode {
REVIEW_NOT_FOUND(HttpStatus.NOT_FOUND, "RV001", "요청한 리뷰를 찾을 수 없습니다."),
INVALID_REVIEW_DATA(HttpStatus.BAD_REQUEST, "RV002", "유효하지 않은 리뷰 데이터입니다."),
INVALID_REVIEW_DATETIME_FORMAT(
HttpStatus.BAD_REQUEST, "RV003", "리뷰 작성일시는 yyyy-MM-dd HH:mm 포맷이어야 합니다.");
HttpStatus.BAD_REQUEST, "RV003", "리뷰 작성일시는 yyyy-MM-dd HH:mm 포맷이어야 합니다."),
INVALID_REVIEW_KEYWORDS(
HttpStatus.BAD_REQUEST, "RV004", "리뷰의 'good' 또는 'bad' 중 적어도 하나는 제공되어야 합니다.");

private final HttpStatus status;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public InvalidReviewDateTimeFormatException(String str) {
super(ReviewErrorCode.INVALID_REVIEW_DATETIME_FORMAT.appended(str));
}
}

public static class InvalidReviewKeywordsException extends ReviewException {
public InvalidReviewKeywordsException() {
super(ReviewErrorCode.INVALID_REVIEW_KEYWORDS);
}

public InvalidReviewKeywordsException(String str) {
super(ReviewErrorCode.INVALID_REVIEW_KEYWORDS.appended(str));
}
}
}

0 comments on commit 33d7134

Please sign in to comment.