- Timestamp:
- 04/28/25 14:21:17 (3 weeks ago)
- Branches:
- main
- Children:
- e15e8d9
- Parents:
- f5b256e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/rezevirajmasa/demo/model/TableEntity.java
rf5b256e rdeea3c4 2 2 3 3 import com.fasterxml.jackson.annotation.JsonBackReference; 4 import com.fasterxml.jackson.annotation.JsonIgnore; 5 import com.fasterxml.jackson.annotation.JsonManagedReference; 4 6 import jakarta.persistence.*; 7 import lombok.Getter; 8 import lombok.Setter; 5 9 import org.springframework.web.bind.annotation.ModelAttribute; 6 10 … … 15 19 16 20 @Entity 21 @Getter 22 @Setter 17 23 @Table(name = "tables") 18 24 public class TableEntity { … … 39 45 private String description; 40 46 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 47 47 @Column(name = "reservation_duration_hours", nullable = true) 48 48 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 49 54 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)); 52 56 } 53 57 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; 63 61 } 64 62 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()); 67 66 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)) 70 68 && desiredTimeSlot.isBefore(endTime)) { 71 return false; // Table is reserved for the desired time slot69 return false; 72 70 } 73 71 } 74 72 75 return true; // No conflicting reservations, table is available73 return true; 76 74 } 77 75 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); 81 83 } 82 84 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)) { 89 89 90 if (generatedDates.containsAll(dates) && dates.containsAll(generatedDates)) {91 System.out.println("Time slots already cover all provided dates.");92 return timeSlots;93 90 } 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); 124 92 } 125 93 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) { 129 95 this.restaurant = restaurant; 130 96 this.capacity = capacity; … … 132 98 this.isSmokingArea = isSmokingArea; 133 99 this.description = description; 134 this.timeSlots = timeSlots;135 100 this.reservationDurationHours = reservationDurationHours; 136 101 } … … 138 103 public TableEntity() { 139 104 } 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 }204 105 }
Note:
See TracChangeset
for help on using the changeset viewer.