Skip to content

Commit

Permalink
refactor: 상품 상세 조회 시, 옵션별 추가금액 정보 포함하여 응답
Browse files Browse the repository at this point in the history
  • Loading branch information
TaeyeonRoyce committed Feb 26, 2024
1 parent 8e89d58 commit 3075f97
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 34 deletions.
11 changes: 10 additions & 1 deletion src/main/kotlin/com/petqua/application/product/ProductService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import com.petqua.domain.product.detail.image.ImageType.DESCRIPTION
import com.petqua.domain.product.detail.image.ImageType.SAMPLE
import com.petqua.domain.product.detail.image.ProductImageRepository
import com.petqua.domain.product.dto.ProductResponse
import com.petqua.domain.product.option.ProductOptionRepository
import com.petqua.domain.product.option.Sex.FEMALE
import com.petqua.domain.product.option.Sex.MALE
import com.petqua.exception.product.ProductException
import com.petqua.exception.product.ProductExceptionType.NOT_FOUND_PRODUCT
import org.springframework.stereotype.Service
Expand All @@ -25,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional
class ProductService(
private val productRepository: ProductRepository,
private val productImageRepository: ProductImageRepository,
private val productOptionRepository: ProductOptionRepository,
private val productKeywordRepository: ProductKeywordRepository,
private val wishProductRepository: WishProductRepository,
) {
Expand All @@ -40,12 +44,17 @@ class ProductService(
productId = productId,
memberId = loginMemberOrGuest.memberId
)
val productOptions = productOptionRepository.findAllByProductId(productId)
val maleAdditionalPrice = productOptions.find { it.sex == MALE }?.additionalPrice
val femaleAdditionalPrice = productOptions.find { it.sex == FEMALE }?.additionalPrice

return ProductDetailResponse(
productWithInfoResponse = productWithInfo,
imageUrls = imagesByType[SAMPLE] ?: emptyList(),
descriptionImageUrls = imagesByType[DESCRIPTION] ?: emptyList(),
isWished = isWished
isWished = isWished,
maleAdditionalPrice = maleAdditionalPrice,
femaleAdditionalPrice = femaleAdditionalPrice,
)
}

Expand Down
23 changes: 16 additions & 7 deletions src/main/kotlin/com/petqua/application/product/dto/ProductDtos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ data class ProductDetailResponse(
)
val pickUpDeliveryFee: BigDecimal?,

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

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

@Schema(
description = "사육 온도 최소",
example = "10"
Expand Down Expand Up @@ -156,12 +168,6 @@ data class ProductDetailResponse(
)
val temperament: String,

@Schema(
description = "암/수 성별을 갖는지 여부",
example = "true"
)
val hasDistinctSex: Boolean,

@Schema(
description = "찜 여부",
example = "true"
Expand All @@ -173,6 +179,8 @@ data class ProductDetailResponse(
imageUrls: List<String>,
descriptionImageUrls: List<String>,
isWished: Boolean,
maleAdditionalPrice: BigDecimal?,
femaleAdditionalPrice: BigDecimal?,
) : this(
id = productWithInfoResponse.id,
name = productWithInfoResponse.name,
Expand All @@ -192,12 +200,13 @@ data class ProductDetailResponse(
safeDeliveryFee = productWithInfoResponse.safeDeliveryFee,
commonDeliveryFee = productWithInfoResponse.commonDeliveryFee,
pickUpDeliveryFee = productWithInfoResponse.pickUpDeliveryFee,
maleAdditionalPrice = maleAdditionalPrice,
femaleAdditionalPrice = femaleAdditionalPrice,
optimalTemperatureMin = productWithInfoResponse.optimalTemperatureMin,
optimalTemperatureMax = productWithInfoResponse.optimalTemperatureMax,
difficultyLevel = productWithInfoResponse.difficultyLevel,
optimalTankSize = productWithInfoResponse.optimalTankSize,
temperament = productWithInfoResponse.temperament,
hasDistinctSex = productWithInfoResponse.hasDistinctSex,
isWished = isWished,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.petqua.domain.product.dto.ProductReadCondition
import com.petqua.domain.product.dto.ProductResponse
import com.petqua.domain.product.dto.ProductSearchCondition
import com.petqua.domain.product.dto.ProductWithInfoResponse
import com.petqua.domain.product.option.ProductOption
import com.petqua.domain.store.Store
import jakarta.persistence.EntityManager
import org.springframework.stereotype.Repository
Expand Down Expand Up @@ -49,14 +48,12 @@ class ProductCustomRepositoryImpl(
),
entity(ProductInfo::class),
entity(Category::class),
entity(ProductOption::class),
).from(
entity(Product::class),
join(Store::class).on(path(Product::storeId).eq(path(Store::id))),
leftJoin(ProductDescription::class).on(path(Product::productDescriptionId).eq(path(ProductDescription::id))),
join(ProductInfo::class).on(path(Product::productInfoId).eq(path(ProductInfo::id))),
join(Category::class).on(path(Product::categoryId).eq(path(Category::id))),
join(ProductOption::class).on(path(Product::id).eq(path(ProductOption::productId)))
).whereAnd(
path(Product::id).eq(id),
active(),
Expand Down
4 changes: 0 additions & 4 deletions src/main/kotlin/com/petqua/domain/product/dto/ProductDtos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.petqua.domain.product.ProductSourceType
import com.petqua.domain.product.Sorter
import com.petqua.domain.product.category.Category
import com.petqua.domain.product.detail.info.ProductInfo
import com.petqua.domain.product.option.ProductOption
import com.petqua.exception.product.ProductException
import com.petqua.exception.product.ProductExceptionType
import io.swagger.v3.oas.annotations.media.Schema
Expand Down Expand Up @@ -82,15 +81,13 @@ data class ProductWithInfoResponse(
val difficultyLevel: String,
val optimalTankSize: String,
val temperament: String,
val hasDistinctSex: Boolean,
) {
constructor(
product: Product,
storeName: String,
productDescription: ProductDescriptionResponse,
productInfo: ProductInfo,
category: Category,
productOption: ProductOption,
) : this(
id = product.id,
name = product.name,
Expand All @@ -114,7 +111,6 @@ data class ProductWithInfoResponse(
difficultyLevel = productInfo.difficultyLevel.description,
optimalTankSize = productInfo.optimalTankSize.description,
temperament = productInfo.temperament.description,
hasDistinctSex = productOption.hasDistinctSex(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository
interface ProductOptionRepository : JpaRepository<ProductOption, Long> {
fun findByProductIdIn(productIds: List<Long>): Set<ProductOption>
fun existsByProductIdAndSex(productId: Long, sex: Sex): Boolean
fun findAllByProductId(productId: Long): List<ProductOption>
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.petqua.domain.product.detail.info.ProductInfoRepository
import com.petqua.domain.product.detail.info.Temperament
import com.petqua.domain.product.option.ProductOptionRepository
import com.petqua.domain.product.option.Sex.FEMALE
import com.petqua.domain.product.option.Sex.MALE
import com.petqua.domain.store.StoreRepository
import com.petqua.exception.product.ProductException
import com.petqua.exception.product.ProductExceptionType.NOT_FOUND_PRODUCT
Expand All @@ -45,7 +46,7 @@ import com.petqua.test.fixture.wishProduct
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import java.math.BigDecimal
import java.math.BigDecimal.ZERO
import kotlin.Long.Companion.MIN_VALUE
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
Expand Down Expand Up @@ -97,17 +98,25 @@ class ProductServiceTest(
name = "고정구피",
storeId = store.id,
categoryId = category.id,
discountPrice = BigDecimal.ZERO,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0,
productDescriptionId = productDescription.id,
productInfoId = productInfo.id,
)
)
val productOption = productOptionRepository.save(
productOptionRepository.save(
productOption(
productId = product.id,
sex = MALE,
additionalPrice = ZERO,
)
)
productOptionRepository.save(
productOption(
productId = product.id,
sex = FEMALE,
additionalPrice = 2000.toBigDecimal(),
)
)
val productImage = productImageRepository.save(
Expand Down Expand Up @@ -146,7 +155,7 @@ class ProductServiceTest(
descriptionImageUrls = listOf(productDescriptionImage.imageUrl),
productInfo = productInfo,
category = category,
hasDistinctSex = productOption.hasDistinctSex(),
femaleAdditionalPrice = 2000.toBigDecimal(),
isWished = true
)
}
Expand Down Expand Up @@ -181,7 +190,7 @@ class ProductServiceTest(
descriptionImageUrls = listOf(productDescriptionImage.imageUrl),
productInfo = productInfo,
category = category,
hasDistinctSex = productOption.hasDistinctSex(),
femaleAdditionalPrice = 2000.toBigDecimal(),
isWished = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.petqua.domain.product.dto.ProductResponse
import com.petqua.domain.product.dto.ProductSearchCondition
import com.petqua.domain.product.dto.ProductWithInfoResponse
import com.petqua.domain.product.option.ProductOptionRepository
import com.petqua.domain.product.option.Sex
import com.petqua.domain.product.option.Sex.MALE
import com.petqua.domain.recommendation.ProductRecommendationRepository
import com.petqua.domain.store.StoreRepository
import com.petqua.exception.product.ProductException
Expand Down Expand Up @@ -114,16 +114,16 @@ class ProductCustomRepositoryImplTest(
productInfoId = productInfo2.id,
)
)
val productOption1 = productOptionRepository.save(
productOptionRepository.save(
productOption(
productId = product1.id,
sex = Sex.MALE,
sex = MALE,
)
)
val productOption2 = productOptionRepository.save(
productOptionRepository.save(
productOption(
productId = product2.id,
sex = Sex.MALE,
sex = MALE,
)
)

Expand All @@ -142,7 +142,6 @@ class ProductCustomRepositoryImplTest(
),
productInfo = productInfo1,
category = category,
productOption = productOption1,
)
}
}
Expand All @@ -162,7 +161,6 @@ class ProductCustomRepositoryImplTest(
),
productInfo = productInfo2,
category = category,
productOption = productOption2,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import com.petqua.domain.product.detail.info.ProductInfoRepository
import com.petqua.domain.product.detail.info.Temperament
import com.petqua.domain.product.dto.ProductResponse
import com.petqua.domain.product.option.ProductOptionRepository
import com.petqua.domain.product.option.Sex.HERMAPHRODITE
import com.petqua.domain.product.option.Sex.FEMALE
import com.petqua.domain.product.option.Sex.MALE
import com.petqua.domain.recommendation.ProductRecommendationRepository
import com.petqua.domain.store.StoreRepository
import com.petqua.exception.product.ProductExceptionType.INVALID_SEARCH_WORD
Expand Down Expand Up @@ -110,10 +111,19 @@ class ProductControllerTest(
productInfoId = productInfo.id
)
)
val productOption = productOptionRepository.save(
productOptionRepository.save(
productOption(
productId = product.id,
sex = HERMAPHRODITE
sex = MALE,
additionalPrice = ZERO,
)
)

productOptionRepository.save(
productOption(
productId = product.id,
sex = FEMALE,
additionalPrice = 2000.toBigDecimal(),
)
)
val productImage = productImageRepository.save(
Expand Down Expand Up @@ -157,7 +167,7 @@ class ProductControllerTest(
descriptionImageUrls = listOf(productDescriptionImage.imageUrl),
productInfo = productInfo,
category = category,
hasDistinctSex = productOption.hasDistinctSex(),
femaleAdditionalPrice = 2000.toBigDecimal(),
isWished = true
)
}
Expand Down Expand Up @@ -199,7 +209,7 @@ class ProductControllerTest(
descriptionImageUrls = listOf(productDescriptionImage.imageUrl),
productInfo = productInfo,
category = category,
hasDistinctSex = productOption.hasDistinctSex(),
femaleAdditionalPrice = 2000.toBigDecimal(),
isWished = false
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/kotlin/com/petqua/test/fixture/ProductFixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ fun productDetailResponse(
descriptionImageUrls: List<String>,
productInfo: ProductInfo,
category: Category,
hasDistinctSex: Boolean,
maleAdditionalPrice: BigDecimal? = BigDecimal.ZERO,
femaleAdditionalPrice: BigDecimal? = BigDecimal.ZERO,
isWished: Boolean,
): ProductDetailResponse {
return ProductDetailResponse(
Expand All @@ -161,12 +162,13 @@ fun productDetailResponse(
safeDeliveryFee = product.safeDeliveryFee,
commonDeliveryFee = product.commonDeliveryFee,
pickUpDeliveryFee = product.pickUpDeliveryFee,
maleAdditionalPrice = maleAdditionalPrice?.setScale(2),
femaleAdditionalPrice = femaleAdditionalPrice?.setScale(2),
optimalTemperatureMin = productInfo.optimalTemperature.optimalTemperatureMin,
optimalTemperatureMax = productInfo.optimalTemperature.optimalTemperatureMax,
difficultyLevel = productInfo.difficultyLevel.description,
optimalTankSize = productInfo.optimalTankSize.description,
temperament = productInfo.temperament.description,
hasDistinctSex = hasDistinctSex,
isWished = isWished,
)
}
Expand Down

0 comments on commit 3075f97

Please sign in to comment.