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

main
Last change on this file since b67dfd3 was b67dfd3, checked in by Aleksandar Panovski <apano77@…>, 12 days ago

Normalization needed to continue, till here done

  • Property mode set to 100644
File size: 3.5 KB
Line 
1package com.example.rezevirajmasa.demo.model;
2
3import com.fasterxml.jackson.annotation.JsonBackReference;
4import com.fasterxml.jackson.annotation.JsonIgnore;
5import com.fasterxml.jackson.annotation.JsonManagedReference;
6import jakarta.persistence.*;
7import lombok.Getter;
8import lombok.Setter;
9import org.springframework.web.bind.annotation.ModelAttribute;
10
11import java.time.LocalDate;
12import java.time.LocalDateTime;
13import java.time.LocalTime;
14import java.util.ArrayList;
15
16import java.util.Iterator;
17import java.util.List;
18import java.util.stream.Collectors;
19
20@Entity
21@Getter
22@Setter
23@Table(name = "tables")
24public 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 = "tableLocation")
39 private String tableLocation;
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<>();
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.tableLocation = location;
98 this.isSmokingArea = isSmokingArea;
99 this.description = description;
100 this.reservationDurationHours = reservationDurationHours;
101 }
102
103 public TableEntity() {
104 }
105}
Note: See TracBrowser for help on using the repository browser.