From ffe6920b2927e1791e984e417f5b7d87c2ef063f Mon Sep 17 00:00:00 2001 From: Emelyanov535 Date: Thu, 3 Oct 2024 17:26:35 +0400 Subject: [PATCH] feature: price history and model --- .../entity/PriceHistoryEntity.java | 32 +++++++++++ .../persistance/entity/ProductEntity.java | 56 +++++++++++++++++++ .../20240926_create_price_history_table.xml | 35 ++++++++++++ .../20240926_create_product_table.xml | 6 -- .../db/changelog/20240926/master.yml | 3 + 5 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/PriceHistoryEntity.java create mode 100644 parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/ProductEntity.java create mode 100644 parsing-service/src/main/resources/db/changelog/20240926/20240926_create_price_history_table.xml diff --git a/parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/PriceHistoryEntity.java b/parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/PriceHistoryEntity.java new file mode 100644 index 0000000..530e66e --- /dev/null +++ b/parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/PriceHistoryEntity.java @@ -0,0 +1,32 @@ +package ru.pricepulse.parsingservice.persistance.entity; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import java.math.BigDecimal; +import java.time.OffsetDateTime; + +@Getter +@Setter +@Entity +@Table(name = "price_history") +public class PriceHistoryEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @OnDelete(action = OnDeleteAction.CASCADE) + @JoinColumn(name = "product_id", nullable = false) + private ProductEntity product; + + @Column(name = "price", nullable = false, precision = 10, scale = 2) + private BigDecimal price; + + @Column(name = "date", nullable = false) + private OffsetDateTime date; +} \ No newline at end of file diff --git a/parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/ProductEntity.java b/parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/ProductEntity.java new file mode 100644 index 0000000..71d25e1 --- /dev/null +++ b/parsing-service/src/main/java/ru/pricepulse/parsingservice/persistance/entity/ProductEntity.java @@ -0,0 +1,56 @@ +package ru.pricepulse.parsingservice.persistance.entity; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.proxy.HibernateProxy; + +import java.time.OffsetDateTime; +import java.util.LinkedHashSet; +import java.util.Objects; +import java.util.Set; + +@Getter +@Setter +@Entity +@Table(name = "product") +public class ProductEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + private Long id; + + @Column(name = "marketplace", nullable = false, length = Integer.MAX_VALUE) + private String marketplace; + + @Column(name = "category", nullable = false, length = Integer.MAX_VALUE) + private String category; + + @Column(name = "brand", nullable = false, length = Integer.MAX_VALUE) + private String brand; + + @Column(name = "product_name", nullable = false, length = Integer.MAX_VALUE) + private String productName; + + @Column(name = "created_at", nullable = false) + private OffsetDateTime createdAt; + + @OneToMany(mappedBy = "product") + private Set priceHistories = new LinkedHashSet<>(); + + @Override + public final boolean equals(Object o) { + if (this == o) return true; + if (o == null) return false; + Class oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass(); + Class thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass(); + if (thisEffectiveClass != oEffectiveClass) return false; + ProductEntity that = (ProductEntity) o; + return getId() != null && Objects.equals(getId(), that.getId()); + } + + @Override + public final int hashCode() { + return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode(); + } +} \ No newline at end of file diff --git a/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_price_history_table.xml b/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_price_history_table.xml new file mode 100644 index 0000000..b193183 --- /dev/null +++ b/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_price_history_table.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_product_table.xml b/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_product_table.xml index 8aa720a..cc26229 100644 --- a/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_product_table.xml +++ b/parsing-service/src/main/resources/db/changelog/20240926/20240926_create_product_table.xml @@ -9,9 +9,6 @@ - - - @@ -33,8 +30,5 @@ - - - \ No newline at end of file diff --git a/parsing-service/src/main/resources/db/changelog/20240926/master.yml b/parsing-service/src/main/resources/db/changelog/20240926/master.yml index 733e940..864ea02 100644 --- a/parsing-service/src/main/resources/db/changelog/20240926/master.yml +++ b/parsing-service/src/main/resources/db/changelog/20240926/master.yml @@ -2,3 +2,6 @@ databaseChangeLog: - include: file: 20240926_create_product_table.xml relativeToChangelogFile: true + - include: + file: 20240926_create_price_history_table.xml + relativeToChangelogFile: true