Changeset 8ca35dc for src/main/java/com/example/rezevirajmasa/demo/service/impl/ReservationImpl.java
- Timestamp:
- 01/19/25 23:18:37 (4 months ago)
- Branches:
- main
- Children:
- f5b256e
- Parents:
- db39d9e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/rezevirajmasa/demo/service/impl/ReservationImpl.java
rdb39d9e r8ca35dc 1 1 package com.example.rezevirajmasa.demo.service.impl; 2 2 3 import com.example.rezevirajmasa.demo.model.Customer; 4 import com.example.rezevirajmasa.demo.model.Reservation; 5 import com.example.rezevirajmasa.demo.model.Restaurant; 6 import com.example.rezevirajmasa.demo.model.TableEntity; 3 import com.example.rezevirajmasa.demo.dto.UserDto; 4 import com.example.rezevirajmasa.demo.mappers.UserMapper; 5 import com.example.rezevirajmasa.demo.model.*; 7 6 import com.example.rezevirajmasa.demo.model.exceptions.InvalidReservationException; 8 7 import com.example.rezevirajmasa.demo.model.exceptions.InvalidReservationIdException; … … 11 10 import com.example.rezevirajmasa.demo.repository.RestaurantRepository; 12 11 import com.example.rezevirajmasa.demo.repository.TableRepository; 12 import com.example.rezevirajmasa.demo.service.ReservationHistoryService; 13 13 import com.example.rezevirajmasa.demo.service.ReservationService; 14 import com.example.rezevirajmasa.demo.service.UserService; 14 15 import org.springframework.beans.factory.annotation.Autowired; 16 import org.springframework.cglib.core.Local; 17 import org.springframework.security.core.Authentication; 18 import org.springframework.security.core.annotation.AuthenticationPrincipal; 19 import org.springframework.security.core.context.SecurityContextHolder; 20 import org.springframework.security.core.userdetails.UserDetails; 15 21 import org.springframework.stereotype.Service; 22 import org.springframework.web.bind.annotation.RequestBody; 16 23 17 24 import javax.swing.text.html.Option; … … 27 34 private TableRepository tableRepository; 28 35 @Autowired 29 private RestaurantRepository restaurantRepository; 36 private ReservationHistoryService reservationHistoryService; 37 private final UserMapper userMapper; 30 38 @Autowired 31 private CustomerRepository customerRepository;39 private UserService userService; 32 40 @Autowired 33 41 private ReservationRepository reservationRepository; 42 43 public ReservationImpl(UserMapper userMapper) { 44 this.userMapper = userMapper; 45 } 34 46 35 47 @Override … … 39 51 40 52 @Override 41 public void makeReservation( Customer customer, TableEntity table, Restaurant restaurant, LocalDateTime localDateTime, LocalDateTime checkInTime, int partySize, String specialRequests) {53 public void makeReservation(User user, TableEntity table, Restaurant restaurant, LocalDateTime localDateTime, LocalDateTime checkInTime, int partySize, String specialRequests) { 42 54 if (!table.isAvailable(checkInTime)) { 43 55 // Handle unavailability (throw an exception, return a specific response, etc.) … … 46 58 47 59 Reservation reservation = 48 new Reservation( customer, table, restaurant, LocalDateTime.now(), partySize, specialRequests, "Reserved", checkInTime, checkInTime.plusHours(2), null);60 new Reservation(user, table, restaurant, LocalDateTime.now(), partySize, specialRequests, "Reserved", checkInTime, checkInTime.plusHours(2), null); 49 61 50 62 // // Update table status or perform additional logic if needed … … 56 68 57 69 @Override 58 public Reservation makeReservationRest(Reservation reservation ) {70 public Reservation makeReservationRest(Reservation reservation, User user) { 59 71 Optional<TableEntity> optionalTable = tableRepository.findById(reservation.getTable().getId()); 60 if (optionalTable.isPresent()) {72 if (optionalTable.isPresent()) { 61 73 TableEntity table = optionalTable.get(); 74 75 reservation.setUser(user); 62 76 63 77 LocalDateTime startTime = reservation.getCheckInTime().minusHours(2); … … 67 81 x -> x.isAfter(startTime) && x.isBefore(endTime) 68 82 ); 69 83 reservation.setReservationDateTime(LocalDateTime.now()); 70 84 return reservationRepository.save(reservation); 71 85 } else { 72 throw new InvalidReservationException("Unsuccessful reservation -> time slot not ava laible");86 throw new InvalidReservationException("Unsuccessful reservation -> time slot not available"); 73 87 } 74 88 } 89 75 90 76 91 @Override … … 109 124 from = from.plusMinutes(15); 110 125 } 126 reservationHistoryService.moveReservationToHistory(reservation, "Canceled", "Canceled by user"); 111 127 reservationRepository.delete(reservation); 112 return true; // Return true indicating successful cancellation128 return true; 113 129 } else { 114 return false; // Return false indicating reservation with the given ID not found130 return false; 115 131 } 116 132 } … … 118 134 119 135 @Override 120 public List<Reservation> findReservationByCustomer(Customer customer) { 121 return reservationRepository.findAllByCustomer(customer); 136 public List<Reservation> findReservationByUser(User user) { 137 LocalDateTime now = LocalDateTime.now(); 138 return reservationRepository.findALlByUserAndCheckInTimeAfter(user, now); 139 } 140 141 @Override 142 public List<Reservation> findReservationsByUserPast(User user) { 143 LocalDateTime now = LocalDateTime.now(); 144 return reservationRepository.findALlByUserAndCheckInTimeBefore(user, now); 122 145 } 123 146
Note:
See TracChangeset
for help on using the changeset viewer.