-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: CartProduct 도메인 추가 * feat: Entity 존재 여부 확인 확장 함수 추가 * feat: CartProduct 저장 로직 추가 * refactor: ProductController 패키지 이동 * feat: CartProduct 저장 API 추가 * test: CartProduct 저장시 예외 상황 테스트 추가 * refactor: 중복된 dependency 제거 * chore: mockk 의존성 추가 * test: 로그인 셋업 메서드 추가 * test: 구현된 Auth 헤더 사용 하도록 CartProduct API 테스트 수정 * refactor: 중복된 dependency 제거 * refactor: Accessor -> LoginMember 네이밍 수정 * test: 검증부 내용 이름 수정 * feat: 회원 존재 유무 검증 로직 추가 * test: application 테스트내 빈 등록 범위 수정 * test: 회원 검증 추가에 따른 테스트 로직 수정 * feat: 상품 수량 값객체 추가 * feat: 봉달 상품 수량 값객체 적용 * test: 잘못된 봉달 상품 수량에 대한 테스트 추가 * refactor: 이미 봉달 목록에 존재하는 상품에 대한 처리 추가 * fix: value 필드 예약어 사용에 의한 오류 해결
- Loading branch information
1 parent
5e8e1e5
commit 2617f49
Showing
26 changed files
with
652 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/petqua/application/cart/CartProductService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.petqua.application.cart | ||
|
||
import com.petqua.application.cart.dto.SaveCartProductCommand | ||
import com.petqua.common.domain.existByIdOrThrow | ||
import com.petqua.domain.cart.CartProductRepository | ||
import com.petqua.domain.member.MemberRepository | ||
import com.petqua.domain.product.ProductRepository | ||
import com.petqua.exception.cart.CartProductException | ||
import com.petqua.exception.cart.CartProductExceptionType.DUPLICATED_PRODUCT | ||
import com.petqua.exception.member.MemberException | ||
import com.petqua.exception.member.MemberExceptionType.NOT_FOUND_MEMBER | ||
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 | ||
|
||
@Transactional | ||
@Service | ||
class CartProductService( | ||
private val cartProductRepository: CartProductRepository, | ||
private val productRepository: ProductRepository, | ||
private val memberRepository: MemberRepository, | ||
) { | ||
|
||
fun save(command: SaveCartProductCommand): Long { | ||
memberRepository.existByIdOrThrow(command.memberId, MemberException(NOT_FOUND_MEMBER)) | ||
productRepository.existByIdOrThrow(command.productId, ProductException(NOT_FOUND_PRODUCT)) | ||
validateDuplicatedProduct(command) | ||
return cartProductRepository.save(command.toCartProduct()).id | ||
} | ||
|
||
private fun validateDuplicatedProduct(command: SaveCartProductCommand) { | ||
cartProductRepository.findByMemberIdAndProductIdAndIsMaleAndDeliveryMethod( | ||
memberId = command.memberId, | ||
productId = command.productId, | ||
isMale = command.isMale, | ||
deliveryMethod = command.deliveryMethod | ||
)?.also { throw CartProductException(DUPLICATED_PRODUCT) } | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/com/petqua/application/cart/dto/CartProductDtos.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.petqua.application.cart.dto | ||
|
||
import com.petqua.domain.cart.CartProduct | ||
import com.petqua.domain.cart.CartProductQuantity | ||
import com.petqua.domain.cart.DeliveryMethod | ||
|
||
data class SaveCartProductCommand( | ||
val memberId: Long, | ||
val productId: Long, | ||
val quantity: Int, | ||
val isMale: Boolean, | ||
val deliveryMethod: DeliveryMethod, | ||
) { | ||
fun toCartProduct(): CartProduct { | ||
return CartProduct( | ||
memberId = memberId, | ||
productId = productId, | ||
quantity = CartProductQuantity(quantity), | ||
isMale = isMale, | ||
deliveryMethod = deliveryMethod, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.petqua.common.util | ||
|
||
inline fun throwExceptionWhen(condition: Boolean, exceptionSupplier: () -> RuntimeException) { | ||
if (condition) throw exceptionSupplier() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.petqua.domain.cart | ||
|
||
import com.petqua.common.domain.BaseEntity | ||
import jakarta.persistence.AttributeOverride | ||
import jakarta.persistence.Column | ||
import jakarta.persistence.Embedded | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.EnumType | ||
import jakarta.persistence.Enumerated | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
|
||
@Entity | ||
class CartProduct( | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0L, | ||
|
||
@Column(nullable = false) | ||
val memberId: Long, | ||
|
||
@Column(nullable = false) | ||
val productId: Long, | ||
|
||
@Embedded | ||
@AttributeOverride(name = "value", column = Column(name = "quantity", nullable = false)) | ||
val quantity: CartProductQuantity, | ||
|
||
@Column(nullable = false) | ||
val isMale: Boolean, | ||
|
||
@Enumerated(value = EnumType.STRING) | ||
@Column(nullable = false) | ||
val deliveryMethod: DeliveryMethod, | ||
) : BaseEntity() |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/petqua/domain/cart/CartProductQuantity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.petqua.domain.cart | ||
|
||
import com.petqua.common.util.throwExceptionWhen | ||
import com.petqua.exception.cart.CartProductException | ||
import com.petqua.exception.cart.CartProductExceptionType.PRODUCT_QUANTITY_OVER_MAXIMUM | ||
import com.petqua.exception.cart.CartProductExceptionType.PRODUCT_QUANTITY_UNDER_MINIMUM | ||
import jakarta.persistence.Embeddable | ||
|
||
private const val MIN_QUANTITY = 1 | ||
private const val MAX_QUANTITY = 99 | ||
|
||
@Embeddable | ||
class CartProductQuantity( | ||
val value: Int = 1, | ||
) { | ||
|
||
init { | ||
throwExceptionWhen(value < MIN_QUANTITY) { CartProductException(PRODUCT_QUANTITY_UNDER_MINIMUM) } | ||
throwExceptionWhen(value > MAX_QUANTITY) { CartProductException(PRODUCT_QUANTITY_OVER_MAXIMUM) } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/petqua/domain/cart/CartProductRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.petqua.domain.cart | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface CartProductRepository : JpaRepository<CartProduct, Long> { | ||
|
||
fun findByMemberIdAndProductIdAndIsMaleAndDeliveryMethod( | ||
memberId: Long, | ||
productId: Long, | ||
isMale: Boolean, | ||
deliveryMethod: DeliveryMethod | ||
): CartProduct? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.petqua.domain.cart | ||
|
||
import com.petqua.exception.cart.CartProductException | ||
import com.petqua.exception.cart.CartProductExceptionType.INVALID_DELIVERY_METHOD | ||
import java.util.Locale.ENGLISH | ||
|
||
enum class DeliveryMethod( | ||
val description: String, | ||
) { | ||
|
||
COMMON("일반 운송"), | ||
SAFETY("안전 운송"), | ||
PICK_UP("직접 방문"), | ||
; | ||
|
||
companion object { | ||
fun from(name: String): DeliveryMethod { | ||
return enumValues<DeliveryMethod>().find { it.name == name.uppercase(ENGLISH) } | ||
?: throw CartProductException(INVALID_DELIVERY_METHOD) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/petqua/exception/cart/CartProductException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.petqua.exception.cart | ||
|
||
import com.petqua.common.exception.BaseException | ||
import com.petqua.common.exception.BaseExceptionType | ||
|
||
class CartProductException( | ||
private val exceptionType: CartProductExceptionType, | ||
) : BaseException() { | ||
|
||
override fun exceptionType(): BaseExceptionType { | ||
return exceptionType | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/petqua/exception/cart/CartProductExceptionType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.petqua.exception.cart | ||
|
||
import com.petqua.common.exception.BaseExceptionType | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.HttpStatus.BAD_REQUEST | ||
|
||
enum class CartProductExceptionType( | ||
private val httpStatus: HttpStatus, | ||
private val errorMessage: String, | ||
) : BaseExceptionType { | ||
|
||
INVALID_DELIVERY_METHOD(httpStatus = BAD_REQUEST, errorMessage = "유효하지 않는 배송 방법입니다."), | ||
PRODUCT_QUANTITY_UNDER_MINIMUM(httpStatus = BAD_REQUEST, errorMessage = "최소 1개 이상의 상품을 담을 수 있습니다."), | ||
PRODUCT_QUANTITY_OVER_MAXIMUM(httpStatus = BAD_REQUEST, errorMessage = "최대 99개까지 구매할 수 있습니다."), | ||
DUPLICATED_PRODUCT(httpStatus = BAD_REQUEST, errorMessage = "이미 장바구니에 담긴 상품입니다.") | ||
; | ||
|
||
override fun httpStatus(): HttpStatus { | ||
return httpStatus | ||
} | ||
|
||
override fun errorMessage(): String { | ||
return errorMessage | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/petqua/exception/member/MemberException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.petqua.exception.member | ||
|
||
import com.petqua.common.exception.BaseException | ||
import com.petqua.common.exception.BaseExceptionType | ||
|
||
class MemberException( | ||
private val exceptionType: MemberExceptionType, | ||
) : BaseException() { | ||
|
||
override fun exceptionType(): BaseExceptionType { | ||
return exceptionType | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/petqua/exception/member/MemberExceptionType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.petqua.exception.member | ||
|
||
import com.petqua.common.exception.BaseExceptionType | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.HttpStatus.NOT_FOUND | ||
|
||
enum class MemberExceptionType( | ||
private val httpStatus: HttpStatus, | ||
private val errorMessage: String, | ||
) : BaseExceptionType { | ||
|
||
NOT_FOUND_MEMBER(NOT_FOUND, "존재하지 않는 회원입니다."), | ||
; | ||
|
||
override fun httpStatus(): HttpStatus { | ||
return httpStatus | ||
} | ||
|
||
override fun errorMessage(): String { | ||
return errorMessage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.