Ignore:
Timestamp:
04/28/25 14:21:17 (3 weeks ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
e15e8d9
Parents:
f5b256e
Message:

Big change done fully handle_reservation_update() trigger works

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/rezevirajmasa/demo/model/TableEntity.java

    rf5b256e rdeea3c4  
    22
    33import com.fasterxml.jackson.annotation.JsonBackReference;
     4import com.fasterxml.jackson.annotation.JsonIgnore;
     5import com.fasterxml.jackson.annotation.JsonManagedReference;
    46import jakarta.persistence.*;
     7import lombok.Getter;
     8import lombok.Setter;
    59import org.springframework.web.bind.annotation.ModelAttribute;
    610
     
    1519
    1620@Entity
     21@Getter
     22@Setter
    1723@Table(name = "tables")
    1824public class TableEntity {
     
    3945    private String description;
    4046
    41     @ElementCollection(fetch = FetchType.EAGER)
    42     @CollectionTable(name = "table_time_slots", joinColumns = @JoinColumn(name = "table_id"))
    43     @Column(name = "time_slot", columnDefinition = "timestamp without time zone")
    44     @OrderBy("time_slot ASC")
    45     private List<LocalDateTime> timeSlots = new ArrayList<>();
    46 
    4747    @Column(name = "reservation_duration_hours", nullable = true)
    4848    private int reservationDurationHours = 2;
     49
     50    @OneToMany(mappedBy = "table", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
     51    @JsonIgnore
     52    private List<Reservation> reservations = new ArrayList<>(); // Store reservations, not time slots
     53
    4954    public void cleanUnusedTimeSlots(LocalDate threeDaysAgo) {
    50         // Iterate over the time slots and remove those that are in the past
    51         timeSlots.removeIf(timeSlot -> timeSlot.isBefore(threeDaysAgo.atStartOfDay()));
     55        reservations.removeIf(reservation -> reservation.getReservationDateTime().toLocalDate().isBefore(threeDaysAgo));
    5256    }
    5357
    54     public void addTimeSlot(LocalDateTime timeSlot) {
    55         this.timeSlots.add(timeSlot);
    56     }
    57 
    58 
    59     @Transient
    60     public boolean isAvailable(LocalDateTime desiredTimeSlot) {
    61         if (timeSlots == null || timeSlots.isEmpty()) {
    62             return true;
     58    public boolean isAvailable(LocalDateTime desiredTimeSlot, int partySize) {
     59        if (this.capacity < partySize) {
     60            return false;
    6361        }
    6462
    65         for (LocalDateTime reservedTimeSlot : timeSlots) {
    66             LocalDateTime endTime = reservedTimeSlot.plusHours(reservationDurationHours);
     63        for (Reservation reservation : reservations) {
     64            LocalDateTime startTime = reservation.getReservationDateTime();
     65            LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
    6766
    68             // Check if the desired time slot overlaps with any existing reservation
    69             if ((desiredTimeSlot.equals(reservedTimeSlot) || desiredTimeSlot.isAfter(reservedTimeSlot))
     67            if ((desiredTimeSlot.isEqual(startTime) || desiredTimeSlot.isAfter(startTime))
    7068                    && desiredTimeSlot.isBefore(endTime)) {
    71                 return false; // Table is reserved for the desired time slot
     69                return false;
    7270            }
    7371        }
    7472
    75         return true; // No conflicting reservations, table is available
     73        return true;
    7674    }
    7775
    78     public boolean hasTimeSlot(LocalDateTime dateTime) {
    79         // Check if the given dateTime exists in the list of time slots
    80         return timeSlots.contains(dateTime);
     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)) {
     80
     81        }
     82        this.reservations.add(reservation);
    8183    }
    8284
    83     public List<LocalDateTime> initializeTimeSlots(LocalTime startTime, LocalTime endTime, List<LocalDate> dates, List<LocalDateTime> listOfUsedCheckIns) {
    84         // Check if time slots are already initialized and cover all provided dates
    85         List<LocalDate> generatedDates = timeSlots.stream()
    86                 .map(LocalDateTime::toLocalDate)
    87                 .distinct()
    88                 .toList();
     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)) {
    8989
    90         if (generatedDates.containsAll(dates) && dates.containsAll(generatedDates)) {
    91             System.out.println("Time slots already cover all provided dates.");
    92             return timeSlots;
    9390        }
    94 
    95         // Generate time slots for the remaining dates
    96         for (LocalDate date : dates) {
    97             if (!generatedDates.contains(date)) {
    98                 LocalTime currentTime = startTime;
    99                 while (currentTime.isBefore(LocalTime.of(23, 0))) {
    100                     LocalDateTime currentDateTime = LocalDateTime.of(date, currentTime);
    101                     boolean isUsed = false;
    102                     if (listOfUsedCheckIns != null) {
    103                         for (LocalDateTime checkIns : listOfUsedCheckIns) {
    104                             if (checkIns.isEqual(currentDateTime)) {
    105                                 isUsed = true;
    106                                 break;
    107                             }
    108                         }
    109                     }
    110                     if (!isUsed) {
    111                         timeSlots.add(currentDateTime);
    112                         System.out.println("Added time slot: " + currentDateTime); // Debug output
    113                     } else {
    114                         // Remove conflicting time slots
    115                         timeSlots.removeIf(x -> x.isAfter(currentDateTime.minusHours(2)) && x.isBefore(currentDateTime.plusHours(2)));
    116                     }
    117                     // Increment currentTime
    118                     currentTime = currentTime.plusMinutes(15);
    119                 }
    120             }
    121         }
    122 
    123         return timeSlots;
     91        this.reservations.remove(reservation);
    12492    }
    12593
    126 
    127 
    128     public TableEntity(Restaurant restaurant, int capacity, String location, boolean isSmokingArea, String description, List<LocalDateTime> timeSlots, int reservationDurationHours) {
     94    public TableEntity(Restaurant restaurant, int capacity, String location, boolean isSmokingArea, String description, int reservationDurationHours) {
    12995        this.restaurant = restaurant;
    13096        this.capacity = capacity;
     
    13298        this.isSmokingArea = isSmokingArea;
    13399        this.description = description;
    134         this.timeSlots = timeSlots;
    135100        this.reservationDurationHours = reservationDurationHours;
    136101    }
     
    138103    public TableEntity() {
    139104    }
    140 
    141     public Long getId() {
    142         return id;
    143     }
    144 
    145     public void setId(Long id) {
    146         this.id = id;
    147     }
    148 
    149     public Restaurant getRestaurant() {
    150         return restaurant;
    151     }
    152 
    153     public void setRestaurant(Restaurant restaurant) {
    154         this.restaurant = restaurant;
    155     }
    156 
    157     public int getCapacity() {
    158         return capacity;
    159     }
    160 
    161     public void setCapacity(int capacity) {
    162         this.capacity = capacity;
    163     }
    164 
    165     public String getLocation() {
    166         return location;
    167     }
    168 
    169     public void setLocation(String location) {
    170         this.location = location;
    171     }
    172 
    173     public void setSmokingArea(Boolean isSmokingArea) {
    174         this.isSmokingArea = isSmokingArea;
    175     }
    176 
    177     public void setSmokingArea(boolean smokingArea) {
    178         isSmokingArea = smokingArea;
    179     }
    180 
    181     public String getDescription() {
    182         return description;
    183     }
    184 
    185     public void setDescription(String description) {
    186         this.description = description;
    187     }
    188 
    189     public List<LocalDateTime> getTimeSlots() {
    190         return timeSlots;
    191     }
    192 
    193     public void setTimeSlots(List<LocalDateTime> timeSlots) {
    194         this.timeSlots = timeSlots;
    195     }
    196 
    197     public int getReservationDurationHours() {
    198         return reservationDurationHours;
    199     }
    200 
    201     public void setReservationDurationHours(int reservationDurationHours) {
    202         this.reservationDurationHours = reservationDurationHours;
    203     }
    204105}
Note: See TracChangeset for help on using the changeset viewer.