1 | package com.example.rezevirajmasa.demo.service.impl;
|
---|
2 |
|
---|
3 | import com.example.rezevirajmasa.demo.dto.ReservationDTO;
|
---|
4 | import com.example.rezevirajmasa.demo.dto.RestaurantDTO;
|
---|
5 | import com.example.rezevirajmasa.demo.dto.TableDTO;
|
---|
6 | import com.example.rezevirajmasa.demo.model.Reservation;
|
---|
7 | import com.example.rezevirajmasa.demo.model.Restaurant;
|
---|
8 | import com.example.rezevirajmasa.demo.model.TableEntity;
|
---|
9 | import com.example.rezevirajmasa.demo.model.exceptions.InvalidTableNumberException;
|
---|
10 | import com.example.rezevirajmasa.demo.repository.ReservationRepository;
|
---|
11 | import com.example.rezevirajmasa.demo.repository.RestaurantRepository;
|
---|
12 | import com.example.rezevirajmasa.demo.repository.TableRepository;
|
---|
13 | import com.example.rezevirajmasa.demo.service.TableService;
|
---|
14 | import jakarta.persistence.Table;
|
---|
15 | import org.modelmapper.ModelMapper;
|
---|
16 | import org.openqa.selenium.InvalidArgumentException;
|
---|
17 | import org.springframework.cglib.core.Local;
|
---|
18 | import org.springframework.stereotype.Service;
|
---|
19 | import org.springframework.transaction.annotation.Transactional;
|
---|
20 |
|
---|
21 | import java.time.DateTimeException;
|
---|
22 | import java.time.LocalDate;
|
---|
23 | import java.time.LocalDateTime;
|
---|
24 | import java.time.LocalTime;
|
---|
25 | import java.time.chrono.ChronoLocalDateTime;
|
---|
26 | import java.time.format.DateTimeFormatter;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Iterator;
|
---|
29 | import java.util.List;
|
---|
30 |
|
---|
31 | @Service
|
---|
32 | public class TableServiceImpl implements TableService {
|
---|
33 | private final TableRepository tableRepository;
|
---|
34 | private final RestaurantRepository restaurantRepository;
|
---|
35 | private final ReservationRepository reservationRepository;
|
---|
36 | private ModelMapper modelMapper = new ModelMapper();
|
---|
37 |
|
---|
38 | public TableServiceImpl(TableRepository tableRepository, RestaurantRepository restaurantRepository, ReservationRepository reservationRepository) {
|
---|
39 | this.tableRepository = tableRepository;
|
---|
40 | this.restaurantRepository = restaurantRepository;
|
---|
41 | this.reservationRepository = reservationRepository;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public List<TableEntity> listall() {
|
---|
46 | return tableRepository.findAll();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public TableDTO findById(Long id) {
|
---|
51 | TableEntity table = tableRepository.findById(id)
|
---|
52 | .orElseThrow(InvalidTableNumberException::new);
|
---|
53 |
|
---|
54 | TableDTO tableDTO = modelMapper.map(table, TableDTO.class);
|
---|
55 |
|
---|
56 | List<ReservationDTO> reservationDTOS = new ArrayList<>();
|
---|
57 | for (Reservation reservation : table.getReservations()) {
|
---|
58 | ReservationDTO reservationDTO = modelMapper.map(reservation, ReservationDTO.class);
|
---|
59 | reservationDTOS.add(reservationDTO);
|
---|
60 | }
|
---|
61 |
|
---|
62 | tableDTO.setReservations(reservationDTOS);
|
---|
63 |
|
---|
64 | return tableDTO;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public TableEntity findByIdTable(Long id) {
|
---|
69 | return tableRepository.findById(id).orElseThrow(() -> new InvalidArgumentException("Invalid table id"));
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void save(int numberOfTables, List<Integer> tableCapacities, List<String> tableLocations, List<String> tableSmokingAreas, List<String> tableDescriptions, Restaurant restaurant) {
|
---|
75 | for (int i = 0; i < numberOfTables; i++) {
|
---|
76 | TableEntity table = new TableEntity();
|
---|
77 | table.setCapacity(tableCapacities.get(i));
|
---|
78 | table.setTableLocation(tableLocations.get(i));
|
---|
79 | table.setSmokingArea(Boolean.valueOf(tableSmokingAreas.get(i)));
|
---|
80 | table.setDescription(tableDescriptions.get(i));
|
---|
81 | table.setRestaurant(restaurant);
|
---|
82 | tableRepository.save(table);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public void deleteTimeSlotsForReservation(Long tableId, LocalDateTime reservationTime) {
|
---|
88 | TableDTO tableDTO = findById(tableId);
|
---|
89 | TableEntity table = convertFromDTO(tableDTO);
|
---|
90 | LocalDate threeDaysAgo = LocalDate.now().minusDays(3);
|
---|
91 | table.cleanUnusedTimeSlots(threeDaysAgo);
|
---|
92 |
|
---|
93 | tableRepository.saveAndFlush(table);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void canceledTimeSlots(Long tableId, LocalDateTime reservationTime) {
|
---|
98 | TableDTO tableDTO = findById(tableId);
|
---|
99 | TableEntity table = convertFromDTO(tableDTO);
|
---|
100 | LocalDateTime startTime = reservationTime.minusHours(1).minusMinutes(45);
|
---|
101 | LocalDateTime endTime = reservationTime.plusHours(1).plusMinutes(45);
|
---|
102 |
|
---|
103 | String[] hours = table.getRestaurant().getOperatingHours().split("-");
|
---|
104 | LocalTime openingHourTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
105 | LocalDateTime openingHour = openingHourTime.atDate(reservationTime.toLocalDate());
|
---|
106 | LocalDateTime closingHour = LocalTime.of(23, 45).atDate(reservationTime.toLocalDate());
|
---|
107 |
|
---|
108 | if (startTime.isBefore(openingHour)) {
|
---|
109 | startTime = openingHour;
|
---|
110 | }
|
---|
111 |
|
---|
112 | while (startTime.isBefore(endTime) || startTime.equals(endTime)) {
|
---|
113 | if (startTime.isAfter(closingHour) || startTime.equals(closingHour)) {
|
---|
114 | break;
|
---|
115 | }
|
---|
116 |
|
---|
117 | Reservation reservation = new Reservation();
|
---|
118 | reservation.setReservationDateTime(startTime);
|
---|
119 | reservation.setTable(table);
|
---|
120 | reservationRepository.save(reservation);
|
---|
121 |
|
---|
122 | startTime = startTime.plusMinutes(15);
|
---|
123 | }
|
---|
124 |
|
---|
125 | tableRepository.saveAndFlush(table);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void saveTable(TableEntity table) {
|
---|
130 | tableRepository.save(table);
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public TableEntity getTableByNumber(Long number) {
|
---|
135 | return tableRepository.findById(number).orElseThrow(InvalidTableNumberException::new);
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public TableEntity deleteTable(Long number) {
|
---|
140 | TableEntity tableEntity = tableRepository.findById(number).orElseThrow(InvalidTableNumberException::new);
|
---|
141 | tableRepository.deleteById(number);
|
---|
142 | return tableEntity;
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public boolean hasAvailableTimeSlotsForRestaurantAndDate(Restaurant restaurant, LocalDate today) {
|
---|
147 | List<TableEntity> tables = tableRepository.findByRestaurant(restaurant);
|
---|
148 |
|
---|
149 | for (TableEntity table : tables) {
|
---|
150 | boolean hasAvailableTimeSlots = hasAvailableTimeSlotsForTableAndDate(table, today);
|
---|
151 | if (hasAvailableTimeSlots) {
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 |
|
---|
159 | public boolean hasAvailableTimeSlotsForTableAndDate(TableEntity table, LocalDate date) {
|
---|
160 | for (Reservation reservation : table.getReservations()) {
|
---|
161 | LocalDateTime startTime = reservation.getReservationDateTime();
|
---|
162 | LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
|
---|
163 |
|
---|
164 | if (startTime.toLocalDate().equals(date) || endTime.toLocalDate().equals(date)) {
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public RestaurantDTO convertToDto(Restaurant restaurant) {
|
---|
172 | return modelMapper.map(restaurant, RestaurantDTO.class);
|
---|
173 | }
|
---|
174 |
|
---|
175 | public TableDTO convertToDto(TableEntity table) {
|
---|
176 | return modelMapper.map(table, TableDTO.class);
|
---|
177 | }
|
---|
178 |
|
---|
179 | public ReservationDTO convertToDto(Reservation reservation) {
|
---|
180 | return modelMapper.map(reservation, ReservationDTO.class);
|
---|
181 | }
|
---|
182 |
|
---|
183 | public TableEntity convertFromDTO(TableDTO tableDTO) {
|
---|
184 | return modelMapper.map(tableDTO, TableEntity.class);
|
---|
185 | }
|
---|
186 | }
|
---|