From fd3f49255a560feeb3b33fd73e5a99d212ec724e Mon Sep 17 00:00:00 2001 From: Harkamal Randhawa Date: Mon, 6 Apr 2026 20:30:11 -0600 Subject: [PATCH] add coupon cart cartItem entities --- .../java/com/petshop/backend/entity/Cart.java | 155 +++++++++++++++++ .../com/petshop/backend/entity/CartItem.java | 121 +++++++++++++ .../com/petshop/backend/entity/Coupon.java | 162 ++++++++++++++++++ .../repository/CartItemRepository.java | 13 ++ .../backend/repository/CartRepository.java | 16 ++ .../backend/repository/CouponRepository.java | 13 ++ 6 files changed, 480 insertions(+) create mode 100644 backend/src/main/java/com/petshop/backend/entity/Cart.java create mode 100644 backend/src/main/java/com/petshop/backend/entity/CartItem.java create mode 100644 backend/src/main/java/com/petshop/backend/entity/Coupon.java create mode 100644 backend/src/main/java/com/petshop/backend/repository/CartItemRepository.java create mode 100644 backend/src/main/java/com/petshop/backend/repository/CartRepository.java create mode 100644 backend/src/main/java/com/petshop/backend/repository/CouponRepository.java diff --git a/backend/src/main/java/com/petshop/backend/entity/Cart.java b/backend/src/main/java/com/petshop/backend/entity/Cart.java new file mode 100644 index 00000000..ba0566f8 --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/entity/Cart.java @@ -0,0 +1,155 @@ +package com.petshop.backend.entity; + +import jakarta.persistence.*; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.Objects; + +@Entity +@Table(name = "cart") +public class Cart { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long cartId; + + @ManyToOne + @JoinColumn(name = "userId", nullable = false) + private User user; + + @ManyToOne + @JoinColumn(name = "storeId") + private StoreLocation store; + + @ManyToOne + @JoinColumn(name = "couponId") + private Coupon coupon; + + @Column(nullable = false, length = 20) + private String cartStatus = "ACTIVE"; + + @Column(nullable = false, precision = 10, scale = 2) + private BigDecimal subtotalAmount = BigDecimal.ZERO; + + @Column(nullable = false, precision = 10, scale = 2) + private BigDecimal discountAmount = BigDecimal.ZERO; + + @Column(nullable = false, precision = 10, scale = 2) + private BigDecimal totalAmount = BigDecimal.ZERO; + + @CreationTimestamp + @Column(name = "created_at", updatable = false) + private LocalDateTime createdAt; + + @UpdateTimestamp + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + public Cart() { + } + + public Long getCartId() { + return cartId; + } + + public void setCartId(Long cartId) { + this.cartId = cartId; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public StoreLocation getStore() { + return store; + } + + public void setStore(StoreLocation store) { + this.store = store; + } + + public Coupon getCoupon() { + return coupon; + } + + public void setCoupon(Coupon coupon) { + this.coupon = coupon; + } + + public String getCartStatus() { + return cartStatus; + } + + public void setCartStatus(String cartStatus) { + this.cartStatus = cartStatus; + } + + public BigDecimal getSubtotalAmount() { + return subtotalAmount; + } + + public void setSubtotalAmount(BigDecimal subtotalAmount) { + this.subtotalAmount = subtotalAmount; + } + + public BigDecimal getDiscountAmount() { + return discountAmount; + } + + public void setDiscountAmount(BigDecimal discountAmount) { + this.discountAmount = discountAmount; + } + + public BigDecimal getTotalAmount() { + return totalAmount; + } + + public void setTotalAmount(BigDecimal totalAmount) { + this.totalAmount = totalAmount; + } + + 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; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Cart cart = (Cart) o; + return Objects.equals(cartId, cart.cartId); + } + + @Override + public int hashCode() { + return Objects.hash(cartId); + } + + @Override + public String toString() { + return "Cart{" + + "cartId=" + cartId + + ", cartStatus='" + cartStatus + '\'' + + ", totalAmount=" + totalAmount + + '}'; + } +} diff --git a/backend/src/main/java/com/petshop/backend/entity/CartItem.java b/backend/src/main/java/com/petshop/backend/entity/CartItem.java new file mode 100644 index 00000000..1ad4c144 --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/entity/CartItem.java @@ -0,0 +1,121 @@ +package com.petshop.backend.entity; + +import jakarta.persistence.*; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.Objects; + +@Entity +@Table(name = "cart_item") +public class CartItem { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long cartItemId; + + @ManyToOne + @JoinColumn(name = "cartId", nullable = false) + private Cart cart; + + @ManyToOne + @JoinColumn(name = "prodId", nullable = false) + private Product product; + + @Column(nullable = false) + private Integer quantity; + + @Column(nullable = false, precision = 10, scale = 2) + private BigDecimal unitPrice; + + @CreationTimestamp + @Column(name = "created_at", updatable = false) + private LocalDateTime createdAt; + + @UpdateTimestamp + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + public CartItem() { + } + + public Long getCartItemId() { + return cartItemId; + } + + public void setCartItemId(Long cartItemId) { + this.cartItemId = cartItemId; + } + + 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; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public BigDecimal getUnitPrice() { + return unitPrice; + } + + public void setUnitPrice(BigDecimal unitPrice) { + this.unitPrice = unitPrice; + } + + 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; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CartItem cartItem = (CartItem) o; + return Objects.equals(cartItemId, cartItem.cartItemId); + } + + @Override + public int hashCode() { + return Objects.hash(cartItemId); + } + + @Override + public String toString() { + return "CartItem{" + + "cartItemId=" + cartItemId + + ", quantity=" + quantity + + ", unitPrice=" + unitPrice + + '}'; + } +} diff --git a/backend/src/main/java/com/petshop/backend/entity/Coupon.java b/backend/src/main/java/com/petshop/backend/entity/Coupon.java new file mode 100644 index 00000000..ba234f12 --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/entity/Coupon.java @@ -0,0 +1,162 @@ +package com.petshop.backend.entity; + +import jakarta.persistence.*; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.Objects; + +@Entity +@Table(name = "coupon") +public class Coupon { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long couponId; + + @Column(nullable = false, length = 50, unique = true) + private String couponCode; + + @Column(nullable = false, length = 20) + private String discountType; + + @Column(nullable = false, precision = 10, scale = 2) + private BigDecimal discountValue; + + @Column(precision = 10, scale = 2) + private BigDecimal minOrderAmount; + + @Column(nullable = false) + private Boolean active = true; + + private LocalDateTime startsAt; + + private LocalDateTime endsAt; + + private Integer usageLimit; + + @CreationTimestamp + @Column(name = "created_at", updatable = false) + private LocalDateTime createdAt; + + @UpdateTimestamp + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + public Coupon() { + } + + public Long getCouponId() { + return couponId; + } + + public void setCouponId(Long couponId) { + this.couponId = couponId; + } + + public String getCouponCode() { + return couponCode; + } + + public void setCouponCode(String couponCode) { + this.couponCode = couponCode; + } + + public String getDiscountType() { + return discountType; + } + + public void setDiscountType(String discountType) { + this.discountType = discountType; + } + + public BigDecimal getDiscountValue() { + return discountValue; + } + + public void setDiscountValue(BigDecimal discountValue) { + this.discountValue = discountValue; + } + + public BigDecimal getMinOrderAmount() { + return minOrderAmount; + } + + public void setMinOrderAmount(BigDecimal minOrderAmount) { + this.minOrderAmount = minOrderAmount; + } + + public Boolean getActive() { + return active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public LocalDateTime getStartsAt() { + return startsAt; + } + + public void setStartsAt(LocalDateTime startsAt) { + this.startsAt = startsAt; + } + + public LocalDateTime getEndsAt() { + return endsAt; + } + + public void setEndsAt(LocalDateTime endsAt) { + this.endsAt = endsAt; + } + + public Integer getUsageLimit() { + return usageLimit; + } + + public void setUsageLimit(Integer usageLimit) { + this.usageLimit = usageLimit; + } + + 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; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Coupon coupon = (Coupon) o; + return Objects.equals(couponId, coupon.couponId); + } + + @Override + public int hashCode() { + return Objects.hash(couponId); + } + + @Override + public String toString() { + return "Coupon{" + + "couponId=" + couponId + + ", couponCode='" + couponCode + '\'' + + ", discountType='" + discountType + '\'' + + ", discountValue=" + discountValue + + ", active=" + active + + '}'; + } +} diff --git a/backend/src/main/java/com/petshop/backend/repository/CartItemRepository.java b/backend/src/main/java/com/petshop/backend/repository/CartItemRepository.java new file mode 100644 index 00000000..ffcdc166 --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/repository/CartItemRepository.java @@ -0,0 +1,13 @@ +package com.petshop.backend.repository; + +import com.petshop.backend.entity.CartItem; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface CartItemRepository extends JpaRepository { + + List findByCartCartId(Long cartId); +} diff --git a/backend/src/main/java/com/petshop/backend/repository/CartRepository.java b/backend/src/main/java/com/petshop/backend/repository/CartRepository.java new file mode 100644 index 00000000..3f6a974a --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/repository/CartRepository.java @@ -0,0 +1,16 @@ +package com.petshop.backend.repository; + +import com.petshop.backend.entity.Cart; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface CartRepository extends JpaRepository { + + List findByUserId(Long userId); + + Optional findByUserIdAndCartStatus(Long userId, String cartStatus); +} diff --git a/backend/src/main/java/com/petshop/backend/repository/CouponRepository.java b/backend/src/main/java/com/petshop/backend/repository/CouponRepository.java new file mode 100644 index 00000000..64870204 --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/repository/CouponRepository.java @@ -0,0 +1,13 @@ +package com.petshop.backend.repository; + +import com.petshop.backend.entity.Coupon; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface CouponRepository extends JpaRepository { + + Optional findByCouponCode(String couponCode); +}