source: src/main/java/com/example/rezevirajmasa/demo/model/TableEntity.java@ f5b256e

main
Last change on this file since f5b256e was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 6.4 KB
Line 
1package com.example.rezevirajmasa.demo.model;
2
3import com.fasterxml.jackson.annotation.JsonBackReference;
4import jakarta.persistence.*;
5import org.springframework.web.bind.annotation.ModelAttribute;
6
7import java.time.LocalDate;
8import java.time.LocalDateTime;
9import java.time.LocalTime;
10import java.util.ArrayList;
11
12import java.util.Iterator;
13import java.util.List;
14import java.util.stream.Collectors;
15
16@Entity
17@Table(name = "tables")
18public class TableEntity {
19 @Id
20 @GeneratedValue(strategy = GenerationType.IDENTITY)
21 @Column(name = "TableID")
22 private Long id;
23
24 @JsonBackReference
25 @ManyToOne(fetch = FetchType.EAGER)
26 @JoinColumn(name = "RestaurantID")
27 private Restaurant restaurant;
28
29 @Column(name = "Capacity")
30 private int capacity;
31
32 @Column(name = "Location")
33 private String location;
34
35 @Column(name = "IsSmokingArea")
36 private boolean isSmokingArea;
37
38 @Column(name = "Description", length = 1000)
39 private String description;
40
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 @Column(name = "reservation_duration_hours", nullable = true)
48 private int reservationDurationHours = 2;
49 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()));
52 }
53
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;
63 }
64
65 for (LocalDateTime reservedTimeSlot : timeSlots) {
66 LocalDateTime endTime = reservedTimeSlot.plusHours(reservationDurationHours);
67
68 // Check if the desired time slot overlaps with any existing reservation
69 if ((desiredTimeSlot.equals(reservedTimeSlot) || desiredTimeSlot.isAfter(reservedTimeSlot))
70 && desiredTimeSlot.isBefore(endTime)) {
71 return false; // Table is reserved for the desired time slot
72 }
73 }
74
75 return true; // No conflicting reservations, table is available
76 }
77
78 public boolean hasTimeSlot(LocalDateTime dateTime) {
79 // Check if the given dateTime exists in the list of time slots
80 return timeSlots.contains(dateTime);
81 }
82
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();
89
90 if (generatedDates.containsAll(dates) && dates.containsAll(generatedDates)) {
91 System.out.println("Time slots already cover all provided dates.");
92 return timeSlots;
93 }
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;
124 }
125
126
127
128 public TableEntity(Restaurant restaurant, int capacity, String location, boolean isSmokingArea, String description, List<LocalDateTime> timeSlots, int reservationDurationHours) {
129 this.restaurant = restaurant;
130 this.capacity = capacity;
131 this.location = location;
132 this.isSmokingArea = isSmokingArea;
133 this.description = description;
134 this.timeSlots = timeSlots;
135 this.reservationDurationHours = reservationDurationHours;
136 }
137
138 public TableEntity() {
139 }
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}
Note: See TracBrowser for help on using the repository browser.