|
9 | 9 | import kattsyn.dev.rentplace.enums.Role; |
10 | 10 | import kattsyn.dev.rentplace.exceptions.ForbiddenException; |
11 | 11 | import kattsyn.dev.rentplace.exceptions.NotFoundException; |
| 12 | +import kattsyn.dev.rentplace.exceptions.ValidationException; |
12 | 13 | import kattsyn.dev.rentplace.mappers.ReservationMapper; |
13 | 14 | import kattsyn.dev.rentplace.repositories.ReservationRepository; |
14 | 15 | import kattsyn.dev.rentplace.services.ReservationService; |
|
17 | 18 | import org.springframework.beans.factory.annotation.Value; |
18 | 19 | import org.springframework.stereotype.Service; |
19 | 20 |
|
| 21 | +import java.time.LocalDate; |
| 22 | +import java.time.temporal.ChronoUnit; |
20 | 23 | import java.util.List; |
21 | 24 |
|
22 | 25 | @RequiredArgsConstructor |
@@ -102,16 +105,26 @@ public ReservationDTO deleteById(long reservationId) { |
102 | 105 | return reservationMapper.fromReservation(reservation); |
103 | 106 | } |
104 | 107 |
|
105 | | - /* |
106 | | - Поменять метод, чтобы нельзя было иметь отрицательный период бронирования (если разные года аренды) |
107 | | - */ |
108 | | - private int countRentPrice(Reservation reservation) { |
| 108 | + |
| 109 | + public int countRentPrice(Reservation reservation) { |
| 110 | + LocalDate startDate = reservation.getStartDate(); |
| 111 | + LocalDate endDate = reservation.getEndDate(); |
| 112 | + |
| 113 | + if (startDate.isBefore(LocalDate.now())) { |
| 114 | + throw new ValidationException("Reservations start date can't be before now."); |
| 115 | + } |
| 116 | + |
| 117 | + if (endDate.isBefore(startDate)) { |
| 118 | + throw new ValidationException("End date can't be before start date."); |
| 119 | + } |
| 120 | + |
109 | 121 | int period; |
110 | 122 | if (reservation.isLongTermRent()) { |
111 | | - period = reservation.getEndDate().getMonthValue() - reservation.getStartDate().getMonthValue(); |
| 123 | + period = (int) ChronoUnit.MONTHS.between(startDate, endDate); |
112 | 124 | } else { |
113 | | - period = reservation.getEndDate().getDayOfYear() - reservation.getStartDate().getDayOfYear(); |
| 125 | + period = (int) ChronoUnit.DAYS.between(startDate, endDate); |
114 | 126 | } |
| 127 | + |
115 | 128 | return period * reservation.getCostInPeriod(); |
116 | 129 | } |
117 | 130 |
|
|
0 commit comments