1 | package com.example.rezevirajmasa.demo.model;
|
---|
2 |
|
---|
3 | import com.fasterxml.jackson.annotation.JsonBackReference;
|
---|
4 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
---|
5 | import com.fasterxml.jackson.annotation.JsonManagedReference;
|
---|
6 | import jakarta.persistence.*;
|
---|
7 | import lombok.Getter;
|
---|
8 | import lombok.Setter;
|
---|
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
|
---|
21 | @Getter
|
---|
22 | @Setter
|
---|
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
|
---|
31 | @ManyToOne(fetch = FetchType.LAZY)
|
---|
32 | @JoinColumn(name = "RestaurantID")
|
---|
33 | private Restaurant restaurant;
|
---|
34 |
|
---|
35 | @Column(name = "Capacity")
|
---|
36 | private int capacity;
|
---|
37 |
|
---|
38 | @Column(name = "Location")
|
---|
39 | private String location;
|
---|
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 |
|
---|
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 |
|
---|
54 | public void cleanUnusedTimeSlots(LocalDate threeDaysAgo) {
|
---|
55 | reservations.removeIf(reservation -> reservation.getReservationDateTime().toLocalDate().isBefore(threeDaysAgo));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public boolean isAvailable(LocalDateTime desiredTimeSlot, int partySize) {
|
---|
59 | if (this.capacity < partySize) {
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 |
|
---|
63 | for (Reservation reservation : reservations) {
|
---|
64 | LocalDateTime startTime = reservation.getReservationDateTime();
|
---|
65 | LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
|
---|
66 |
|
---|
67 | if ((desiredTimeSlot.isEqual(startTime) || desiredTimeSlot.isAfter(startTime))
|
---|
68 | && desiredTimeSlot.isBefore(endTime)) {
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
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);
|
---|
83 | }
|
---|
84 |
|
---|
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 |
|
---|
90 | }
|
---|
91 | this.reservations.remove(reservation);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public TableEntity(Restaurant restaurant, int capacity, String location, boolean isSmokingArea, String description, int reservationDurationHours) {
|
---|
95 | this.restaurant = restaurant;
|
---|
96 | this.capacity = capacity;
|
---|
97 | this.location = location;
|
---|
98 | this.isSmokingArea = isSmokingArea;
|
---|
99 | this.description = description;
|
---|
100 | this.reservationDurationHours = reservationDurationHours;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public TableEntity() {
|
---|
104 | }
|
---|
105 | }
|
---|