diff --git a/src/main/java/com/zufar/onlinestore/reservation/dto/ReservationDto.java b/src/main/java/com/zufar/onlinestore/reservation/dto/ReservationDto.java index b0967b9e2..ed23454a3 100644 --- a/src/main/java/com/zufar/onlinestore/reservation/dto/ReservationDto.java +++ b/src/main/java/com/zufar/onlinestore/reservation/dto/ReservationDto.java @@ -5,18 +5,24 @@ import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Past; -import java.time.LocalDateTime; +import java.time.Instant; public record ReservationDto( - int reservationId, - @NotBlank(message = "Product ID is mandatory") - int productId, - @NotNull(message = "Reserved quantity is mandatory") - int reservedQuantity, + @NotNull(message = "ReservationId is the mandatory attribute") + Integer reservationId, + + @NotBlank(message = "ProductId is the mandatory attribute") + String productId, + + @NotNull(message = "ReservedQuantity is the mandatory attribute") + Integer reservedQuantity, + + @NotNull(message = "CreatedAt is the mandatory attribute") @Past - LocalDateTime createdAt, - @NotBlank(message = "Status is mandatory") + Instant createdAt, + + @NotBlank(message = "Status is the mandatory attribute") String status ) { -} \ No newline at end of file +} diff --git a/src/main/java/com/zufar/onlinestore/reservation/entity/Reservation.java b/src/main/java/com/zufar/onlinestore/reservation/entity/Reservation.java index 70cf3b091..c2e80c58f 100644 --- a/src/main/java/com/zufar/onlinestore/reservation/entity/Reservation.java +++ b/src/main/java/com/zufar/onlinestore/reservation/entity/Reservation.java @@ -1,92 +1,63 @@ package com.zufar.onlinestore.reservation.entity; import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.Objects; +import java.util.UUID; @Entity @EntityListeners(AuditingEntityListener.class) @Table(name = "reservation") +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor public class Reservation { @Id - @GeneratedValue(strategy = GenerationType.UUID) - @Column(name = "reservation_id") - private String reservationId; - @Column(name = "product_id", nullable = false) - private String productId; - @Column(name = "reserved_quantity", nullable = false) - private int reservedQuantity; - @CreatedDate - @Column(name = "created_at", nullable = false, updatable = false) - private LocalDateTime createdAt; - @Column(name = "status", nullable = false) - private String status; - - public Reservation() { - } - - public Reservation(String productId, int reservedQuantity, LocalDateTime createdAt, String status) { - this.productId = productId; - this.reservedQuantity = reservedQuantity; - this.createdAt = createdAt; - this.status = status; - } - - public String getReservationId() { - return reservationId; - } - - public void setReservationId(String reservationId) { - this.reservationId = reservationId; - } - - public String getProductId() { - return productId; - } + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Integer id; - public void setProductId(String productId) { - this.productId = productId; - } + @Column(name = "reservation_id", nullable = false) + private UUID reservationId; - public int getReservedQuantity() { - return reservedQuantity; - } - - public void setReservedQuantity(int reservedQuantity) { - this.reservedQuantity = reservedQuantity; - } - - public LocalDateTime getCreatedAt() { - return createdAt; - } + @Column(name = "product_id", nullable = false) + private UUID productId; - public void setCreatedAt(LocalDateTime createdAt) { - this.createdAt = createdAt; - } + @Column(name = "reserved_quantity", nullable = false) + private Integer reservedQuantity; - public String getStatus() { - return status; - } + @CreatedDate + @Column(name = "created_at", nullable = false, updatable = false) + private Instant createdAt; - public void setStatus(String status) { - this.status = status; - } + @Enumerated(EnumType.STRING) + @Column(name = "status", nullable = false) + private ReservationStatus status; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Reservation that = (Reservation) o; - return Objects.equals(reservationId, that.reservationId) && Objects.equals(productId, that.productId) && Objects.equals(createdAt, that.createdAt); + return reservationId.equals(that.reservationId) && productId.equals(that.productId); } @Override public int hashCode() { - return Objects.hash(reservationId, productId, createdAt); + return Objects.hash(reservationId, productId); + } + + public enum ReservationStatus { + CREATED, CONFIRMED, CANCELLED } } diff --git a/src/main/resources/db/changelog/version-1.0/22.07.2023.create-reservation-table.sql b/src/main/resources/db/changelog/version-1.0/22.07.2023.create-reservation-table.sql new file mode 100644 index 000000000..03352e51e --- /dev/null +++ b/src/main/resources/db/changelog/version-1.0/22.07.2023.create-reservation-table.sql @@ -0,0 +1,11 @@ +CREATE TYPE RESERVATION_STATUS AS ENUM ('CREATED', 'CONFIRMED', 'CANCELLED'); + +CREATE TABLE IF NOT EXISTS reservation +( + id SERIAL PRIMARY KEY, + reservation_id UUID NOT NULL, + product_id UUID NOT NULL, + reserved_quantity INT NOT NULL CHECK (reserved_quantity > 0), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + status RESERVATION_STATUS NOT NULL +);