|
| 1 | +package com.amigoscode.product; |
| 2 | + |
| 3 | +import jakarta.persistence.*; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.time.Instant; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +@Entity |
| 11 | +@Table( |
| 12 | + name = "product" |
| 13 | +) |
| 14 | +public class Product { |
| 15 | + |
| 16 | + @Id |
| 17 | + private UUID id; |
| 18 | + |
| 19 | + @Column(nullable = false, length = 50) |
| 20 | + private String name; |
| 21 | + |
| 22 | + private String description; |
| 23 | + |
| 24 | + @Column(nullable = false, precision = 10, scale = 2) |
| 25 | + private BigDecimal price; |
| 26 | + |
| 27 | + @Column(length = 200) |
| 28 | + private String imageUrl; |
| 29 | + |
| 30 | + @Column(nullable = false) |
| 31 | + private Integer stockLevel; |
| 32 | + |
| 33 | + @Column(nullable = false, updatable = false) |
| 34 | + private Instant createdAt; |
| 35 | + |
| 36 | + private Instant updatedAt; |
| 37 | + |
| 38 | + private Instant deletedAt; |
| 39 | + |
| 40 | + @PrePersist |
| 41 | + public void prePersist(){ |
| 42 | + if(this.id == null) { |
| 43 | + this.id = UUID.randomUUID(); |
| 44 | + } |
| 45 | + this.createdAt = Instant.now(); |
| 46 | + this.updatedAt = Instant.now(); |
| 47 | + } |
| 48 | + |
| 49 | + @PreUpdate |
| 50 | + public void preUpdate(){ |
| 51 | + this.updatedAt = Instant.now(); |
| 52 | + } |
| 53 | + |
| 54 | + public UUID getId() { |
| 55 | + return id; |
| 56 | + } |
| 57 | + |
| 58 | + public void setId(UUID id) { |
| 59 | + this.id = id; |
| 60 | + } |
| 61 | + |
| 62 | + public String getName() { |
| 63 | + return name; |
| 64 | + } |
| 65 | + |
| 66 | + public void setName(String name) { |
| 67 | + this.name = name; |
| 68 | + } |
| 69 | + |
| 70 | + public String getDescription() { |
| 71 | + return description; |
| 72 | + } |
| 73 | + |
| 74 | + public void setDescription(String description) { |
| 75 | + this.description = description; |
| 76 | + } |
| 77 | + |
| 78 | + public BigDecimal getPrice() { |
| 79 | + return price; |
| 80 | + } |
| 81 | + |
| 82 | + public void setPrice(BigDecimal price) { |
| 83 | + this.price = price; |
| 84 | + } |
| 85 | + |
| 86 | + public String getImageUrl() { |
| 87 | + return imageUrl; |
| 88 | + } |
| 89 | + |
| 90 | + public void setImageUrl(String imageUrl) { |
| 91 | + this.imageUrl = imageUrl; |
| 92 | + } |
| 93 | + |
| 94 | + public int getStockLevel() { |
| 95 | + return stockLevel; |
| 96 | + } |
| 97 | + |
| 98 | + public void setStockLevel(int stockLevel) { |
| 99 | + this.stockLevel = stockLevel; |
| 100 | + } |
| 101 | + |
| 102 | + public Instant getCreatedAt() { |
| 103 | + return createdAt; |
| 104 | + } |
| 105 | + |
| 106 | + public void setCreatedAt(Instant createdAt) { |
| 107 | + this.createdAt = createdAt; |
| 108 | + } |
| 109 | + |
| 110 | + public Instant getUpdatedAt() { |
| 111 | + return updatedAt; |
| 112 | + } |
| 113 | + |
| 114 | + public void setUpdatedAt(Instant updatedAt) { |
| 115 | + this.updatedAt = updatedAt; |
| 116 | + } |
| 117 | + |
| 118 | + public Instant getDeletedAt() { |
| 119 | + return deletedAt; |
| 120 | + } |
| 121 | + |
| 122 | + public void setDeletedAt(Instant deletedAt) { |
| 123 | + this.deletedAt = deletedAt; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean equals(Object o) { |
| 128 | + if (o == null || getClass() != o.getClass()) return false; |
| 129 | + Product product = (Product) o; |
| 130 | + return stockLevel == product.stockLevel && Objects.equals(id, product.id) && Objects.equals(name, product.name) && Objects.equals(description, product.description) && Objects.equals(price, product.price) && Objects.equals(imageUrl, product.imageUrl) && Objects.equals(createdAt, product.createdAt) && Objects.equals(updatedAt, product.updatedAt) && Objects.equals(deletedAt, product.deletedAt); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public int hashCode() { |
| 135 | + return Objects.hash(id, name, description, price, imageUrl, stockLevel, createdAt, updatedAt, deletedAt); |
| 136 | + } |
| 137 | +} |
0 commit comments