Skip to content

Commit

Permalink
feat: 거래 날짜 검증 로직 추가 (#471)
Browse files Browse the repository at this point in the history
* feat: 거래 날짜 검증 로직 추가

* refactor: 검증 로직을 서비스로 이동
  • Loading branch information
masonkimseoul authored Sep 23, 2024
1 parent 91b2346 commit 14072b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private double roundHalfUp(double number, int roundingScale) {
private void validateOriginPrice() {
int dividedPrice = totalPrice / totalCount;
if (originPrice != null && originPrice < dividedPrice) {
throw new MarketException(OfferingErrorCode.CANNOT_ORIGIN_PRICE_LESS_THEN_DIVIDED_PRICE);
throw new MarketException(OfferingErrorCode.CANNOT_ORIGIN_PRICE_LESS_THAN_DIVIDED_PRICE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum OfferingErrorCode implements ErrorResponse {
INVALID_CONDITION(BAD_REQUEST, "유효하지 않은 공모 상태입니다"),
NOT_PARTICIPATE_MEMBER(BAD_REQUEST, "해당 공모의 참여자가 아닙니다."),
NOT_PROPOSE_MEMBER(BAD_REQUEST, "해당 공모의 총대가 아닙니다."),
CANNOT_ORIGIN_PRICE_LESS_THEN_DIVIDED_PRICE(BAD_REQUEST, "원가 가격이 n빵 가격보다 작을 수 없습니다.");
CANNOT_ORIGIN_PRICE_LESS_THAN_DIVIDED_PRICE(BAD_REQUEST, "원가 가격이 n빵 가격보다 작을 수 없습니다."),
CANNOT_MEETING_DATE_BEFORE_THAN_TOMORROW(BAD_REQUEST, "거래 날짜는 내일부터 설정할 수 있습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.zzang.chongdae.offeringmember.repository.OfferingMemberRepository;
import com.zzang.chongdae.offeringmember.repository.entity.OfferingMemberEntity;
import com.zzang.chongdae.storage.service.StorageService;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -112,6 +113,7 @@ private void validateIsProposer(OfferingEntity offering, MemberEntity member) {

public Long saveOffering(OfferingSaveRequest request, MemberEntity member) {
OfferingEntity offering = request.toEntity(member);
validateMeetingDate(offering);
OfferingEntity savedOffering = offeringRepository.save(offering);

OfferingMemberEntity offeringMember = new OfferingMemberEntity(member, offering, OfferingMemberRole.PROPOSER);
Expand All @@ -120,6 +122,13 @@ public Long saveOffering(OfferingSaveRequest request, MemberEntity member) {
return savedOffering.getId();
}

private void validateMeetingDate(OfferingEntity offering) {
LocalDate thresholdDate = LocalDate.now().plusDays(1);
if (offering.getMeetingDate().toLocalDate().isBefore(thresholdDate)) {
throw new MarketException(OfferingErrorCode.CANNOT_MEETING_DATE_BEFORE_THAN_TOMORROW);
}
}

public OfferingProductImageResponse uploadProductImageToS3(MultipartFile image) {
String imageUrl = storageService.uploadFile(image, "chongdae-market/images/offerings/product/");
return new OfferingProductImageResponse(imageUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,33 @@ void should_throwException_when_overMaximumTotalCount() {
.then().log().all()
.statusCode(400);
}

@DisplayName("거래 날짜를 내일보다 과거로 설정하는 경우 예외가 발생한다.")
@Test
void should_throwException_when_meetingDateBeforeTomorrow() {
OfferingSaveRequest request = new OfferingSaveRequest(
"공모 제목",
"www.naver.com",
"www.naver.com/favicon.ico",
10,
10000,
2000,
"서울특별시 광진구 구의강변로 3길 11",
"상세주소아파트",
"구의동",
LocalDateTime.now(),
"내용입니다."
);

given(spec).log().all()
.filter(document("create-offering-fail-with-invalid-meeting-date", resource(failSnippets)))
.cookies(cookieProvider.createCookiesWithMember(member))
.contentType(ContentType.JSON)
.body(request)
.when().post("/offerings")
.then().log().all()
.statusCode(400);
}
}

@DisplayName("상품 이미지 추출")
Expand Down

0 comments on commit 14072b3

Please sign in to comment.