Skip to content

Commit

Permalink
fix: product 도메인 매핑 오류 수정, 개행 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogu1208 committed Aug 4, 2024
1 parent 9bfc559 commit 5a84d66
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
21 changes: 5 additions & 16 deletions src/main/kotlin/org/store/clothstar/product/domain/entity/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,17 @@ import org.store.clothstar.product.domain.type.SaleStatus
*/
@Entity
class Item (
@Column
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L,
var name: String,
@Column
var price: Int,
@Column
var stock: Int,
@Column
var saleStatus: SaleStatus,
@Column
var displayStatus: DisplayStatus,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_line_id")
@JoinColumn(name = "product_id")
val product: Product

) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L

// @ElementCollection
// @CollectionTable(name = "item_attributes", joinColumns = [JoinColumn(name = "item_id")])
//

}
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ import org.store.clothstar.product.domain.type.ProductColor
*/
@Entity
class OptionValue (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
val productColor: ProductColor, // 색상 코드
@Column(nullable = false)
var value: String,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_option_id")
val productOption: ProductOption
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import org.store.clothstar.product.dto.request.UpdateProductRequest

@Entity
class Product (
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var productId:Long? = null,

// 연관 관계 필드 (N:1)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
val member: Member,
@Column(name = "member_id")
val memberId: Long,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
val category: Category,
@Column(name = "category_id")
val categoryId: Long,

// 기본 정보 필드
var name: String,
Expand All @@ -41,15 +43,13 @@ class Product (
var saleStatus: SaleStatus, // 판매 상태

// 연관 관계 (1:N)
@OneToMany(mappedBy = "product_line_id", cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
@OneToMany(mappedBy = "product", cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
var productOptions: MutableSet<ProductOption> = mutableSetOf(),

@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
var items: MutableList<Item> = mutableListOf()
): BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id:Long = 0


/**
* @param UpdateProductRequest
Expand All @@ -71,17 +71,17 @@ class Product (
// productOptions 업데이트 로직 (삭제 불가)
request.productOptions?.let {
val newOptions = it.toSet()
val existingOptions = this.productOptions.map { it.id }.toSet()
val existingOptions = this.productOptions.map { it.productOptionId }.toSet()

// 기존 옵션들에 포함되지 않은 새 옵션 추가
newOptions.forEach { newOption ->
if (newOption.id !in existingOptions) {
if (newOption.productOptionId !in existingOptions) {
this.productOptions.add(newOption)
}
}

// 기존 옵션 삭제 방지
val toRemove = this.productOptions.filterNot { it.id in newOptions.map { it.id } }
val toRemove = this.productOptions.filterNot { it.productOptionId in newOptions.map { it.productOptionId } }
if (toRemove.isNotEmpty()) {
throw IllegalArgumentException("Existing product options cannot be removed")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ import org.store.clothstar.product.domain.type.OptionType
* }
*/
@Entity
class ProductOption (
class ProductOption(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val productOptionId: Long? = null,

val name: String,
val order: Int = 0,
val required: Boolean = true,
@Enumerated(EnumType.STRING)
val optionType: OptionType,

) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="product_id")
val product: Product,

@OneToMany(mappedBy = "product_line_id", cascade = [CascadeType.ALL], orphanRemoval = true)
var optionValues: MutableList<OptionValue> = mutableListOf()
}
@OneToMany(mappedBy = "productOption", cascade = [CascadeType.ALL], orphanRemoval = true)
var optionValues: MutableList<OptionValue> = mutableListOf(),
)

0 comments on commit 5a84d66

Please sign in to comment.