Skip to content

Commit

Permalink
refactor: Money 값객체 생성 및 적용 (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgo641 authored Mar 15, 2024
1 parent f17cf9d commit 1c5d75c
Show file tree
Hide file tree
Showing 33 changed files with 360 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.petqua.application.cart.dto.CartProductWithSupportedOptionResponse
import com.petqua.application.cart.dto.DeleteCartProductCommand
import com.petqua.application.cart.dto.SaveCartProductCommand
import com.petqua.application.cart.dto.UpdateCartProductOptionCommand
import com.petqua.common.domain.Money
import com.petqua.common.domain.existByIdOrThrow
import com.petqua.common.domain.findByIdOrThrow
import com.petqua.common.util.throwExceptionWhen
Expand All @@ -27,7 +28,6 @@ import com.petqua.exception.product.ProductException
import com.petqua.exception.product.ProductExceptionType.NOT_FOUND_PRODUCT
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal

@Transactional
@Service
Expand Down Expand Up @@ -86,7 +86,7 @@ class CartProductService(
)
}

private fun validateDeliveryFee(product: Product, deliveryMethod: DeliveryMethod, deliveryFee: BigDecimal) {
private fun validateDeliveryFee(product: Product, deliveryMethod: DeliveryMethod, deliveryFee: Money) {
product.getDeliveryFee(deliveryMethod)
.also { throwExceptionWhen(it != deliveryFee) { CartProductException(DIFFERENT_DELIVERY_FEE) } }
}
Expand Down
43 changes: 22 additions & 21 deletions src/main/kotlin/com/petqua/application/cart/dto/CartProductDtos.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.petqua.application.cart.dto

import com.petqua.common.domain.Money
import com.petqua.domain.cart.CartProduct
import com.petqua.domain.cart.CartProductQuantity
import com.petqua.domain.delivery.DeliveryMethod
Expand All @@ -14,7 +15,7 @@ data class SaveCartProductCommand(
val quantity: Int,
val sex: Sex,
val deliveryMethod: DeliveryMethod,
val deliveryFee: BigDecimal,
val deliveryFee: Money,
) {
fun toCartProduct(): CartProduct {
return CartProduct(
Expand All @@ -23,7 +24,7 @@ data class SaveCartProductCommand(
quantity = CartProductQuantity(quantity),
sex = sex,
deliveryMethod = deliveryMethod,
deliveryFee = deliveryFee.setScale(2),
deliveryFee = deliveryFee,
)
}
}
Expand All @@ -35,7 +36,7 @@ data class UpdateCartProductOptionCommand(
val quantity: CartProductQuantity,
val sex: Sex,
val deliveryMethod: DeliveryMethod,
val deliveryFee: BigDecimal,
val deliveryFee: Money,
)

data class DeleteCartProductCommand(
Expand Down Expand Up @@ -78,7 +79,7 @@ data class CartProductWithSupportedOptionResponse(
description = "상품 가격",
example = "30000"
)
val productPrice: Int,
val productPrice: Money,

@Schema(
description = "가격 할인율",
Expand All @@ -90,7 +91,7 @@ data class CartProductWithSupportedOptionResponse(
description = "할인 가격(판매 가격)",
example = "21000"
)
val productDiscountPrice: Int,
val productDiscountPrice: Money,

@Schema(
description = "봉달(장바구니) 수량",
Expand All @@ -116,7 +117,7 @@ data class CartProductWithSupportedOptionResponse(
description = "배송비",
example = "3000"
)
val deliveryFee: BigDecimal,
val deliveryFee: Money,

@Schema(
description = "판매 여부(품절 및 삭제 확인)",
Expand All @@ -129,37 +130,37 @@ data class CartProductWithSupportedOptionResponse(
description = "안전 배송 가격 (null인 경우 지원 X)",
example = "5000"
)
val safeDeliveryFee: BigDecimal?,
val safeDeliveryFee: Money?,

@Schema(
description = "일반 배송 가격 (null인 경우 지원 X)",
example = "3000"
)
val commonDeliveryFee: BigDecimal?,
val commonDeliveryFee: Money?,

@Schema(
description = "픽업 배송 가격 (null인 경우 지원 X)",
example = "0"
)
val pickUpDeliveryFee: BigDecimal?,
val pickUpDeliveryFee: Money?,

@Schema(
description = "수컷 추가 가격 (null인 경우 지원 X)",
example = "1000"
)
val maleAdditionalPrice: BigDecimal?,
val maleAdditionalPrice: Money?,

@Schema(
description = "암컷 추가 가격 (null인 경우 지원 X)",
example = "1000"
)
val femaleAdditionalPrice: BigDecimal?,
val femaleAdditionalPrice: Money?,
) {

constructor(
cartProductResponse: CartProductResponse,
maleAdditionalPrice: BigDecimal?,
femaleAdditionalPrice: BigDecimal?,
maleAdditionalPrice: Money?,
femaleAdditionalPrice: Money?,
) : this(
id = cartProductResponse.id,
storeName = cartProductResponse.storeName,
Expand Down Expand Up @@ -188,17 +189,17 @@ data class CartProductResponse(
val productId: Long,
val productName: String,
val productThumbnailUrl: String,
val productPrice: Int,
val productPrice: Money,
val productDiscountRate: Int,
val productDiscountPrice: Int,
val productDiscountPrice: Money,
val quantity: Int,
val sex: Sex,
val deliveryMethod: String,
val deliveryFee: BigDecimal,
val deliveryFee: Money,
val isOnSale: Boolean,
val safeDeliveryFee: BigDecimal?,
val commonDeliveryFee: BigDecimal?,
val pickUpDeliveryFee: BigDecimal?,
val safeDeliveryFee: Money?,
val commonDeliveryFee: Money?,
val pickUpDeliveryFee: Money?,
) {

constructor(
Expand All @@ -211,9 +212,9 @@ data class CartProductResponse(
productId = product?.id ?: 0L,
productName = product?.name ?: "",
productThumbnailUrl = product?.thumbnailUrl ?: "",
productPrice = product?.price?.intValueExact() ?: 0,
productPrice = product?.price ?: Money.from(BigDecimal.ZERO),
productDiscountRate = product?.discountRate ?: 0,
productDiscountPrice = product?.discountPrice?.intValueExact() ?: 0,
productDiscountPrice = product?.discountPrice ?: Money.from(BigDecimal.ZERO),
quantity = cartProduct.quantity.value,
sex = cartProduct.sex,
deliveryMethod = cartProduct.deliveryMethod.name,
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/com/petqua/application/order/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.petqua.application.order
import com.petqua.application.order.dto.SaveOrderCommand
import com.petqua.application.order.dto.SaveOrderResponse
import com.petqua.application.payment.infra.PaymentGatewayClient
import com.petqua.common.domain.Money
import com.petqua.common.domain.findByIdOrThrow
import com.petqua.common.util.throwExceptionWhen
import com.petqua.domain.order.Order
Expand Down Expand Up @@ -70,8 +71,8 @@ class OrderService(
?: throw ProductException(INVALID_PRODUCT_OPTION)

throwExceptionWhen(
productCommand.orderPrice.setScale(2) != (product.discountPrice + productOption.additionalPrice) * productCommand.quantity.toBigDecimal()
|| productCommand.deliveryFee.setScale(2) != product.getDeliveryFee(productCommand.deliveryMethod)
productCommand.orderPrice != (product.discountPrice + productOption.additionalPrice) * productCommand.quantity.toBigDecimal()
|| productCommand.deliveryFee != product.getDeliveryFee(productCommand.deliveryMethod)
) {
OrderException(
ORDER_PRICE_NOT_MATCH
Expand All @@ -89,11 +90,11 @@ class OrderService(
}
val orderDeliveryFee = groupBy.map { (storeDeliveryMethod, products) ->
val deliveryMethod = storeDeliveryMethod.second
products.first().getDeliveryFee(deliveryMethod).toInt()
products.first().getDeliveryFee(deliveryMethod).value.toInt() // TODO int로 바꾼 이유?
}.sum()

// 4. 총 결제 금액 검증
throwExceptionWhen(command.totalAmount != orderDeliveryFee.toBigDecimal() + command.orderProductCommands.sumOf { it.orderPrice }) {
throwExceptionWhen(command.totalAmount != Money.from(orderDeliveryFee.toBigDecimal() + command.orderProductCommands.sumOf { it.orderPrice.value })) {
OrderException(
ORDER_PRICE_NOT_MATCH
)
Expand Down
24 changes: 12 additions & 12 deletions src/main/kotlin/com/petqua/application/order/dto/OrderDtos.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package com.petqua.application.order.dto

import com.petqua.common.domain.Money
import com.petqua.domain.delivery.DeliveryMethod
import com.petqua.domain.order.OrderProduct
import com.petqua.domain.order.ShippingNumber
import com.petqua.domain.product.Product
import com.petqua.domain.product.option.ProductOption
import com.petqua.domain.product.option.Sex
import java.math.BigDecimal

data class SaveOrderCommand(
val memberId: Long,
val shippingAddressId: Long,
val shippingRequest: String?,
val orderProductCommands: List<OrderProductCommand>,
val totalAmount: BigDecimal,
val totalAmount: Money,
)

data class OrderProductCommand(
val productId: Long,
val quantity: Int,
val originalPrice: BigDecimal,
val originalPrice: Money,
val discountRate: Int,
val discountPrice: BigDecimal,
val orderPrice: BigDecimal,
val discountPrice: Money,
val orderPrice: Money,
val sex: Sex,
val additionalPrice: BigDecimal,
val deliveryFee: BigDecimal,
val additionalPrice: Money,
val deliveryFee: Money,
val deliveryMethod: DeliveryMethod,
) {

fun toProductOption(): ProductOption {
return ProductOption(
sex = sex,
productId = productId,
additionalPrice = additionalPrice.setScale(2),
additionalPrice = additionalPrice,
)
}

Expand All @@ -44,12 +44,12 @@ data class OrderProductCommand(
): OrderProduct {
return OrderProduct(
quantity = quantity,
originalPrice = originalPrice.setScale(2),
originalPrice = originalPrice,
discountRate = discountRate,
discountPrice = discountPrice.setScale(2),
deliveryFee = deliveryFee.setScale(2),
discountPrice = discountPrice,
deliveryFee = deliveryFee,
shippingNumber = shippingNumber,
orderPrice = orderPrice.setScale(2),
orderPrice = orderPrice,
productId = productId,
productName = product.name,
thumbnailUrl = product.thumbnailUrl,
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/petqua/application/payment/PaymentDtos.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.petqua.application.payment

import com.petqua.common.domain.Money
import com.petqua.domain.order.OrderNumber
import com.petqua.domain.payment.tosspayment.TossPaymentType
import com.petqua.exception.payment.FailPaymentCode
import com.petqua.exception.payment.FailPaymentException
import com.petqua.exception.payment.FailPaymentExceptionType.ORDER_NUMBER_MISSING_EXCEPTION
import java.math.BigDecimal

data class SucceedPaymentCommand(
val memberId: Long,
val paymentType: TossPaymentType,
val orderNumber: OrderNumber,
val paymentKey: String,
val amount: BigDecimal,
val amount: Money,
) {
fun toPaymentConfirmRequest(): PaymentConfirmRequestToPG {
return PaymentConfirmRequestToPG(
Expand All @@ -28,7 +28,7 @@ data class SucceedPaymentCommand(
paymentType: String,
orderId: String,
paymentKey: String,
amount: BigDecimal,
amount: Money,
): SucceedPaymentCommand {
return SucceedPaymentCommand(
memberId = memberId,
Expand Down
Loading

0 comments on commit 1c5d75c

Please sign in to comment.