- Timestamp:
- 01/19/25 23:18:37 (4 months ago)
- Branches:
- main
- Children:
- f5b256e
- Parents:
- db39d9e
- Location:
- src/main/java/com/example/rezevirajmasa/demo/service/impl
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/rezevirajmasa/demo/service/impl/ReservationHistoryServiceImpl.java
rdb39d9e r8ca35dc 4 4 import com.example.rezevirajmasa.demo.model.Reservation; 5 5 import com.example.rezevirajmasa.demo.model.Restaurant; 6 import com.example.rezevirajmasa.demo.model.User; 6 7 import com.example.rezevirajmasa.demo.repository.ReservationHistoryRepository; 7 8 import com.example.rezevirajmasa.demo.service.ReservationHistoryService; … … 22 23 public void moveReservationToHistory(Reservation reservation, String status, String cancellationReason) { 23 24 Restaurant.ReservationHistory history = new Restaurant.ReservationHistory(); 24 history.set Customer(reservation.getCustomer());25 history.setUser(reservation.getUser()); 25 26 history.setTable(reservation.getTable()); 26 27 history.setReservationDateTime(reservation.getReservationDateTime()); … … 42 43 43 44 @Override 44 public List<Restaurant.ReservationHistory> findBy Customer(Customer customer) {45 return reservationHistoryRepository.findA llByCustomer(customer);45 public List<Restaurant.ReservationHistory> findByUser(User user) { 46 return reservationHistoryRepository.findALlByUser(user); 46 47 } 47 48 48 49 @Override 49 50 public void moveReservationsToPast(List<Restaurant.ReservationHistory> reservationsToMove) { 50 // Update the status of reservations to "Past"51 51 for (Restaurant.ReservationHistory reservation : reservationsToMove) { 52 52 reservation.setStatus("Past"); 53 53 } 54 54 55 // Save the updated reservations56 55 reservationHistoryRepository.saveAll(reservationsToMove); 57 56 } -
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 -
src/main/java/com/example/rezevirajmasa/demo/service/impl/RestaurantServiceImpl.java
rdb39d9e r8ca35dc 175 175 .collect(Collectors.toList()); 176 176 } else { 177 List<Restaurant> restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(search); 177 String searchTerm = "%" + search + "%"; 178 List<Restaurant> restaurantList = restaurantRepository.findAllByNameLikeIgnoreCase(searchTerm); 178 179 if (restaurantList.isEmpty()) { 179 180 restaurantList = restaurantRepository.findAllByCuisineTypeContaining(search); -
src/main/java/com/example/rezevirajmasa/demo/service/impl/UserServiceImpl.java
rdb39d9e r8ca35dc 6 6 import com.example.rezevirajmasa.demo.mappers.UserMapper; 7 7 import com.example.rezevirajmasa.demo.mappers.UserMapperImpl; 8 import com.example.rezevirajmasa.demo.model.MembershipLevel; 9 import com.example.rezevirajmasa.demo.model.Role; 8 10 import com.example.rezevirajmasa.demo.model.User; 9 11 import com.example.rezevirajmasa.demo.model.exceptions.AppException; … … 12 14 import lombok.RequiredArgsConstructor; 13 15 import org.mapstruct.control.MappingControl; 16 import org.openqa.selenium.InvalidArgumentException; 14 17 import org.springframework.http.HttpStatus; 15 18 import org.springframework.security.crypto.password.PasswordEncoder; 16 19 import org.springframework.stereotype.Service; 17 20 import java.nio.CharBuffer; 21 import java.sql.Date; 22 import java.time.LocalDate; 18 23 import java.util.Optional; 19 24 … … 30 35 } 31 36 37 @Override 38 public User findByMail(String email) { 39 return userRepository.findByEmail(email).orElseThrow(() -> new AppException(("Unknown user"), HttpStatus.NOT_FOUND)); 40 } 41 32 42 public UserDto login(CredentialsDto credentialsDto) { 33 User user = userRepository.findByEmail(credentialsDto.get Login())43 User user = userRepository.findByEmail(credentialsDto.getEmail()) 34 44 .orElseThrow(() -> new AppException("Unknown user", HttpStatus.NOT_FOUND)); 35 45 … … 51 61 user.setPassword(passwordEncoder.encode(CharBuffer.wrap(userDto.getPassword()))); 52 62 63 user.setFirstName(userDto.getFirstName()); 64 user.setLastName(userDto.getLastName()); 65 user.setAddress(userDto.getAddress()); 66 user.setPhone(userDto.getPhone()); 67 user.setMembershipLevel(MembershipLevel.valueOf(userDto.getMembershipLevel())); 68 user.setRegistrationDate(Date.valueOf(LocalDate.now())); 69 user.setRole(Role.ROLE_USER); 70 53 71 User savedUser = userRepository.save(user); 54 72 55 return userMapper.toUserDto(user); 73 return userMapper.toUserDto(savedUser); 74 } 75 76 @Override 77 public User findUserById(Long userId) { 78 return userRepository.findById(userId).orElseThrow(()->new InvalidArgumentException("Invalid user Id")); 56 79 } 57 80 }
Note:
See TracChangeset
for help on using the changeset viewer.