Ignore:
Timestamp:
04/28/25 14:21:17 (3 weeks ago)
Author:
Aleksandar Panovski <apano77@…>
Branches:
main
Children:
e15e8d9
Parents:
f5b256e
Message:

Big change done fully handle_reservation_update() trigger works

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/rezevirajmasa/demo/service/impl/RestaurantServiceImpl.java

    rf5b256e rdeea3c4  
    11package com.example.rezevirajmasa.demo.service.impl;
    22
     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;
    37import com.example.rezevirajmasa.demo.model.Restaurant;
    48import com.example.rezevirajmasa.demo.model.TableEntity;
     9import com.example.rezevirajmasa.demo.model.User;
    510import com.example.rezevirajmasa.demo.model.exceptions.InvalidRestaurantIdException;
    611import com.example.rezevirajmasa.demo.repository.RestaurantRepository;
    712import com.example.rezevirajmasa.demo.repository.TableRepository;
     13import com.example.rezevirajmasa.demo.service.ReservationService;
    814import com.example.rezevirajmasa.demo.service.RestaurantService;
    915import com.example.rezevirajmasa.demo.service.TableService;
    1016import com.sun.tools.jconsole.JConsoleContext;
    1117import jakarta.transaction.Transactional;
     18import org.openqa.selenium.InvalidArgumentException;
    1219import org.springframework.stereotype.Service;
    13 
     20import org.modelmapper.ModelMapper;
    1421import java.time.LocalDate;
    1522import java.time.LocalDateTime;
     
    3138    private final TableRepository tableRepository;
    3239    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) {
    3544        this.restaurantRepository = restaurantRepository;
    3645        this.tableRepository = tableRepository;
    3746        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() {
    4278        return restaurantRepository.findAll();
    4379    }
     
    5187    @Override
    5288    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
    5393        restaurantRepository.save(restaurant);
    5494        String[] hours = restaurant.getOperatingHours().split("-");
     
    60100            LocalTime startTime = LocalTime.parse(hours[0], DateTimeFormatter.ofPattern("HH:mm"));
    61101            LocalTime endTime = LocalTime.parse(hours[1], DateTimeFormatter.ofPattern("HH:mm"));
    62             System.out.println("IsBefore: " + startTime.isBefore(endTime) + " and equals: " + startTime.equals(endTime));
    63102
    64103            for (int i = 0; i < numberOfTables; i++) {
    65104                TableEntity table = new TableEntity();
    66 
    67 //                table.initializeTimeSlots(startTime, endTime);
    68 
    69105                table.setCapacity(tableCapacities.get(i));
    70106                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"));
    74108                table.setDescription(tableDescriptions.get(i));
    75109                table.setRestaurant(restaurant);
    76 
    77110                tableRepository.save(table);
    78111            }
     
    84117    }
    85118    @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
    89149
    90150    @Override
     
    115175    @Override
    116176    public List<Restaurant> listRestaurantBy(String search) {
    117         // Get all restaurants from the repository
    118177        List<Restaurant> allRestaurants = restaurantRepository.findAll();
    119178
     
    134193
    135194        for (Restaurant restaurant : restaurants) {
    136             // Check if the restaurant has available time slots for today
    137195            boolean hasAvailableTimeSlots = tableService.hasAvailableTimeSlotsForRestaurantAndDate(restaurant, today);
    138196            if (hasAvailableTimeSlots) {
     
    146204    @Override
    147205    public List<Restaurant> findRestaurantsByDateTimeAndPartySize(LocalDateTime dateTime, int partySize, String search) {
     206        LocalDateTime checkOutTime = dateTime.plusHours(2);
    148207        List<Restaurant> allRestaurants = restaurantRepository.findAll();
     208
    149209        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)))
    151213                .filter(restaurant -> isMatch(restaurant, search))
    152214                .collect(Collectors.toList());
    153215    }
    154216
    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 
    164217    private boolean isMatch(Restaurant restaurant, String name) {
    165218        return name == null || name.isEmpty() || restaurant.getName().contains(name);
     
    167220
    168221    @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;
    170226        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();
    176228        } else {
    177229            String searchTerm = "%" + search + "%";
    178             List<Restaurant> restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(searchTerm);
     230            restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(searchTerm);
     231
    179232            if (restaurantList.isEmpty()) {
    180233                restaurantList = restaurantRepository.findAllByCuisineTypeContaining(search);
    181234            }
    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;
    184248    }
    185249
     
    190254
    191255    @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);
    194271    }
    195272}
Note: See TracChangeset for help on using the changeset viewer.