From 547e040462ea86be2cffb54155201163bc33795e Mon Sep 17 00:00:00 2001 From: Combi153 Date: Tue, 20 Feb 2024 01:23:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Description,=20Option,=20Info=20?= =?UTF-8?q?=EC=9D=98=20=EC=99=B8=EB=9E=98=ED=82=A4=EB=A5=BC=20Product=20?= =?UTF-8?q?=EA=B0=80=20=EA=B4=80=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../petqua/common/config/DataInitializer.kt | 76 +++++++++---------- .../com/petqua/domain/product/Product.kt | 8 ++ .../product/ProductCustomRepositoryImpl.kt | 6 +- .../detail/description/ProductDescription.kt | 4 - .../domain/product/detail/info/ProductInfo.kt | 3 - .../domain/product/option/ProductOption.kt | 3 - .../application/product/ProductServiceTest.kt | 38 +++++----- .../ProductCustomRepositoryImplTest.kt | 54 ++++++------- .../product/ProductControllerTest.kt | 39 +++++----- .../petqua/test/fixture/ProductFixtures.kt | 12 +-- 10 files changed, 119 insertions(+), 124 deletions(-) diff --git a/src/main/kotlin/com/petqua/common/config/DataInitializer.kt b/src/main/kotlin/com/petqua/common/config/DataInitializer.kt index b0ccc8b7..56066f48 100644 --- a/src/main/kotlin/com/petqua/common/config/DataInitializer.kt +++ b/src/main/kotlin/com/petqua/common/config/DataInitializer.kt @@ -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, @@ -185,6 +218,9 @@ class DataInitializer( canDeliverSafely = canDeliverSafely, canDeliverCommonly = canDeliverCommonly, canPickUp = canPickUp, + productOptionId = productOption.id, + productDescriptionId = productDescriptionId, + productInfoId = productInfo.id, ) } productRepository.saveAll(products) @@ -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 @@ -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)) { diff --git a/src/main/kotlin/com/petqua/domain/product/Product.kt b/src/main/kotlin/com/petqua/domain/product/Product.kt index 2d35068b..3536e688 100644 --- a/src/main/kotlin/com/petqua/domain/product/Product.kt +++ b/src/main/kotlin/com/petqua/domain/product/Product.kt @@ -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 { diff --git a/src/main/kotlin/com/petqua/domain/product/ProductCustomRepositoryImpl.kt b/src/main/kotlin/com/petqua/domain/product/ProductCustomRepositoryImpl.kt index f07904c2..587bc1a6 100644 --- a/src/main/kotlin/com/petqua/domain/product/ProductCustomRepositoryImpl.kt +++ b/src/main/kotlin/com/petqua/domain/product/ProductCustomRepositoryImpl.kt @@ -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(), diff --git a/src/main/kotlin/com/petqua/domain/product/detail/description/ProductDescription.kt b/src/main/kotlin/com/petqua/domain/product/detail/description/ProductDescription.kt index 44c00c7c..0c8a8887 100644 --- a/src/main/kotlin/com/petqua/domain/product/detail/description/ProductDescription.kt +++ b/src/main/kotlin/com/petqua/domain/product/detail/description/ProductDescription.kt @@ -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 @@ -13,9 +12,6 @@ class ProductDescription( @Id @GeneratedValue(strategy = IDENTITY) val id: Long = 0, - @Column(nullable = false) - val productId: Long, - @Embedded val title: ProductDescriptionTitle, diff --git a/src/main/kotlin/com/petqua/domain/product/detail/info/ProductInfo.kt b/src/main/kotlin/com/petqua/domain/product/detail/info/ProductInfo.kt index 0eefe8f3..9552c301 100644 --- a/src/main/kotlin/com/petqua/domain/product/detail/info/ProductInfo.kt +++ b/src/main/kotlin/com/petqua/domain/product/detail/info/ProductInfo.kt @@ -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, diff --git a/src/main/kotlin/com/petqua/domain/product/option/ProductOption.kt b/src/main/kotlin/com/petqua/domain/product/option/ProductOption.kt index c34c3f1c..50205501 100644 --- a/src/main/kotlin/com/petqua/domain/product/option/ProductOption.kt +++ b/src/main/kotlin/com/petqua/domain/product/option/ProductOption.kt @@ -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, diff --git a/src/test/kotlin/com/petqua/application/product/ProductServiceTest.kt b/src/test/kotlin/com/petqua/application/product/ProductServiceTest.kt index c3438e35..879f2fec 100644 --- a/src/test/kotlin/com/petqua/application/product/ProductServiceTest.kt +++ b/src/test/kotlin/com/petqua/application/product/ProductServiceTest.kt @@ -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, @@ -118,12 +124,6 @@ class ProductServiceTest( imageType = DESCRIPTION ) ) - val productOption = productOptionRepository.save( - productOption( - productId = product.id, - sex = FEMALE, - ) - ) wishProductRepository.save( wishProduct( productId = product.id, diff --git a/src/test/kotlin/com/petqua/domain/product/ProductCustomRepositoryImplTest.kt b/src/test/kotlin/com/petqua/domain/product/ProductCustomRepositoryImplTest.kt index a5d3faf4..c120a107 100644 --- a/src/test/kotlin/com/petqua/domain/product/ProductCustomRepositoryImplTest.kt +++ b/src/test/kotlin/com/petqua/domain/product/ProductCustomRepositoryImplTest.kt @@ -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, @@ -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, @@ -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) { diff --git a/src/test/kotlin/com/petqua/presentation/product/ProductControllerTest.kt b/src/test/kotlin/com/petqua/presentation/product/ProductControllerTest.kt index ab468b7a..611e415c 100644 --- a/src/test/kotlin/com/petqua/presentation/product/ProductControllerTest.kt +++ b/src/test/kotlin/com/petqua/presentation/product/ProductControllerTest.kt @@ -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, @@ -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, diff --git a/src/test/kotlin/com/petqua/test/fixture/ProductFixtures.kt b/src/test/kotlin/com/petqua/test/fixture/ProductFixtures.kt index 48773223..11e5c389 100644 --- a/src/test/kotlin/com/petqua/test/fixture/ProductFixtures.kt +++ b/src/test/kotlin/com/petqua/test/fixture/ProductFixtures.kt @@ -38,6 +38,9 @@ fun product( canDeliverySafely: Boolean = true, canDeliveryCommonly: Boolean = true, canPickUp: Boolean = true, + productOptionId: Long = 0, + productDescriptionId: Long? = null, + productInfoId: Long = 0, ): Product { return Product( id = id, @@ -55,6 +58,9 @@ fun product( canDeliverSafely = canDeliverySafely, canDeliverCommonly = canDeliveryCommonly, canPickUp = canPickUp, + productOptionId = productOptionId, + productDescriptionId = productDescriptionId, + productInfoId = productInfoId ) } @@ -72,7 +78,6 @@ fun productKeyword( fun productInfo( id: Long = 0L, - productId: Long, categoryId: Long, optimalTemperature: OptimalTemperature, difficultyLevel: DifficultyLevel, @@ -81,7 +86,6 @@ fun productInfo( ): ProductInfo { return ProductInfo( id = id, - productId = productId, categoryId = categoryId, optimalTemperature = optimalTemperature, difficultyLevel = difficultyLevel, @@ -106,13 +110,11 @@ fun productImage( fun productOption( id: Long = 0, - productId: Long, sex: Sex, additionalPrice: BigDecimal = BigDecimal.ZERO, ): ProductOption { return ProductOption( id = id, - productId = productId, sex = sex, additionalPrice = additionalPrice, ) @@ -120,13 +122,11 @@ fun productOption( fun productDescription( id: Long = 0, - productId: Long, title: String = "제목", content: String = "내용", ): ProductDescription { return ProductDescription( id = id, - productId = productId, title = ProductDescriptionTitle(title), content = ProductDescriptionContent(content) )