From 1d6a263ac327a366e0f27be6a008a54e66eaa655 Mon Sep 17 00:00:00 2001 From: 05AM Date: Fri, 15 Dec 2023 20:59:56 +0900 Subject: [PATCH] =?UTF-8?q?#140=20[FEAT]=20=EA=B2=B0=EC=A0=9C=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20api:=20=EA=B2=B0=EC=A0=9C=20=EA=B8=88=EC=95=A1=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/facade/impl/OrderFacadeImpl.java | 15 ++++--- .../domain/order/model/OrderAmount.java | 39 +++++++++++++++++++ .../sticker/provider/vo/StickerOrderInfo.java | 19 +++++++++ .../server/global/exception/ErrorType.java | 4 +- .../server/global/util/EntityDtoMapper.java | 22 ++--------- 5 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 server/src/main/java/org/tattour/server/domain/order/model/OrderAmount.java diff --git a/server/src/main/java/org/tattour/server/domain/order/facade/impl/OrderFacadeImpl.java b/server/src/main/java/org/tattour/server/domain/order/facade/impl/OrderFacadeImpl.java index f21de24..059a0f2 100644 --- a/server/src/main/java/org/tattour/server/domain/order/facade/impl/OrderFacadeImpl.java +++ b/server/src/main/java/org/tattour/server/domain/order/facade/impl/OrderFacadeImpl.java @@ -14,6 +14,7 @@ import org.tattour.server.domain.order.facade.dto.request.UpdateOrderStatusReq; import org.tattour.server.domain.order.facade.dto.response.ReadOrderHistoryListRes; import org.tattour.server.domain.order.facade.dto.response.ReadUserOrderHistoryListRes; +import org.tattour.server.domain.order.model.OrderAmount; import org.tattour.server.domain.order.model.OrderHistory; import org.tattour.server.domain.order.model.OrderStatus; import org.tattour.server.domain.order.model.OrderedProduct; @@ -88,13 +89,18 @@ private StickerOrderInfo getCartStickersOrderInfo(List carts) { @Override @Transactional public void order(PurchaseRequest purchaseRequest, CreateOrderReq orderReq) { - // todo: totalAmount 비교하고 다름 감지 로직 추가하기 (vo OrderAmount) User user = userProvider.readUserById(orderReq.getUserId()); + StickerOrderInfo stickerOrderInfo = getStickerOrderInfo(user, purchaseRequest); + OrderAmount orderAmount = OrderAmount.calculate( + stickerOrderInfo, + orderReq.getTotalAmount(), + orderReq.getShippingFee()); + OrderHistory orderHistory = orderService.saveOrder( OrderHistory.builder() - .productAmount(orderReq.getProductAmount()) - .shippingFee(orderReq.getShippingFee()) - .totalAmount(orderReq.getTotalAmount()) + .productAmount(orderAmount.getProductAmount()) + .shippingFee(orderAmount.getShippingFee()) + .totalAmount(orderAmount.getTotalAmount()) .recipientName(orderReq.getRecipientName()) .contact(orderReq.getContact()) .mailingAddress(orderReq.getMailingAddress()) @@ -103,7 +109,6 @@ public void order(PurchaseRequest purchaseRequest, CreateOrderReq orderReq) { .user(user) .build()); - StickerOrderInfo stickerOrderInfo = getStickerOrderInfo(user, purchaseRequest); List orderedProducts = orderService.saveOrderedProducts(orderHistory, stickerOrderInfo); diff --git a/server/src/main/java/org/tattour/server/domain/order/model/OrderAmount.java b/server/src/main/java/org/tattour/server/domain/order/model/OrderAmount.java new file mode 100644 index 0000000..787a023 --- /dev/null +++ b/server/src/main/java/org/tattour/server/domain/order/model/OrderAmount.java @@ -0,0 +1,39 @@ +package org.tattour.server.domain.order.model; + +import lombok.Getter; +import org.tattour.server.domain.sticker.provider.vo.StickerOrderInfo; +import org.tattour.server.global.exception.BusinessException; +import org.tattour.server.global.exception.ErrorType; + +@Getter +public class OrderAmount { + + private final int productAmount; + private final int shippingFee; + private final int totalAmount; + + private OrderAmount(StickerOrderInfo stickerOrderInfo, int requestTotalAmount, + int shippingFee) { + int totalAmount = stickerOrderInfo.calculateTotalAmount(shippingFee); + validate(requestTotalAmount, totalAmount); + + this.productAmount = stickerOrderInfo.calculateProductAmount(); + this.shippingFee = shippingFee; + this.totalAmount = totalAmount; + } + + public static OrderAmount calculate(StickerOrderInfo stickerOrderInfo, int requestTotalAmount, + int shippingFee) { + return new OrderAmount(stickerOrderInfo, requestTotalAmount, shippingFee); + } + + private void validate(int requestTotalAmount, int totalAmount) { + if (isTotalAmountNotMatch(requestTotalAmount, totalAmount)) { + throw new BusinessException(ErrorType.ORDER_AMOUNT_NOT_MATCH_EXCEPTION); + } + } + + private boolean isTotalAmountNotMatch(int requestTotalAmount, int totalAmount) { + return requestTotalAmount != totalAmount; + } +} diff --git a/server/src/main/java/org/tattour/server/domain/sticker/provider/vo/StickerOrderInfo.java b/server/src/main/java/org/tattour/server/domain/sticker/provider/vo/StickerOrderInfo.java index dd1d3e7..48dca1e 100644 --- a/server/src/main/java/org/tattour/server/domain/sticker/provider/vo/StickerOrderInfo.java +++ b/server/src/main/java/org/tattour/server/domain/sticker/provider/vo/StickerOrderInfo.java @@ -1,6 +1,7 @@ package org.tattour.server.domain.sticker.provider.vo; import java.util.Map; +import java.util.Objects; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; @@ -9,9 +10,27 @@ @Getter @AllArgsConstructor(access = AccessLevel.PRIVATE) public class StickerOrderInfo { + private Map stickerOrderInfos; public static StickerOrderInfo of(Map stickerOrderInfo) { return new StickerOrderInfo(stickerOrderInfo); } + + public int calculateProductAmount() { + return stickerOrderInfos + .entrySet() + .stream() + .mapToInt(entry -> getFinalProductPrice(entry.getKey()) * entry.getValue()) + .sum(); + } + + private int getFinalProductPrice(Sticker sticker) { + return Objects.isNull(sticker.getDiscountPrice()) + ? sticker.getPrice() : sticker.getDiscountPrice(); + } + + public int calculateTotalAmount(int shippingFee) { + return calculateProductAmount() + shippingFee; + } } diff --git a/server/src/main/java/org/tattour/server/global/exception/ErrorType.java b/server/src/main/java/org/tattour/server/global/exception/ErrorType.java index 71dbf42..40dbe35 100644 --- a/server/src/main/java/org/tattour/server/global/exception/ErrorType.java +++ b/server/src/main/java/org/tattour/server/global/exception/ErrorType.java @@ -13,7 +13,8 @@ public enum ErrorType { VALIDATION_INPUT_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), VALIDATION_WRONG_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 타입이 입력되었습니다."), INVALID_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 인자가 입력되었습니다."), - INVALID_ORDERSHEET_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST, "stickerId와 count를 둘 다 제공하거나 제공하지 말아야 합니다."), + INVALID_ORDERSHEET_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST, + "stickerId와 count를 둘 다 제공하거나 제공하지 말아야 합니다."), INVALID_IMAGE_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 이미지 파일입니다."), INVALID_CUSTOM_IMAGE_COUNT(HttpStatus.BAD_REQUEST, "이미지 파일이 1개 이상이어야합니다."), INVALID_MULTIPART_EXTENSION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 파일 확장자입니다."), @@ -66,6 +67,7 @@ public enum ErrorType { ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 유저 이름입니다."), ALREADY_EXIST_PRODUCTLIKED_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 좋아요한 상품입니다."), ALREADY_CANCELED_ORDER_HISTORY_EXCEPTION(HttpStatus.CONFLICT, "이미 취소 처리된 결제 내역입니다."), + ORDER_AMOUNT_NOT_MATCH_EXCEPTION(HttpStatus.CONFLICT, "주문 요청의 금액과 실제 결제 금액이 일치하지 않습니다."), /** * 500 INTERNAL SERVER ERROR diff --git a/server/src/main/java/org/tattour/server/global/util/EntityDtoMapper.java b/server/src/main/java/org/tattour/server/global/util/EntityDtoMapper.java index 1da4a9b..ac2dfd7 100644 --- a/server/src/main/java/org/tattour/server/global/util/EntityDtoMapper.java +++ b/server/src/main/java/org/tattour/server/global/util/EntityDtoMapper.java @@ -1,7 +1,6 @@ package org.tattour.server.global.util; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; import org.mapstruct.IterableMapping; import org.mapstruct.Mapping; @@ -17,7 +16,6 @@ import org.tattour.server.domain.order.provider.vo.OrderAmountDetailRes; import org.tattour.server.domain.order.provider.vo.OrderHistoryInfo; import org.tattour.server.domain.order.provider.vo.UserOrderHistoryInfo; -import org.tattour.server.domain.sticker.model.Sticker; import org.tattour.server.domain.sticker.provider.vo.StickerLikedInfo; import org.tattour.server.domain.sticker.provider.vo.StickerOrderInfo; import org.tattour.server.domain.user.model.ProductLiked; @@ -82,26 +80,14 @@ default List toOrderSheetStickerRes(StickerOrderInfo stick .collect(Collectors.toList()); } - default OrderAmountDetailRes toOrderAmountRes(StickerOrderInfo stickerOrderInfo, int shippingFee) { - int productAmount = stickerOrderInfo.getStickerOrderInfos() - .entrySet() - .stream() - .mapToInt(entry -> calculateProductAmount(entry.getKey(), entry.getValue())) - .sum(); - int totalAmount = calculateTotalAmount(productAmount, shippingFee); + default OrderAmountDetailRes toOrderAmountRes(StickerOrderInfo stickerOrderInfo, + int shippingFee) { + int productAmount = stickerOrderInfo.calculateProductAmount(); + int totalAmount = stickerOrderInfo.calculateTotalAmount(shippingFee); return OrderAmountDetailRes.of(totalAmount, productAmount, shippingFee); } - private int calculateProductAmount(Sticker sticker, int count) { - int price = Objects.isNull(sticker.getDiscountPrice()) ? sticker.getPrice() : sticker.getDiscountPrice(); - return price * count; - } - - private int calculateTotalAmount(int productAmount, int shippingFee) { - return productAmount + shippingFee; - } - // Custom CreateCustomSummaryRes toCustomApplySummaryInfo(Custom custom);