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/TableServiceImpl.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;
    5 import com.example.rezevirajmasa.demo.model.exceptions.InvalidRestaurantIdException;
    69import com.example.rezevirajmasa.demo.model.exceptions.InvalidTableNumberException;
     10import com.example.rezevirajmasa.demo.repository.ReservationRepository;
    711import com.example.rezevirajmasa.demo.repository.RestaurantRepository;
    812import com.example.rezevirajmasa.demo.repository.TableRepository;
    913import com.example.rezevirajmasa.demo.service.TableService;
     14import jakarta.persistence.Table;
     15import org.modelmapper.ModelMapper;
     16import org.openqa.selenium.InvalidArgumentException;
    1017import org.springframework.cglib.core.Local;
    1118import org.springframework.stereotype.Service;
     
    1825import java.time.chrono.ChronoLocalDateTime;
    1926import java.time.format.DateTimeFormatter;
     27import java.util.ArrayList;
    2028import java.util.Iterator;
    2129import java.util.List;
     
    2533    private final TableRepository tableRepository;
    2634    private final RestaurantRepository restaurantRepository;
     35    private final ReservationRepository reservationRepository;
     36    private ModelMapper modelMapper = new ModelMapper();
    2737
    28     public TableServiceImpl(TableRepository tableRepository, RestaurantRepository restaurantRepository) {
     38    public TableServiceImpl(TableRepository tableRepository, RestaurantRepository restaurantRepository, ReservationRepository reservationRepository) {
    2939        this.tableRepository = tableRepository;
    3040        this.restaurantRepository = restaurantRepository;
    31     }
    32 
    33     @Override
    34     public TableEntity findById(Long id) {
    35         return tableRepository.findById(id).orElseThrow(InvalidTableNumberException::new);
     41        this.reservationRepository = reservationRepository;
    3642    }
    3743
     
    4147    }
    4248
     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
    4374    public void save(int numberOfTables, List<Integer> tableCapacities, List<String> tableLocations, List<String> tableSmokingAreas, List<String> tableDescriptions, Restaurant restaurant) {
    4475        for (int i = 0; i < numberOfTables; i++) {
     
    5586    @Override
    5687    public void deleteTimeSlotsForReservation(Long tableId, LocalDateTime reservationTime) {
    57         LocalDateTime startTime = reservationTime.minusHours(2);
    58         LocalDateTime endTime = reservationTime.plusHours(2);
    59 
    60         TableEntity table = findById(tableId);
    61         List<LocalDateTime> timeSlots = table.getTimeSlots();
    62 
    63         timeSlots.removeIf(timeSlot -> timeSlot.isAfter(startTime) && timeSlot.isBefore(endTime));
    64 
    65         table.setTimeSlots(timeSlots);
     88        TableDTO tableDTO = findById(tableId);
     89        TableEntity table = convertFromDTO(tableDTO);
     90        LocalDate threeDaysAgo = LocalDate.now().minusDays(3);
     91        table.cleanUnusedTimeSlots(threeDaysAgo);
    6692
    6793        tableRepository.saveAndFlush(table);
     
    7096    @Override
    7197    public void canceledTimeSlots(Long tableId, LocalDateTime reservationTime) {
    72         TableEntity table = findById(tableId);
    73         List<LocalDateTime> timeSlots = table.getTimeSlots();
     98        TableDTO tableDTO = findById(tableId);
     99        TableEntity table = convertFromDTO(tableDTO);
    74100        LocalDateTime startTime = reservationTime.minusHours(1).minusMinutes(45);
    75101        LocalDateTime endTime = reservationTime.plusHours(1).plusMinutes(45);
    76102
    77         LocalDate localDate = reservationTime.toLocalDate();
    78 
    79103        String[] hours = table.getRestaurant().getOperatingHours().split("-");
    80104        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());
    81107
    82         LocalDateTime openingHour = openingHourTime.atDate(localDate);
    83         LocalDateTime closingHour = LocalTime.of(23, 45).atDate(localDate);
    84         if(startTime.isBefore(openingHour)) {
    85             startTime = LocalDateTime.from(openingHour);
     108        if (startTime.isBefore(openingHour)) {
     109            startTime = openingHour;
    86110        }
    87         while(startTime.isBefore(endTime) || startTime.equals(endTime)) {
    88             timeSlots.add(startTime);
    89             startTime = startTime.plusMinutes(15);
    90             if(startTime.isAfter(closingHour) || startTime.equals(closingHour)) {
     111
     112        while (startTime.isBefore(endTime) || startTime.equals(endTime)) {
     113            if (startTime.isAfter(closingHour) || startTime.equals(closingHour)) {
    91114                break;
    92115            }
     116
     117            Reservation reservation = new Reservation();
     118            reservation.setReservationDateTime(startTime);
     119            reservation.setTable(table);
     120            reservationRepository.save(reservation);
     121
     122            startTime = startTime.plusMinutes(15);
    93123        }
    94         table.setTimeSlots(timeSlots);
     124
    95125        tableRepository.saveAndFlush(table);
    96126    }
     
    105135        return tableRepository.findById(number).orElseThrow(InvalidTableNumberException::new);
    106136    }
     137
    107138    @Override
    108139    public TableEntity deleteTable(Long number) {
     
    116147        List<TableEntity> tables = tableRepository.findByRestaurant(restaurant);
    117148
    118         // Iterate through each table to check for available time slots
    119149        for (TableEntity table : tables) {
    120150            boolean hasAvailableTimeSlots = hasAvailableTimeSlotsForTableAndDate(table, today);
    121151            if (hasAvailableTimeSlots) {
    122                 return true; // If any table has available time slots, return true
     152                return true;
    123153            }
    124154        }
     
    128158
    129159    public boolean hasAvailableTimeSlotsForTableAndDate(TableEntity table, LocalDate date) {
    130         List<LocalDateTime> timeSlots = table.getTimeSlots();
     160        for (Reservation reservation : table.getReservations()) {
     161            LocalDateTime startTime = reservation.getReservationDateTime();
     162            LocalDateTime endTime = startTime.plusHours(reservation.getTable().getReservationDurationHours());
    131163
    132         // Implement your logic to check if the table has available time slots for the given date
    133         // This could involve querying the database for reservations for the given table and date,
    134         // and checking if there are any open time slots.
    135         // You may need to consider factors such as operating hours, existing reservations, etc.
    136         // Return true if there are available time slots, false otherwise.
    137         return false;
     164            if (startTime.toLocalDate().equals(date) || endTime.toLocalDate().equals(date)) {
     165                return false;
     166            }
     167        }
     168        return true;
    138169    }
    139170
     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    }
    140186}
Note: See TracChangeset for help on using the changeset viewer.