Skip to content

Commit

Permalink
refactor: Description, Option, Info 의 외래키를 Product 가 관리하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Combi153 committed Feb 19, 2024
1 parent 98e95d9 commit 547e040
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 124 deletions.
76 changes: 36 additions & 40 deletions src/main/kotlin/com/petqua/common/config/DataInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,39 @@ class DataInitializer(
}
val reviewCount = (1..5).random()

val sex = when {
(it % 3) == 0 -> MALE
(it % 7) == 0 -> FEMALE
else -> HERMAPHRODITE
}
val productOption = productOptionRepository.save(
ProductOption(
sex = sex,
additionalPrice = BigDecimal.ZERO
)
)

val productInfo = productInfoRepository.save(
ProductInfo(
categoryId = categoryId,
optimalTemperature = OptimalTemperature(22, 25),
difficultyLevel = NORMAL,
optimalTankSize = TANK2,
temperament = PEACEFUL
)
)

val productDescriptionId = when {
(it % 4) != 0 -> productDescriptionRepository.save(
ProductDescription(
title = ProductDescriptionTitle("물생활 핵 인싸어, 상품$it"),
content = ProductDescriptionContent("지느러미가 아름다운 상품$it 입니다")
)
).id

else -> null
}

Product(
name = "상품$it",
categoryId = categoryId,
Expand All @@ -185,6 +218,9 @@ class DataInitializer(
canDeliverSafely = canDeliverSafely,
canDeliverCommonly = canDeliverCommonly,
canPickUp = canPickUp,
productOptionId = productOption.id,
productDescriptionId = productDescriptionId,
productInfoId = productInfo.id,
)
}
productRepository.saveAll(products)
Expand All @@ -200,18 +236,6 @@ class DataInitializer(
}
wishProductRepository.saveAll(wishProducts)

// productDescription
val productDescriptions = products.filter {
(it.id % 4).toInt() != 0
}.map {
ProductDescription(
productId = it.id,
title = ProductDescriptionTitle("물생활 핵 인싸어, ${it.name}"),
content = ProductDescriptionContent("지느러미가 아름다운 ${it.name}입니다")
)
}
productDescriptionRepository.saveAll(productDescriptions)

// productKeyword
val productKeywords = products.filter {
(it.id % 9).toInt() == 0
Expand All @@ -231,34 +255,6 @@ class DataInitializer(
}
recommendationRepository.saveAll(productRecommendations)

// productOption
val productOptions = products.map {
val sex = when {
(it.id % 3).toInt() == 0 -> MALE
(it.id % 7).toInt() == 0 -> HERMAPHRODITE
else -> FEMALE
}
ProductOption(
productId = it.id,
sex = sex,
additionalPrice = BigDecimal.ZERO
)
}
productOptionRepository.saveAll(productOptions)

// productInfo
val productInfos = products.map {
ProductInfo(
productId = it.id,
categoryId = it.categoryId,
optimalTemperature = OptimalTemperature(22, 25),
difficultyLevel = NORMAL,
optimalTankSize = TANK2,
temperament = PEACEFUL
)
}
productInfoRepository.saveAll(productInfos)

// productImage
val productImages = products.flatMap { product ->
List(Random.nextInt(1, 6)) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/petqua/domain/product/Product.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class Product(

@Column(nullable = false)
val canPickUp: Boolean,

@Column(nullable = false)
val productOptionId: Long,

val productDescriptionId: Long?,

@Column(nullable = false)
val productInfoId: Long,
) : BaseEntity(), SoftDeleteEntity {

fun averageReviewScore(): Double {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class ProductCustomRepositoryImpl(
).from(
entity(Product::class),
join(Store::class).on(path(Product::storeId).eq(path(Store::id))),
leftJoin(ProductDescription::class).on(path(Product::id).eq(path(ProductDescription::productId))),
join(ProductInfo::class).on(path(Product::id).eq(path(ProductInfo::productId))),
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)))
join(ProductOption::class).on(path(Product::productOptionId).eq(path(ProductOption::id)))
).whereAnd(
path(Product::id).eq(id),
active(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.petqua.domain.product.detail.description

import com.petqua.common.domain.BaseEntity
import jakarta.persistence.Column
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
Expand All @@ -13,9 +12,6 @@ class ProductDescription(
@Id @GeneratedValue(strategy = IDENTITY)
val id: Long = 0,

@Column(nullable = false)
val productId: Long,

@Embedded
val title: ProductDescriptionTitle,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class ProductInfo(
@Id @GeneratedValue(strategy = IDENTITY)
val id: Long = 0L,

@Column(nullable = false)
val productId: Long,

@Column(nullable = false)
val categoryId: Long,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class ProductOption(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L,

@Column(nullable = false)
val productId: Long,

@Column(nullable = false)
@Enumerated(STRING)
val sex: Sex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,39 @@ class ProductServiceTest(
species = "고정구피"
)
)
val product = productRepository.save(
product(
name = "고정구피",
storeId = store.id,
categoryId = category.id,
discountPrice = BigDecimal.ZERO,
reviewCount = 0,
reviewTotalScore = 0
)
)
val productDescription = productDescriptionRepository.save(
productDescription(
productId = product.id,
title = "물생활 핵 인싸어, 레드 브론즈 구피",
content = "레드 턱시도라고도 불리며 지느러미가 아름다운 구피입니다"
)
)
val productInfo = productInfoRepository.save(
productInfo(
productId = product.id,
categoryId = 0,
categoryId = category.id,
optimalTemperature = OptimalTemperature(26, 28),
difficultyLevel = DifficultyLevel.EASY,
optimalTankSize = OptimalTankSize.TANK1,
temperament = Temperament.PEACEFUL,
)
)
val productOption = productOptionRepository.save(
productOption(
sex = FEMALE,
)
)
val product = productRepository.save(
product(
name = "고정구피",
storeId = store.id,
categoryId = category.id,
discountPrice = BigDecimal.ZERO,
reviewCount = 0,
reviewTotalScore = 0,
productOptionId = productOption.id,
productDescriptionId = productDescription.id,
productInfoId = productInfo.id,
)
)
val productImage = productImageRepository.save(
productImage(
productId = product.id,
Expand All @@ -118,12 +124,6 @@ class ProductServiceTest(
imageType = DESCRIPTION
)
)
val productOption = productOptionRepository.save(
productOption(
productId = product.id,
sex = FEMALE,
)
)
wishProductRepository.save(
wishProduct(
productId = product.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,9 @@ class ProductCustomRepositoryImplTest(
species = "고정구피"
)
)
val product1 = productRepository.save(
product(
name = "고정구피",
storeId = store.id,
categoryId = category.id,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0
)
)
val product2 = productRepository.save(
product(
name = "팬시구피",
storeId = store.id,
categoryId = category.id,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0
)
)
val productInfo1 = productInfoRepository.save(
productInfo(
productId = product1.id,
categoryId = 0,
categoryId = category.id,
optimalTemperature = OptimalTemperature(26, 28),
difficultyLevel = EASY,
optimalTankSize = OptimalTankSize.TANK1,
Expand All @@ -99,8 +78,7 @@ class ProductCustomRepositoryImplTest(
)
val productInfo2 = productInfoRepository.save(
productInfo(
productId = product2.id,
categoryId = 0,
categoryId = category.id,
optimalTemperature = OptimalTemperature(26, 28),
difficultyLevel = EASY,
optimalTankSize = OptimalTankSize.TANK1,
Expand All @@ -109,23 +87,45 @@ class ProductCustomRepositoryImplTest(
)
val productOption1 = productOptionRepository.save(
productOption(
productId = product1.id,
sex = Sex.MALE,
)
)
val productOption2 = productOptionRepository.save(
productOption(
productId = product2.id,
sex = Sex.MALE,
)
)
val productDescription1 = productDescriptionRepository.save(
productDescription(
productId = product1.id,
title = "물생활 핵 인싸어, 레드 브론즈 구피",
content = "레드 턱시도라고도 불리며 지느러미가 아름다운 구피입니다"
)
)
val product1 = productRepository.save(
product(
name = "고정구피",
storeId = store.id,
categoryId = category.id,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0,
productOptionId = productOption1.id,
productDescriptionId = productDescription1.id,
productInfoId = productInfo1.id,
)
)
val product2 = productRepository.save(
product(
name = "팬시구피",
storeId = store.id,
categoryId = category.id,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0,
productOptionId = productOption2.id,
productInfoId = productInfo2.id,
)
)

When("Id를 입력하면") {
val productWithInfoResponse = productRepository.findProductWithInfoByIdOrThrow(product1.id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,39 @@ class ProductControllerTest(
species = "고정구피"
)
)
val product = productRepository.save(
product(
name = "고정구피",
categoryId = category.id,
storeId = store.id,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0
)
)
val productDescription = productDescriptionRepository.save(
productDescription(
productId = product.id,
title = "물생활 핵 인싸어, 레드 브론즈 구피",
content = "레드 턱시도라고도 불리며 지느러미가 아름다운 구피입니다"
)
)
val productInfo = productInfoRepository.save(
productInfo(
productId = product.id,
categoryId = 0,
categoryId = category.id,
optimalTemperature = OptimalTemperature(26, 28),
difficultyLevel = DifficultyLevel.EASY,
optimalTankSize = OptimalTankSize.TANK1,
temperament = Temperament.PEACEFUL,
)
)
val productOption = productOptionRepository.save(
productOption(
sex = HERMAPHRODITE
)
)
val product = productRepository.save(
product(
name = "고정구피",
categoryId = category.id,
storeId = store.id,
discountPrice = ZERO,
reviewCount = 0,
reviewTotalScore = 0,
productOptionId = productOption.id,
productDescriptionId = productDescription.id,
productInfoId = productInfo.id
)
)
val productImage = productImageRepository.save(
productImage(
productId = product.id,
Expand All @@ -124,12 +130,7 @@ class ProductControllerTest(
imageType = ImageType.DESCRIPTION
)
)
val productOption = productOptionRepository.save(
productOption(
productId = product.id,
sex = HERMAPHRODITE
)
)

wishProductRepository.save(
wishProduct(
productId = product.id,
Expand Down
Loading

0 comments on commit 547e040

Please sign in to comment.