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.User;
|
---|
10 | import com.example.rezevirajmasa.demo.model.exceptions.InvalidRestaurantIdException;
|
---|
11 | import com.example.rezevirajmasa.demo.repository.RestaurantRepository;
|
---|
12 | import com.example.rezevirajmasa.demo.repository.TableRepository;
|
---|
13 | import com.example.rezevirajmasa.demo.service.ReservationService;
|
---|
14 | import com.example.rezevirajmasa.demo.service.RestaurantService;
|
---|
15 | import com.example.rezevirajmasa.demo.service.TableService;
|
---|
16 | import com.sun.tools.jconsole.JConsoleContext;
|
---|
17 | import jakarta.transaction.Transactional;
|
---|
18 | import org.openqa.selenium.InvalidArgumentException;
|
---|
19 | import org.springframework.stereotype.Service;
|
---|
20 | import org.modelmapper.ModelMapper;
|
---|
21 | import java.time.LocalDate;
|
---|
22 | import java.time.LocalDateTime;
|
---|
23 | import java.time.LocalTime;
|
---|
24 | import java.time.format.DateTimeFormatter;
|
---|
25 | import java.math.BigDecimal;
|
---|
26 | import java.time.format.DateTimeParseException;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Collections;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Optional;
|
---|
31 | import java.util.stream.Collectors;
|
---|
32 |
|
---|
33 |
|
---|
34 | @Service
|
---|
35 | @Transactional
|
---|
36 | public class RestaurantServiceImpl implements RestaurantService {
|
---|
37 | private final RestaurantRepository restaurantRepository;
|
---|
38 | private final TableRepository tableRepository;
|
---|
39 | private final TableService tableService;
|
---|
40 | private final ReservationService reservationService;
|
---|
41 | private ModelMapper modelMapper = new ModelMapper();
|
---|
42 |
|
---|
43 | public RestaurantServiceImpl(RestaurantRepository restaurantRepository, TableRepository tableRepository, TableService tableService, ReservationService reservationService) {
|
---|
44 | this.restaurantRepository = restaurantRepository;
|
---|
45 | this.tableRepository = tableRepository;
|
---|
46 | this.tableService = tableService;
|
---|
47 | this.reservationService = reservationService;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public List<RestaurantDTO> listall() {
|
---|
52 | List<Restaurant> restaurants = restaurantRepository.findAll();
|
---|
53 |
|
---|
54 | List<RestaurantDTO> restaurantDTOS = new ArrayList<>();
|
---|
55 |
|
---|
56 | for (Restaurant restaurant : restaurants) {
|
---|
57 | RestaurantDTO restaurantDTO = modelMapper.map(restaurant, RestaurantDTO.class);
|
---|
58 | List<TableDTO> tableDTOS = new ArrayList<>();
|
---|
59 | for (TableEntity table : restaurant.getTablesList()) {
|
---|
60 | TableDTO tableDTO = modelMapper.map(table, TableDTO.class);
|
---|
61 | List<ReservationDTO> reservationDTOS = new ArrayList<>();
|
---|
62 | for (Reservation reservation : table.getReservations()) {
|
---|
63 | ReservationDTO reservationDTO = modelMapper.map(reservation, ReservationDTO.class);
|
---|
64 |
|
---|
65 | reservationDTOS.add(reservationDTO);
|
---|
66 | }
|
---|
67 | tableDTO.setReservations(reservationDTOS);
|
---|
68 | tableDTOS.add(tableDTO);
|
---|
69 | }
|
---|
70 | restaurantDTO.setTablesList(tableDTOS);
|
---|
71 | restaurantDTOS.add(restaurantDTO);
|
---|
72 | }
|
---|
73 | return restaurantDTOS;
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public List<Restaurant> listAll() {
|
---|
78 | return restaurantRepository.findAll();
|
---|
79 | }
|
---|
80 |
|
---|
81 | // @Override
|
---|
82 | // public void save(String name, String cuisineType, String address, String phone, String operatingHours, String website, String socialMediaLinks, BigDecimal rating, List<Long> tablesList) {
|
---|
83 | // List<TableEntity> tableEntities = tableRepository.findAllById(tablesList);
|
---|
84 | // restaurantRepository.save(new Restaurant(name, cuisineType, address, phone, operatingHours, website, socialMediaLinks, rating, tableEntities));
|
---|
85 | // }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void save(Restaurant restaurant, int numberOfTables, List<Integer> tableCapacities, List<String> tableLocations, List<String> tableSmokingAreas, List<String> tableDescriptions) {
|
---|
89 | if (numberOfTables != tableCapacities.size() || numberOfTables != tableLocations.size() || numberOfTables != tableSmokingAreas.size() || numberOfTables != tableDescriptions.size()) {
|
---|
90 | throw new IllegalArgumentException("Mismatched table data. Number of tables does not match the size of the input lists.");
|
---|
91 | }
|
---|
92 |
|
---|
93 | restaurantRepository.save(restaurant);
|
---|
94 | String[] hours = restaurant.getOperatingHours().split("-");
|
---|
95 | try {
|
---|
96 | if (hours.length != 2) {
|
---|
97 | throw new IllegalArgumentException("Invalid operating hours format");
|
---|
98 | }
|
---|
99 |
|
---|
100 | LocalTime startTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
101 | LocalTime endTime = LocalTime.parse(hours[1], DateTimeFormatter.ofPattern("HH:mm"));
|
---|
102 |
|
---|
103 | for (int i = 0; i < numberOfTables; i++) {
|
---|
104 | TableEntity table = new TableEntity();
|
---|
105 | table.setCapacity(tableCapacities.get(i));
|
---|
106 | table.setTableLocation(tableLocations.get(i));
|
---|
107 | table.setSmokingArea(tableSmokingAreas.get(i).equalsIgnoreCase("on"));
|
---|
108 | table.setDescription(tableDescriptions.get(i));
|
---|
109 | table.setRestaurant(restaurant);
|
---|
110 | tableRepository.save(table);
|
---|
111 | }
|
---|
112 | } catch (DateTimeParseException e) {
|
---|
113 | System.out.println("Error parsing operating hours: " + e.getMessage());
|
---|
114 | } catch (IllegalArgumentException e) {
|
---|
115 | System.out.println("Invalid operating hours format: " + e.getMessage());
|
---|
116 | }
|
---|
117 | }
|
---|
118 | @Override
|
---|
119 | public RestaurantDTO findById(Long restaurantId) {
|
---|
120 | Restaurant restaurant = restaurantRepository.findById(restaurantId)
|
---|
121 | .orElseThrow(InvalidRestaurantIdException::new);
|
---|
122 |
|
---|
123 | RestaurantDTO restaurantDTO = modelMapper.map(restaurant, RestaurantDTO.class);
|
---|
124 |
|
---|
125 | List<TableDTO> tableDTOS = new ArrayList<>();
|
---|
126 | for (TableEntity table : restaurant.getTablesList()) {
|
---|
127 | TableDTO tableDTO = modelMapper.map(table, TableDTO.class);
|
---|
128 |
|
---|
129 | List<ReservationDTO> reservationDTOS = new ArrayList<>();
|
---|
130 | for (Reservation reservation : table.getReservations()) {
|
---|
131 | ReservationDTO reservationDTO = modelMapper.map(reservation, ReservationDTO.class);
|
---|
132 | reservationDTOS.add(reservationDTO);
|
---|
133 | }
|
---|
134 |
|
---|
135 | tableDTO.setReservations(reservationDTOS);
|
---|
136 | tableDTOS.add(tableDTO);
|
---|
137 | }
|
---|
138 |
|
---|
139 | restaurantDTO.setTablesList(tableDTOS);
|
---|
140 |
|
---|
141 | return restaurantDTO;
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public Restaurant findByIdRestaurant(Long restaurantId) {
|
---|
146 | return restaurantRepository.findById(restaurantId).orElseThrow(()-> new InvalidArgumentException("No restaurant with provided id"));
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public Restaurant updateRestaurant(Long restaurantId, String name, String cuisineType, String address, String phone, String operatingHours, String website, String socialMediaLinks, BigDecimal rating, List<Long> tablesList) {
|
---|
152 | List<TableEntity> tableEntities = tableRepository.findAllById(tablesList);
|
---|
153 |
|
---|
154 | Restaurant restaurant = restaurantRepository.findById(restaurantId).orElseThrow(InvalidRestaurantIdException::new);
|
---|
155 | restaurant.setName(name);
|
---|
156 | restaurant.setCuisineType(cuisineType);
|
---|
157 | restaurant.setAddress(address);
|
---|
158 | restaurant.setPhone(phone);
|
---|
159 | restaurant.setOperatingHours(operatingHours);
|
---|
160 | restaurant.setWebsite(website);
|
---|
161 | restaurant.setSocialMediaLinks(socialMediaLinks);
|
---|
162 | restaurant.setRating(rating);
|
---|
163 | restaurant.setTablesList(tableEntities);
|
---|
164 |
|
---|
165 | return restaurantRepository.save(restaurant);
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public Restaurant deleteRestaurant(Long restaurantId) {
|
---|
170 | Restaurant restaurant = restaurantRepository.findById(restaurantId).orElseThrow(InvalidRestaurantIdException::new);
|
---|
171 | restaurantRepository.delete(restaurant);
|
---|
172 | return restaurant;
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public List<Restaurant> listRestaurantBy(String search) {
|
---|
177 | List<Restaurant> allRestaurants = restaurantRepository.findAll();
|
---|
178 |
|
---|
179 |
|
---|
180 | return allRestaurants.stream()
|
---|
181 | .filter(restaurant ->
|
---|
182 | restaurant.getName().toLowerCase().contains(search.toLowerCase()) ||
|
---|
183 | restaurant.getCuisineType().toLowerCase().contains(search.toLowerCase())
|
---|
184 | )
|
---|
185 | .collect(Collectors.toList());
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public List<Restaurant> getRestaurantsWithAvailableTimeSlotsForToday() {
|
---|
190 | LocalDate today = LocalDate.now();
|
---|
191 | List<Restaurant> restaurants = restaurantRepository.findAll();
|
---|
192 | List<Restaurant> restaurantsWithAvailableTimeSlots = new ArrayList<>();
|
---|
193 |
|
---|
194 | for (Restaurant restaurant : restaurants) {
|
---|
195 | boolean hasAvailableTimeSlots = tableService.hasAvailableTimeSlotsForRestaurantAndDate(restaurant, today);
|
---|
196 | if (hasAvailableTimeSlots) {
|
---|
197 | restaurantsWithAvailableTimeSlots.add(restaurant);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | return restaurantsWithAvailableTimeSlots;
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public List<Restaurant> findRestaurantsByDateTimeAndPartySize(LocalDateTime dateTime, int partySize, String search) {
|
---|
206 | LocalDateTime checkOutTime = dateTime.plusHours(2);
|
---|
207 | List<Restaurant> allRestaurants = restaurantRepository.findAll();
|
---|
208 |
|
---|
209 | return allRestaurants.stream()
|
---|
210 | .filter(restaurant -> restaurant.getTablesList().stream()
|
---|
211 | .anyMatch(table -> tableRepository.findAvailableTables(dateTime, checkOutTime, partySize)
|
---|
212 | .contains(table)))
|
---|
213 | .filter(restaurant -> isMatch(restaurant, search))
|
---|
214 | .collect(Collectors.toList());
|
---|
215 | }
|
---|
216 |
|
---|
217 | private boolean isMatch(Restaurant restaurant, String name) {
|
---|
218 | return name == null || name.isEmpty() || restaurant.getName().contains(name);
|
---|
219 | }
|
---|
220 |
|
---|
221 | @Override
|
---|
222 | public List<RestaurantDTO> findRestaurantsBySearchParams(LocalDateTime dateTime, int partySize, String search) {
|
---|
223 | List<Restaurant> availableRestaurants = new ArrayList<>();
|
---|
224 |
|
---|
225 | List<Restaurant> restaurantList;
|
---|
226 | if (search == null || search.isEmpty()) {
|
---|
227 | restaurantList = restaurantRepository.findAll();
|
---|
228 | } else {
|
---|
229 | String searchTerm = "%" + search + "%";
|
---|
230 | restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(searchTerm);
|
---|
231 |
|
---|
232 | if (restaurantList.isEmpty()) {
|
---|
233 | restaurantList = restaurantRepository.findAllByCuisineTypeContaining(search);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | for (Restaurant restaurant : restaurantList) {
|
---|
238 | boolean hasAvailableTable = restaurant.getTablesList().stream()
|
---|
239 | .anyMatch(table -> table.isAvailable(dateTime, partySize));
|
---|
240 |
|
---|
241 | if (hasAvailableTable) {
|
---|
242 | availableRestaurants.add(restaurant);
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | List<RestaurantDTO> restaurantDTOS = availableRestaurants.stream().map(this::convertToDto).toList();
|
---|
247 | return restaurantDTOS;
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Override
|
---|
251 | public List<String> findALlCuisineTypes() {
|
---|
252 | return restaurantRepository.findAllCuisineTypes();
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public List<RestaurantDTO> findRestaurantsByCuisineType(String param) {
|
---|
257 | List<Restaurant> restaurants = restaurantRepository.findAllByCuisineType(param);
|
---|
258 | return restaurants.stream().map(this::convertToDto).toList();
|
---|
259 | }
|
---|
260 |
|
---|
261 | public RestaurantDTO convertToDto(Restaurant restaurant) {
|
---|
262 | return modelMapper.map(restaurant, RestaurantDTO.class);
|
---|
263 | }
|
---|
264 |
|
---|
265 | public TableDTO convertToDto(TableEntity table) {
|
---|
266 | return modelMapper.map(table, TableDTO.class);
|
---|
267 | }
|
---|
268 |
|
---|
269 | public ReservationDTO convertToDto(Reservation reservation) {
|
---|
270 | return modelMapper.map(reservation, ReservationDTO.class);
|
---|
271 | }
|
---|
272 | }
|
---|