CartItem.java
package com.ctrlbuy.webshop.entity;
import jakarta.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Entity
@Table(name = "cart_items")
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cart_id", nullable = false)
private Cart cart;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id", nullable = false)
private Product product;
@Column(name = "quantity", nullable = false)
private Integer quantity;
@Column(name = "unit_price", precision = 10, nullable = false)
private BigDecimal price;
@Column(name = "subtotal", precision = 10)
private BigDecimal subtotal;
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(name = "updated_at")
private LocalDateTime updatedAt;
@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
updatedAt = LocalDateTime.now();
calculateSubtotal();
}
@PreUpdate
protected void onUpdate() {
updatedAt = LocalDateTime.now();
calculateSubtotal();
}
// Constructors
public CartItem() {}
public CartItem(Cart cart, Product product, Integer quantity) {
this.cart = cart;
this.product = product;
this.quantity = quantity;
this.price = product.getPrice();
calculateSubtotal();
}
public CartItem(Product product, Integer quantity) {
this.product = product;
this.quantity = quantity;
this.price = product.getPrice();
calculateSubtotal();
}
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
if (product != null) {
this.price = product.getPrice();
calculateSubtotal();
}
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
calculateSubtotal();
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
calculateSubtotal();
}
public BigDecimal getSubtotal() {
return subtotal;
}
public void setSubtotal(BigDecimal subtotal) {
this.subtotal = subtotal;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public LocalDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
// Business methods
public void setUnitPrice(BigDecimal unitPrice) {
this.price = unitPrice;
calculateSubtotal();
}
public BigDecimal getUnitPrice() {
return this.price;
}
public void updateQuantity(Integer newQuantity) {
this.quantity = newQuantity;
calculateSubtotal();
this.updatedAt = LocalDateTime.now();
}
public void calculateSubtotal() {
if (price != null && quantity != null) {
this.subtotal = price.multiply(BigDecimal.valueOf(quantity));
}
}
public void increaseQuantity() {
this.quantity++;
calculateSubtotal();
}
public void decreaseQuantity() {
if (this.quantity > 1) {
this.quantity--;
calculateSubtotal();
}
}
public String getProductName() {
return product != null ? product.getName() : "Unknown Product";
}
public String getProductImageUrl() {
return product != null ? product.getImageUrl() : null;
}
@Override
public String toString() {
return "CartItem{" +
"id=" + id +
", product=" + (product != null ? product.getName() : "null") +
", quantity=" + quantity +
", price=" + price +
", subtotal=" + subtotal +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CartItem cartItem = (CartItem) o;
if (id != null ? !id.equals(cartItem.id) : cartItem.id != null) return false;
return product != null ? product.equals(cartItem.product) : cartItem.product == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (product != null ? product.hashCode() : 0);
return result;
}
}