Skip to content

Commit

Permalink
nashtech-garage#981 update promotion database and entities (nashtech-…
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvanhadncntt authored Sep 5, 2024
1 parent 49ab7b8 commit a4fc129
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 9 deletions.
23 changes: 22 additions & 1 deletion promotion/src/main/java/com/yas/promotion/model/Promotion.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.yas.promotion.model;

import com.yas.promotion.model.enumeration.ApplyTo;
import com.yas.promotion.model.enumeration.DiscountType;
import com.yas.promotion.model.enumeration.UsageType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.ZonedDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.ZonedDateTime;

@Entity
@Table(name = "promotion")
@Getter
Expand All @@ -35,10 +41,25 @@ public class Promotion extends AbstractAuditEntity {

private String couponCode;

@Enumerated(EnumType.STRING)
private DiscountType discountType;

@Enumerated(EnumType.STRING)
private UsageType usageType;

@Enumerated(EnumType.STRING)
private ApplyTo applyTo;

private int usageLimit;

private int usageCount;

private Long discountPercentage;

private Long discountAmount;

private Long minimumOrderPurchaseAmount;

private Boolean isActive;

private ZonedDateTime startDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.yas.promotion.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "promotion_apply")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Builder
public class PromotionApply extends AbstractAuditEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "promotion_id")
private Promotion promotion;

private Long productId;

private Long categoryId;

private Long brandId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.yas.promotion.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "promotion_usage")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Builder
public class PromotionUsage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "promotion_id")
private Promotion promotion;

private Long productId;

private String userId;

private Long orderId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yas.promotion.model.enumeration;

public enum ApplyTo {
PRODUCT, CATEGORY, BRAND
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yas.promotion.model.enumeration;

public enum DiscountType {
PERCENTAGE, FIXED;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yas.promotion.model.enumeration;

public enum UsageType {
LIMITED, UNLIMITED
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import com.yas.promotion.viewmodel.PromotionDetailVm;
import com.yas.promotion.viewmodel.PromotionListVm;
import com.yas.promotion.viewmodel.PromotionPostVm;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -33,9 +34,13 @@ public PromotionDetailVm createPromotion(PromotionPostVm promotionPostVm) {
.slug(promotionPostVm.slug())
.description(promotionPostVm.description())
.couponCode(promotionPostVm.couponCode())
.applyTo(promotionPostVm.applyTo())
.usageType(promotionPostVm.usageType())
.usageLimit(promotionPostVm.usageLimit())
.discountType(promotionPostVm.discountType())
.discountPercentage(promotionPostVm.discountPercentage())
.discountAmount(promotionPostVm.discountAmount())
.isActive(true)
.isActive(promotionPostVm.isActive())
.startDate(promotionPostVm.startDate())
.endDate(promotionPostVm.endDate())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.yas.promotion.viewmodel;

import com.yas.promotion.model.Promotion;
import java.time.ZonedDateTime;
import com.yas.promotion.model.enumeration.ApplyTo;
import com.yas.promotion.model.enumeration.DiscountType;
import com.yas.promotion.model.enumeration.UsageType;
import lombok.Builder;

import java.time.ZonedDateTime;

@Builder
public record PromotionDetailVm(Long id,
String name,
String slug,
String description,
String couponCode,
int usageLimit,
int usageCount,
DiscountType discountType,
ApplyTo applyTo,
UsageType usageType,
Long discountPercentage,
Long discountAmount,
Boolean isActive,
Expand All @@ -21,6 +30,12 @@ public static PromotionDetailVm fromModel(Promotion promotion) {
.id(promotion.getId())
.name(promotion.getName())
.slug(promotion.getSlug())
.couponCode(promotion.getCouponCode())
.usageLimit(promotion.getUsageLimit())
.usageCount(promotion.getUsageCount())
.discountType(promotion.getDiscountType())
.applyTo(promotion.getApplyTo())
.usageType(promotion.getUsageType())
.description(promotion.getDescription())
.discountPercentage(promotion.getDiscountPercentage())
.discountAmount(promotion.getDiscountAmount())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.yas.promotion.viewmodel;

import java.util.List;
import lombok.Builder;

import java.util.List;

@Builder
public record PromotionListVm(
List<PromotionDetailVm> promotionDetailVmList,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.yas.promotion.viewmodel;

import com.yas.promotion.model.enumeration.ApplyTo;
import com.yas.promotion.model.enumeration.DiscountType;
import com.yas.promotion.model.enumeration.UsageType;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import lombok.Builder;

import java.time.ZonedDateTime;

@Builder
public record PromotionPostVm(@Size(min = 1, max = 450) String name,
@NotBlank String slug,
String description,
String couponCode,
int usageLimit,
DiscountType discountType,
ApplyTo applyTo,
UsageType usageType,
@Min(0) @Max(100) Long discountPercentage,
@Min(0) Long discountAmount,
Boolean isActive,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.yas.promotion.viewmodel;

import com.yas.promotion.model.Promotion;
import java.time.ZonedDateTime;
import lombok.Builder;

import java.time.ZonedDateTime;

@Builder
public record PromotionVm(Long id,
String name,
Expand Down
37 changes: 37 additions & 0 deletions promotion/src/main/resources/db/changelog/ddl/changelog-0002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--liquibase formatted sql

--changeset nguyenvanhadncntt:issue-981-1
ALTER TABLE promotion ADD COLUMN discount_type VARCHAR(25) NOT NULL;
ALTER TABLE promotion ADD COLUMN usage_limit INT;
ALTER TABLE promotion ADD COLUMN usage_count INT DEFAULT 0 NOT NULL;
ALTER TABLE promotion ADD COLUMN usage_type VARCHAR(25) DEFAULT 'UNLIMITED' NOT NULL ;
ALTER TABLE promotion ADD COLUMN apply_to VARCHAR(25) NOT NULL;
ALTER TABLE promotion ADD COLUMN minimum_order_purchase_amount bigint;

--changeset nguyenvanhadncntt:issue-981-2
CREATE TABLE promotion_usage (
id SERIAL PRIMARY KEY,
promotion_id bigint NOT NULL,
user_id varchar(255) NOT NULL,
order_id bigint NOT NULL ,
product_id bigint NOT NULL,
created_by varchar(255),
created_on timestamp with time zone,
last_modified_by varchar(255),
last_modified_on timestamp with time zone,
foreign key (promotion_id) references promotion(id)
);

--changeset nguyenvanhadncntt:issue-981-3
CREATE TABLE promotion_apply (
id SERIAL PRIMARY KEY,
promotion_id bigint NOT NULL,
brand_id bigint,
category_id bigint,
product_id bigint,
created_by varchar(255),
created_on timestamp with time zone,
last_modified_by varchar(255),
last_modified_on timestamp with time zone,
foreign key (promotion_id) references promotion(id)
);

0 comments on commit a4fc129

Please sign in to comment.