source: src/main/java/com/example/rezevirajmasa/demo/service/impl/TableServiceImpl.java

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

Normalization needed to continue, till here done

  • Property mode set to 100644
File size: 7.1 KB
Line 
1package com.example.rezevirajmasa.demo.service.impl;
2
3import com.example.rezevirajmasa.demo.dto.ReservationDTO;
4import com.example.rezevirajmasa.demo.dto.RestaurantDTO;
5import com.example.rezevirajmasa.demo.dto.TableDTO;
6import com.example.rezevirajmasa.demo.model.Reservation;
7import com.example.rezevirajmasa.demo.model.Restaurant;
8import com.example.rezevirajmasa.demo.model.TableEntity;
9import com.example.rezevirajmasa.demo.model.exceptions.InvalidTableNumberException;
10import com.example.rezevirajmasa.demo.repository.ReservationRepository;
11import com.example.rezevirajmasa.demo.repository.RestaurantRepository;
12import com.example.rezevirajmasa.demo.repository.TableRepository;
13import com.example.rezevirajmasa.demo.service.TableService;
14import jakarta.persistence.Table;
15import org.modelmapper.ModelMapper;
16import org.openqa.selenium.InvalidArgumentException;
17import org.springframework.cglib.core.Local;
18import org.springframework.stereotype.Service;
19import org.springframework.transaction.annotation.Transactional;
20
21import java.time.DateTimeException;
22import java.time.LocalDate;
23import java.time.LocalDateTime;
24import java.time.LocalTime;
25import java.time.chrono.ChronoLocalDateTime;
26import java.time.format.DateTimeFormatter;
27import java.util.ArrayList;
28import java.util.Iterator;
29import java.util.List;
30
31@Service
32public 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}
Note: See TracBrowser for help on using the repository browser.