diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java index e761452..ba58757 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java @@ -1,6 +1,5 @@ package com.kampus.kbazaar.cart; -import com.kampus.kbazaar.product.Product; import com.kampus.kbazaar.promotion.Promotion; import com.kampus.kbazaar.shopper.Shopper; import jakarta.persistence.*; @@ -21,12 +20,8 @@ public class Cart { @Column(name = "id") private Long id; - @ManyToMany(cascade = {CascadeType.ALL}) - @JoinTable( - name = "cart_product", - joinColumns = @JoinColumn(name = "cart_id"), - inverseJoinColumns = @JoinColumn(name = "product_id")) - private Set products; + @OneToMany(mappedBy = "cart", cascade = CascadeType.ALL) + private Set products; @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable( @@ -39,9 +34,9 @@ public class Cart { @JoinColumn(name = "shopper_id", referencedColumnName = "id") private Shopper shopper; - public Cart addProduct(Product product) { - this.products.add(product); - product.getCarts().add(this); + public Cart addPromotion(Promotion promotion) { + this.promotions.add(promotion); + promotion.getCarts().add(this); return this; } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartProduct.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartProduct.java new file mode 100644 index 0000000..e65644a --- /dev/null +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartProduct.java @@ -0,0 +1,27 @@ +package com.kampus.kbazaar.cart; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CartProduct { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "product_id") + private Long productId; + + @Column(name = "quantity") + private Integer quantity; + + @ManyToOne + @JoinColumn(name = "cart_id") + private Cart cart; +} diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java index de73239..2e8ba07 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java @@ -1,13 +1,11 @@ package com.kampus.kbazaar.product; -import com.kampus.kbazaar.cart.Cart; import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.PositiveOrZero; import jakarta.validation.constraints.Size; import java.math.BigDecimal; -import java.util.Set; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -27,11 +25,6 @@ public class Product { @Column(name = "id") private Long id; - @ManyToMany( - cascade = {CascadeType.ALL}, - mappedBy = "products") - private Set carts; - @NotBlank @Size(max = 255) @Column(name = "name", nullable = false, length = 255)