diff --git a/src/main/java/com/champions/carsharingservice/model/Car.java b/src/main/java/com/champions/carsharingservice/model/Car.java new file mode 100644 index 0000000..3a74198 --- /dev/null +++ b/src/main/java/com/champions/carsharingservice/model/Car.java @@ -0,0 +1,64 @@ +package com.champions.carsharingservice.model; + +import jakarta.persistence.CascadeType; +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.OneToMany; +import jakarta.persistence.Table; +import java.math.BigDecimal; +import java.util.HashSet; +import java.util.Set; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; + +@Entity +@Getter +@Setter +@EqualsAndHashCode(exclude = {"rentals"}) +@ToString(exclude = {"rentals"}) +@SQLDelete(sql = "UPDATE cars SET is_deleted = true WHERE id = ?") +@Where(clause = "is_deleted=false") +@Table(name = "cars") +public class Car { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String model; + + @Column(nullable = false) + private String brand; + + @Enumerated(EnumType.STRING) + @Column(nullable = false, unique = true) + private CarType type; + + @Column(nullable = false) + private Integer inventory; + + @Column(nullable = false, name = "daily_fee") + private BigDecimal dailyFee; + + @OneToMany(mappedBy = "car", orphanRemoval = true, cascade = CascadeType.REMOVE) + private Set rentals = new HashSet<>(); + + @Column(nullable = false, name = "is_deleted") + private boolean isDeleted = false; + + public enum CarType { + SEDAN, + SUV, + HATCHBACK, + UNIVERSAL + } +} diff --git a/src/main/java/com/champions/carsharingservice/model/Rental.java b/src/main/java/com/champions/carsharingservice/model/Rental.java new file mode 100644 index 0000000..453c964 --- /dev/null +++ b/src/main/java/com/champions/carsharingservice/model/Rental.java @@ -0,0 +1,21 @@ +package com.champions.carsharingservice.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Entity +public class Rental { + @Id + private Long id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "car_id", nullable = false) + private Car car; +}