Skip to content

Commit

Permalink
refactor: OrderProducts 유효성 검증 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hgo641 committed Apr 8, 2024
1 parent 64857c0 commit 4b76a51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class OrderProductsValidator(
fun validate(totalAmount: Money, orderProductCommands: List<OrderProductCommand>) {
validateProductsIsExist(orderProductCommands)
validateProductOptionsIsExist(orderProductCommands)
validateProductDetailIsMatching(orderProductCommands)
validateOrderProductPrices(orderProductCommands)
validateTotalAmount(totalAmount, orderProductCommands)
}
Expand All @@ -45,6 +46,16 @@ class OrderProductsValidator(
}
}

fun validateProductDetailIsMatching(orderProductCommands: List<OrderProductCommand>) {
orderProductCommands.forEach { orderProductCommand ->
val product = productById[orderProductCommand.productId]
?: throw OrderException(OrderExceptionType.PRODUCT_NOT_FOUND)
throwExceptionWhen(!product.isDetailMatching(orderProductCommand)) {
OrderException(OrderExceptionType.PRODUCT_INFO_NOT_MATCH)
}
}
}

fun validateOrderProductPrices(orderProductCommands: List<OrderProductCommand>) {
orderProductCommands.forEach { orderProductCommand ->
val product = productById[orderProductCommand.productId]
Expand Down Expand Up @@ -96,4 +107,17 @@ class OrderProductsValidator(
return find { it.productId == productId }
?: throw ProductException(ProductExceptionType.INVALID_PRODUCT_OPTION)
}

private fun Product.isDetailMatching(orderProductCommand: OrderProductCommand): Boolean {
if (price != orderProductCommand.originalPrice) {
return false
}
if (discountRate != orderProductCommand.discountRate) {
return false
}
if (discountPrice != orderProductCommand.discountPrice) {
return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum class OrderExceptionType(
PAYMENT_PRICE_NOT_MATCH(BAD_REQUEST, "O12", "주문한 상품 가격과 결제 금액이 일치하지 않습니다."),
EMPTY_SHIPPING_ADDRESS(BAD_REQUEST, "O13", "배송지가 입력되지 않았습니다."),
STORE_NOT_FOUND(BAD_REQUEST, "O14", "주문한 상품의 상점이 존재하지 않았습니다."),
PRODUCT_INFO_NOT_MATCH(BAD_REQUEST, "O15", "주문한 상품이 등록된 상품의 정보와 일치하지 않습니다."),

INVALID_PAYMENT_TYPE(BAD_REQUEST, "O20", "유효하지 않은 결제 방식입니다."),

Expand Down

0 comments on commit 4b76a51

Please sign in to comment.