source: src/main/java/com/example/rezevirajmasa/demo/bootstrap/TimeSlotManagementService.java@ a7d40aa

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

RetaurantServiceImpl problemi
isAvailable od tableEntity...

  • Property mode set to 100644
File size: 4.2 KB
Line 
1package com.example.rezevirajmasa.demo.bootstrap;
2
3import com.example.rezevirajmasa.demo.model.Reservation;
4import com.example.rezevirajmasa.demo.model.Restaurant;
5import com.example.rezevirajmasa.demo.model.TableEntity;
6import com.example.rezevirajmasa.demo.repository.ReservationRepository;
7import com.example.rezevirajmasa.demo.repository.TableRepository;
8import jakarta.annotation.PostConstruct;
9import jakarta.annotation.PreDestroy;
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.cglib.core.Local;
12import org.springframework.stereotype.Component;
13
14import java.time.LocalDate;
15import java.time.LocalDateTime;
16import java.time.LocalTime;
17import java.time.format.DateTimeFormatter;
18import java.util.ArrayList;
19import java.util.Collections;
20import java.util.List;
21import java.util.stream.Collectors;
22
23@Component
24public class TimeSlotManagementService {
25 @Autowired
26 private TableRepository tableRepository;
27 @Autowired
28 private ReservationRepository reservationRepository;
29
30 @PostConstruct
31 public void initializeTimeSlots() {
32 LocalDate startDate = LocalDate.now();
33 initializeTimeSlotsForNextDays(startDate);
34 }
35
36 private void initializeTimeSlotsForNextDays(LocalDate startDate) {
37 LocalDate currentDate = startDate;
38 for (int i = 0; i < 3; i++) {
39 Iterable<TableEntity> tableEntities = tableRepository.findAll();
40 for (TableEntity tableEntity : tableEntities) {
41 initializeTimeSlotsForTable(tableEntity, currentDate);
42 tableRepository.save(tableEntity);
43 }
44 currentDate = currentDate.plusDays(1);
45 }
46 }
47
48 private void initializeTimeSlotsForTable(TableEntity tableEntity, LocalDate date) {
49 // Get existing time slots for the table
50 List<LocalDateTime> existingTimeSlots = tableEntity.getTimeSlots().stream()
51 .filter(slot -> slot.toLocalDate().isEqual(date)) // Filter time slots for the specified date
52 .toList();
53
54 // If existing time slots are not null and not empty, return without initializing new ones
55 if (!existingTimeSlots.isEmpty()) {
56 return;
57 }
58
59 Restaurant restaurant = tableEntity.getRestaurant();
60 String[] hours = restaurant.getOperatingHours().split("-");
61 LocalTime startTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm"));
62 LocalTime endTime = LocalTime.parse(hours[1], DateTimeFormatter.ofPattern("HH:mm"));
63
64 List<LocalDateTime> reservationCheckIns = getReservationCheckInsAfterNow(date);
65
66 tableEntity.initializeTimeSlots(startTime, endTime, Collections.singletonList(date), reservationCheckIns);
67 }
68
69 private List<LocalDateTime> getReservationCheckInsAfterNow(LocalDate date) {
70 LocalDateTime currentTime = LocalDateTime.now();
71 List<Reservation> reservations = reservationRepository.findByCheckInTimeAfterAndCheckInTimeBefore(
72 LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
73 List<LocalDateTime> checkIns = new ArrayList<>();
74 for (Reservation reservation : reservations) {
75 if (reservation.getCheckInTime().isAfter(currentTime)) {
76 checkIns.add(reservation.getCheckInTime());
77 }
78 }
79 return checkIns;
80 }
81
82 @PreDestroy
83 public void cleanUpTimeSlots() {
84 // Define the cutoff date (yesterday)
85 LocalDate cutoffDate = LocalDate.now().minusDays(1);
86
87 // Iterate through all tables and remove unused time slots older than the cutoff date
88 Iterable<TableEntity> tableEntities = tableRepository.findAll();
89 for (TableEntity tableEntity : tableEntities) {
90 // Remove unused time slots older than the cutoff date
91 removeUnusedTimeSlots(tableEntity, cutoffDate);
92 tableRepository.save(tableEntity);
93 }
94 }
95
96 private void removeUnusedTimeSlots(TableEntity tableEntity, LocalDate cutoffDate) {
97 // Get all time slots for the table
98 List<LocalDateTime> timeSlots = tableEntity.getTimeSlots();
99
100 // Remove unused time slots older than the cutoff date
101 timeSlots.removeIf(timeSlot -> timeSlot.toLocalDate().isBefore(cutoffDate));
102 }
103}
Note: See TracBrowser for help on using the repository browser.