Skip to content

Commit

Permalink
placeOrder의 유효성 검증 중 `validateOrderQuantityInMaxOrderQuantityPerOrd…
Browse files Browse the repository at this point in the history
…er` 추가
  • Loading branch information
daadaadaah committed Jun 24, 2023
1 parent 91e8443 commit 86f98da
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public List<TimeDealProductSummary> getDealProductsByDealType(DealType dealType,
return sortedDealProducts;
}

public int getMaxOrderQuantityPerOrderByDealProductUuid(UUID dealProductUuid) {

String dealOpenDate = getDateForCurrentDealProducts();

String key = "timeDealProducts:"+dealOpenDate;

String hashKey = dealProductUuid.toString();

TimeDealProduct timeDealProduct = redisHashRepository.getOneByKeyAndHashKey(key, hashKey, new TypeReference<TimeDealProduct>() {});

return timeDealProduct.getMaxOrderQuantityPerOrder();
}

public TimeDealProductDetail getTimeDealProductDetailByDealProductUuid(UUID dealProductUuid) {

String dealOpenDate = getDateForCurrentDealProducts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ public class TimeDealProduct {
private final DiscountType dealProductDiscountType;
private final int dealProductDiscountValue;
private final int dealProductDealQuantity;

private final String[] productDetailImgUrls;

private final String productMainImgThumbnailUrl;
private final DealProductStatus dealProductStatus;
private final int maxOrderQuantityPerOrder;

@Builder
@ConstructorProperties({
Expand All @@ -33,7 +32,8 @@ public class TimeDealProduct {
"dealProductDealQuantity",
"productDetailImgUrls",
"productMainImgThumbnailUrl",
"dealProductStatus"
"dealProductStatus",
"maxOrderQuantityPerOrder"
})
public TimeDealProduct(
UUID dealProductUuid,
Expand All @@ -45,7 +45,8 @@ public TimeDealProduct(
int dealProductDealQuantity,
String[] productDetailImgUrls,
String productMainImgThumbnailUrl,
DealProductStatus dealProductStatus
DealProductStatus dealProductStatus,
int maxOrderQuantityPerOrder
) {
this.dealProductUuid = dealProductUuid;
this.dealProductTile = dealProductTile;
Expand All @@ -57,5 +58,6 @@ public TimeDealProduct(
this.productDetailImgUrls = productDetailImgUrls;
this.productMainImgThumbnailUrl = productMainImgThumbnailUrl;
this.dealProductStatus = dealProductStatus;
this.maxOrderQuantityPerOrder = maxOrderQuantityPerOrder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class TimeDealProductDetail {
private final DiscountType dealProductDiscountType;
private final int dealProductDiscountValue;
private final int dealProductDealQuantity;
private final int maxOrderQuantityPerOrder;

@Builder
public TimeDealProductDetail(
Expand All @@ -25,7 +26,8 @@ public TimeDealProductDetail(
int productOriginPrice,
DiscountType dealProductDiscountType,
int dealProductDiscountValue,
int dealProductDealQuantity
int dealProductDealQuantity,
int maxOrderQuantityPerOrder
) {
this.dealProductUuid = dealProductUuid;
this.dealProductTile = dealProductTile;
Expand All @@ -35,5 +37,6 @@ public TimeDealProductDetail(
this.dealProductDiscountType = dealProductDiscountType;
this.dealProductDiscountValue = dealProductDiscountValue;
this.dealProductDealQuantity = dealProductDealQuantity;
this.maxOrderQuantityPerOrder = maxOrderQuantityPerOrder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hcommerce.heecommerce.order;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class MaxOrderQuantityExceededException extends RuntimeException {
public MaxOrderQuantityExceededException(int maxOrderQuantityPerOrder) {
super("최대 주문 수량은 " + maxOrderQuantityPerOrder + "개 입니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -17,10 +15,6 @@
@RestController
public class OrderController {

@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addValidators(new OrderFormValidator());
}

private final OrderService orderService;

Expand Down

This file was deleted.

13 changes: 9 additions & 4 deletions src/main/java/com/hcommerce/heecommerce/order/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ public void placeOrder(OrderForm orderForm) {
}

private void validateOrderForm(OrderForm orderForm) {
validateHasDealProductUuid(orderForm.getDealProductUuid());
UUID orderUuid = orderForm.getDealProductUuid();

validateHasDealProductUuid(orderUuid);

// TODO : Mybatis 연동이 필요하므로, 다른 PR에서 작업할 예정
validateHasUserId(orderForm.getUserId());

// TODO : RedisRepository에 추가적인 함수 필요하므로, 다른 PR에서 작업할 예정
validateOrderQuantityInMaxOrderQuantityPerOrder(orderForm.getOrderQuantity());
validateOrderQuantityInMaxOrderQuantityPerOrder(orderUuid, orderForm.getOrderQuantity());
}

private void validateHasDealProductUuid(UUID dealProductUuid) {
Expand All @@ -56,7 +57,11 @@ private void validateHasUserId(int userId) {

}

private void validateOrderQuantityInMaxOrderQuantityPerOrder(int orderQuantity) {
private void validateOrderQuantityInMaxOrderQuantityPerOrder(UUID orderUuid, int orderQuantity) {
int maxOrderQuantity = dealQueryRepository.getMaxOrderQuantityPerOrderByDealProductUuid(orderUuid);

if(maxOrderQuantity < orderQuantity) {
throw new MaxOrderQuantityExceededException(maxOrderQuantity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class OrderServiceTest {

private static final UUID NOT_EXIST_DEAL_PRODUCTUUID = UUID.fromString("8b455042-e709-11ed-93e5-0242ac110001");

private static final UUID EXIST_DEAL_PRODUCTUUID = UUID.fromString("8b455042-e709-11ed-93e5-0242ac110000");

private static final int ORDER_QUANTITY_EXCEEDING_MAX_ORDER_QUANTITY = 9999;

@Nested
@DisplayName("completeOrderReceipt")
class Describe_CompleteOrderReceipt {
Expand Down Expand Up @@ -110,5 +114,39 @@ void It_throws_TimeDealProductNotFoundException() {
});
}
}

@Nested
@DisplayName("with invalid OrderForm's OrderQuantity exceeding maxOrderQuantity")
class Context_With_Invalid_OrderQuantity_Exceeding_MaxOrderQuantity {
@Test
@DisplayName("throws MaxOrderQuantityExceededException")
void It_throws_MaxOrderQuantityExceededException() {
// given
given(dealQueryRepository.hasDealProductUuid(EXIST_DEAL_PRODUCTUUID)).willReturn(true);
given(dealQueryRepository.getMaxOrderQuantityPerOrderByDealProductUuid(EXIST_DEAL_PRODUCTUUID)).willReturn(10);

OrderForm orderForm = OrderForm.builder()
.userId(1)
.recipientInfoForm(
RecipientInfoForm.builder()
.recipientName("leecommerce")
.recipientPhoneNumber("01087654321")
.recipientAddress("서울시 ")
.recipientDetailAddress("101호")
.shippingRequest("빠른 배송 부탁드려요!")
.build()
)
.outOfStockHandlingOption(ALL_CANCEL)
.dealProductUuid(EXIST_DEAL_PRODUCTUUID)
.orderQuantity(ORDER_QUANTITY_EXCEEDING_MAX_ORDER_QUANTITY)
.paymentType(PaymentType.CREDIT_CARD)
.build();

// then
assertThrows(MaxOrderQuantityExceededException.class, () -> {
orderService.placeOrder(orderForm);
});
}
}
}
}

0 comments on commit 86f98da

Please sign in to comment.