feature: price history and model

This commit is contained in:
Emelyanov535 2024-10-03 17:26:35 +04:00
parent 30ca5acc34
commit ffe6920b29
5 changed files with 126 additions and 6 deletions

View File

@ -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;
}

View File

@ -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<PriceHistoryEntity> 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();
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd">
<changeSet id="20240926_create_price_history_table.xml" author="Emelyanov535">
<preConditions>
<not>
<tableExists tableName="price_history" />
</not>
</preConditions>
<createTable tableName="price_history">
<column name="id" type="bigint" autoIncrement="true" remarks="Идентификатор">
<constraints primaryKey="true" />
</column>
<column name="product_id" type="bigint" remarks="ID товара">
<constraints nullable="false" />
</column>
<column name="price" type="numeric(10,2)" remarks="Цена товара">
<constraints nullable="false" />
</column>
<column name="date" type="timestamptz" remarks="Дата сохранения">
<constraints nullable="false" />
</column>
</createTable>
<addForeignKeyConstraint baseTableName="price_history"
baseColumnNames="product_id"
constraintName="fk_product_price_history"
referencedTableName="product"
referencedColumnNames="id"
onDelete="CASCADE"/>
</changeSet>
</databaseChangeLog>

View File

@ -9,9 +9,6 @@
<not>
<tableExists tableName="product" />
</not>
<not>
<tableExists tableName="product_price_history" />
</not>
</preConditions>
<createTable tableName="product">
<column name="id" type="bigint" autoIncrement="true" remarks="Идентификатор товара">
@ -33,8 +30,5 @@
<constraints nullable="false" />
</column>
</createTable>
<sql>
</sql>
</changeSet>
</databaseChangeLog>

View File

@ -2,3 +2,6 @@ databaseChangeLog:
- include:
file: 20240926_create_product_table.xml
relativeToChangelogFile: true
- include:
file: 20240926_create_price_history_table.xml
relativeToChangelogFile: true