[d24f17c] | 1 | package com.example.rezevirajmasa.demo.model;
|
---|
| 2 |
|
---|
| 3 | import com.fasterxml.jackson.annotation.JsonBackReference;
|
---|
[deea3c4] | 4 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
---|
| 5 | import com.fasterxml.jackson.annotation.JsonManagedReference;
|
---|
[d24f17c] | 6 | import jakarta.persistence.*;
|
---|
[deea3c4] | 7 | import lombok.Getter;
|
---|
| 8 | import lombok.Setter;
|
---|
[d24f17c] | 9 | import org.springframework.web.bind.annotation.ModelAttribute;
|
---|
| 10 |
|
---|
| 11 | import java.time.LocalDate;
|
---|
| 12 | import java.time.LocalDateTime;
|
---|
| 13 | import java.time.LocalTime;
|
---|
| 14 | import java.util.ArrayList;
|
---|
| 15 |
|
---|
| 16 | import java.util.Iterator;
|
---|
| 17 | import java.util.List;
|
---|
| 18 | import java.util.stream.Collectors;
|
---|
| 19 |
|
---|
| 20 | @Entity
|
---|
[deea3c4] | 21 | @Getter
|
---|
| 22 | @Setter
|
---|
[d24f17c] | 23 | @Table(name = "tables")
|
---|
| 24 | public class TableEntity {
|
---|
| 25 | @Id
|
---|
| 26 | @GeneratedValue(strategy = GenerationType.IDENTITY)
|
---|
| 27 | @Column(name = "TableID")
|
---|
| 28 | private Long id;
|
---|
| 29 |
|
---|
| 30 | @JsonBackReference
|
---|
[2518b3a] | 31 | @ManyToOne(fetch = FetchType.LAZY)
|
---|
[d24f17c] | 32 | @JoinColumn(name = "RestaurantID")
|
---|
| 33 | private Restaurant restaurant;
|
---|
| 34 |
|
---|
| 35 | @Column(name = "Capacity")
|
---|
| 36 | private int capacity;
|
---|
| 37 |
|
---|
[b67dfd3] | 38 | @Column(name = "tableLocation")
|
---|
| 39 | private String tableLocation;
|
---|
[d24f17c] | 40 |
|
---|
| 41 | @Column(name = "IsSmokingArea")
|
---|
| 42 | private boolean isSmokingArea;
|
---|
| 43 |
|
---|
| 44 | @Column(name = "Description", length = 1000)
|
---|
| 45 | private String description;
|
---|
| 46 |
|
---|
| 47 | @Column(name = "reservation_duration_hours", nullable = true)
|
---|
| 48 | private int reservationDurationHours = 2;
|
---|
| 49 |
|
---|
[deea3c4] | 50 | @OneToMany(mappedBy = "table", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
---|
| 51 | @JsonIgnore
|
---|
[142c0f8] | 52 | private List<Reservation> reservations = new ArrayList<>();
|
---|
[d24f17c] | 53 |
|
---|
[deea3c4] | 54 | public void cleanUnusedTimeSlots(LocalDate threeDaysAgo) {
|
---|
| 55 | reservations.removeIf(reservation -> reservation.getReservationDateTime().toLocalDate().isBefore(threeDaysAgo));
|
---|
| 56 | }
|
---|
[d24f17c] | 57 |
|
---|
[deea3c4] | 58 | public boolean isAvailable(LocalDateTime desiredTimeSlot, int partySize) {
|
---|
| 59 | if (this.capacity < partySize) {
|
---|
| 60 | return false;
|
---|
[d24f17c] | 61 | }
|
---|
| 62 |
|
---|
[deea3c4] | 63 | for (Reservation reservation : reservations) {
|
---|
| 64 | LocalDateTime startTime = reservation.getReservationDateTime();
|
---|
| 65 | LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
|
---|
[d24f17c] | 66 |
|
---|
[deea3c4] | 67 | if ((desiredTimeSlot.isEqual(startTime) || desiredTimeSlot.isAfter(startTime))
|
---|
[d24f17c] | 68 | && desiredTimeSlot.isBefore(endTime)) {
|
---|
[deea3c4] | 69 | return false;
|
---|
[d24f17c] | 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[deea3c4] | 73 | return true;
|
---|
[d24f17c] | 74 | }
|
---|
| 75 |
|
---|
[deea3c4] | 76 | public void addReservation(Reservation reservation) {
|
---|
| 77 | LocalDateTime startTime = reservation.getReservationDateTime();
|
---|
| 78 | LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
|
---|
| 79 | for (LocalDateTime timeSlot = startTime; timeSlot.isBefore(endTime); timeSlot = timeSlot.plusMinutes(15)) {
|
---|
[d24f17c] | 80 |
|
---|
| 81 | }
|
---|
[deea3c4] | 82 | this.reservations.add(reservation);
|
---|
[d24f17c] | 83 | }
|
---|
| 84 |
|
---|
[deea3c4] | 85 | public void removeReservation(Reservation reservation) {
|
---|
| 86 | LocalDateTime startTime = reservation.getReservationDateTime();
|
---|
| 87 | LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
|
---|
| 88 | for (LocalDateTime timeSlot = startTime; timeSlot.isBefore(endTime); timeSlot = timeSlot.plusMinutes(15)) {
|
---|
[d24f17c] | 89 |
|
---|
[deea3c4] | 90 | }
|
---|
| 91 | this.reservations.remove(reservation);
|
---|
| 92 | }
|
---|
[d24f17c] | 93 |
|
---|
[deea3c4] | 94 | public TableEntity(Restaurant restaurant, int capacity, String location, boolean isSmokingArea, String description, int reservationDurationHours) {
|
---|
[d24f17c] | 95 | this.restaurant = restaurant;
|
---|
| 96 | this.capacity = capacity;
|
---|
[b67dfd3] | 97 | this.tableLocation = location;
|
---|
[d24f17c] | 98 | this.isSmokingArea = isSmokingArea;
|
---|
| 99 | this.description = description;
|
---|
| 100 | this.reservationDurationHours = reservationDurationHours;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public TableEntity() {
|
---|
| 104 | }
|
---|
| 105 | }
|
---|