Skip to content

Commit 6219e5f

Browse files
committed
feat: change countRentPrice method
1 parent f51db80 commit 6219e5f

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/ReservationServiceImpl.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import kattsyn.dev.rentplace.enums.Role;
1010
import kattsyn.dev.rentplace.exceptions.ForbiddenException;
1111
import kattsyn.dev.rentplace.exceptions.NotFoundException;
12+
import kattsyn.dev.rentplace.exceptions.ValidationException;
1213
import kattsyn.dev.rentplace.mappers.ReservationMapper;
1314
import kattsyn.dev.rentplace.repositories.ReservationRepository;
1415
import kattsyn.dev.rentplace.services.ReservationService;
@@ -17,6 +18,8 @@
1718
import org.springframework.beans.factory.annotation.Value;
1819
import org.springframework.stereotype.Service;
1920

21+
import java.time.LocalDate;
22+
import java.time.temporal.ChronoUnit;
2023
import java.util.List;
2124

2225
@RequiredArgsConstructor
@@ -102,16 +105,26 @@ public ReservationDTO deleteById(long reservationId) {
102105
return reservationMapper.fromReservation(reservation);
103106
}
104107

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+
109121
int period;
110122
if (reservation.isLongTermRent()) {
111-
period = reservation.getEndDate().getMonthValue() - reservation.getStartDate().getMonthValue();
123+
period = (int) ChronoUnit.MONTHS.between(startDate, endDate);
112124
} else {
113-
period = reservation.getEndDate().getDayOfYear() - reservation.getStartDate().getDayOfYear();
125+
period = (int) ChronoUnit.DAYS.between(startDate, endDate);
114126
}
127+
115128
return period * reservation.getCostInPeriod();
116129
}
117130

0 commit comments

Comments
 (0)