Changeset deea3c4 for src/main/java/com/example/rezevirajmasa/demo/service/impl/RestaurantServiceImpl.java
- Timestamp:
- 04/28/25 14:21:17 (3 weeks ago)
- Branches:
- main
- Children:
- e15e8d9
- Parents:
- f5b256e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/rezevirajmasa/demo/service/impl/RestaurantServiceImpl.java
rf5b256e rdeea3c4 1 1 package com.example.rezevirajmasa.demo.service.impl; 2 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; 3 7 import com.example.rezevirajmasa.demo.model.Restaurant; 4 8 import com.example.rezevirajmasa.demo.model.TableEntity; 9 import com.example.rezevirajmasa.demo.model.User; 5 10 import com.example.rezevirajmasa.demo.model.exceptions.InvalidRestaurantIdException; 6 11 import com.example.rezevirajmasa.demo.repository.RestaurantRepository; 7 12 import com.example.rezevirajmasa.demo.repository.TableRepository; 13 import com.example.rezevirajmasa.demo.service.ReservationService; 8 14 import com.example.rezevirajmasa.demo.service.RestaurantService; 9 15 import com.example.rezevirajmasa.demo.service.TableService; 10 16 import com.sun.tools.jconsole.JConsoleContext; 11 17 import jakarta.transaction.Transactional; 18 import org.openqa.selenium.InvalidArgumentException; 12 19 import org.springframework.stereotype.Service; 13 20 import org.modelmapper.ModelMapper; 14 21 import java.time.LocalDate; 15 22 import java.time.LocalDateTime; … … 31 38 private final TableRepository tableRepository; 32 39 private final TableService tableService; 33 34 public RestaurantServiceImpl(RestaurantRepository restaurantRepository, TableRepository tableRepository, 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) { 35 44 this.restaurantRepository = restaurantRepository; 36 45 this.tableRepository = tableRepository; 37 46 this.tableService = tableService; 38 } 39 40 @Override 41 public List<Restaurant> listall() { 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() { 42 78 return restaurantRepository.findAll(); 43 79 } … … 51 87 @Override 52 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 53 93 restaurantRepository.save(restaurant); 54 94 String[] hours = restaurant.getOperatingHours().split("-"); … … 60 100 LocalTime startTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm")); 61 101 LocalTime endTime = LocalTime.parse(hours[1], DateTimeFormatter.ofPattern("HH:mm")); 62 System.out.println("IsBefore: " + startTime.isBefore(endTime) + " and equals: " + startTime.equals(endTime));63 102 64 103 for (int i = 0; i < numberOfTables; i++) { 65 104 TableEntity table = new TableEntity(); 66 67 // table.initializeTimeSlots(startTime, endTime);68 69 105 table.setCapacity(tableCapacities.get(i)); 70 106 table.setLocation(tableLocations.get(i)); 71 72 String smokingAreaString = tableSmokingAreas.get(i); 73 table.setSmokingArea(smokingAreaString.equalsIgnoreCase("on")); 107 table.setSmokingArea(tableSmokingAreas.get(i).equalsIgnoreCase("on")); 74 108 table.setDescription(tableDescriptions.get(i)); 75 109 table.setRestaurant(restaurant); 76 77 110 tableRepository.save(table); 78 111 } … … 84 117 } 85 118 @Override 86 public Restaurant findById(Long restaurantId) { 87 return restaurantRepository.findById(restaurantId).orElseThrow(InvalidRestaurantIdException::new); 88 } 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 89 149 90 150 @Override … … 115 175 @Override 116 176 public List<Restaurant> listRestaurantBy(String search) { 117 // Get all restaurants from the repository118 177 List<Restaurant> allRestaurants = restaurantRepository.findAll(); 119 178 … … 134 193 135 194 for (Restaurant restaurant : restaurants) { 136 // Check if the restaurant has available time slots for today137 195 boolean hasAvailableTimeSlots = tableService.hasAvailableTimeSlotsForRestaurantAndDate(restaurant, today); 138 196 if (hasAvailableTimeSlots) { … … 146 204 @Override 147 205 public List<Restaurant> findRestaurantsByDateTimeAndPartySize(LocalDateTime dateTime, int partySize, String search) { 206 LocalDateTime checkOutTime = dateTime.plusHours(2); 148 207 List<Restaurant> allRestaurants = restaurantRepository.findAll(); 208 149 209 return allRestaurants.stream() 150 .filter(restaurant -> hasAvailableTable(restaurant, dateTime, partySize)) 210 .filter(restaurant -> restaurant.getTablesList().stream() 211 .anyMatch(table -> tableRepository.findAvailableTables(dateTime, checkOutTime, partySize) 212 .contains(table))) 151 213 .filter(restaurant -> isMatch(restaurant, search)) 152 214 .collect(Collectors.toList()); 153 215 } 154 216 155 private boolean hasAvailableTable(Restaurant restaurant, LocalDateTime dateTime, int partySize) {156 for (TableEntity table : restaurant.getTablesList()) {157 if (table.isAvailable(dateTime) && table.getCapacity() >= partySize) {158 return true;159 }160 }161 return false;162 }163 164 217 private boolean isMatch(Restaurant restaurant, String name) { 165 218 return name == null || name.isEmpty() || restaurant.getName().contains(name); … … 167 220 168 221 @Override 169 public List<Restaurant> findRestaurantsBySearchParams(LocalDateTime dateTime, int partySize, String search) { 222 public List<RestaurantDTO> findRestaurantsBySearchParams(LocalDateTime dateTime, int partySize, String search) { 223 List<Restaurant> availableRestaurants = new ArrayList<>(); 224 225 List<Restaurant> restaurantList; 170 226 if (search == null || search.isEmpty()) { 171 List<TableEntity> tableEntities = tableRepository.findAllByTimeSlotsContainingAndCapacityGreaterThanEqual(dateTime, partySize); 172 return tableEntities.stream() 173 .map(TableEntity::getRestaurant) 174 .distinct() 175 .collect(Collectors.toList()); 227 restaurantList = restaurantRepository.findAll(); 176 228 } else { 177 229 String searchTerm = "%" + search + "%"; 178 List<Restaurant> restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(searchTerm); 230 restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(searchTerm); 231 179 232 if (restaurantList.isEmpty()) { 180 233 restaurantList = restaurantRepository.findAllByCuisineTypeContaining(search); 181 234 } 182 return restaurantList; 183 } 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; 184 248 } 185 249 … … 190 254 191 255 @Override 192 public List<Restaurant> findRestaurantsByCuisineType(String param) { 193 return restaurantRepository.findAllByCuisineType(param); 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); 194 271 } 195 272 }
Note:
See TracChangeset
for help on using the changeset viewer.