Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/NewFit/NewFit-Backend in…
Browse files Browse the repository at this point in the history
…to release/dev
  • Loading branch information
Sangwook02 committed Jan 2, 2024
2 parents a207dbd + 1a34f59 commit 3847843
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ReservationRequest {

@Future
private LocalDateTime startAt;

@Future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
public class ReservationUpdateRequest {
@Min(1)
private Long equipmentGymId;
@Future

private LocalDateTime startAt;

@Future
private LocalDateTime endAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ List<Reservation> findAllByAuthorityIdAndStartAtAndEndAt(@Param("authorityId") L
List<Reservation> findAllByEquipmentGymIdIdAndStartAtAndEndAt(@Param("equipmentGymId") Long equipmentGymId,
@Param("startAt") LocalDateTime startAt,
@Param("endAt") LocalDateTime endAt);

@Query(value = "select r.* from Reservation r " +
"join authority a " +
"on a.id=:authorityId " +
"where ( " +
"(:startAt BETWEEN r.start_at AND r.end_at) " +
"OR (:endAt BETWEEN r.start_at AND r.end_at) " +
"OR (:startAt <= r.start_at AND r.end_at <= :endAt)) " +
"AND (r.id <> :reservationId);", nativeQuery = true)
List<Reservation> findAllByAuthorityIdAndStartAtAndEndAtExcludingCurrent(@Param("authorityId") Long authorityId,
@Param("startAt") LocalDateTime startAt,
@Param("endAt") LocalDateTime endAt,
@Param("reservationId") Long reservationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void reserve(Long authorityId,
LocalDateTime endAt = request.getEndAt();

validateTime(startAt, endAt, authority);
validateMyReservationOverlap(authority, startAt, endAt);

EquipmentGym equipmentGym = equipmentGymRepository.findAvailableByEquipmentGymIdAndStartAtAndEndAt(
equipmentGymId, startAt, endAt)
Expand All @@ -58,12 +59,6 @@ public void reserve(Long authorityId,
reservationRepository.save(reservation);
}

private void validateLastTagAt(Authority authority) {
LocalDateTime tagAt = authority.getTagAt();
if (tagAt.isBefore(now().minusHours(2)))
throw new CustomException(EXPIRED_TAG);
}

@Transactional(readOnly = true)
public ReservationListResponse listReservation(Long equipmentGymId) {
EquipmentGym equipmentGym = equipmentGymRepository.findById(equipmentGymId)
Expand All @@ -87,6 +82,7 @@ public void update(Long reservationId, ReservationUpdateRequest request) {
LocalDateTime endAt = request.getEndAt();

validateTime(startAt, endAt, authority);
validateMyReservationOverlapExcludingCurrent(authority, startAt, endAt, reservationId);

if (equipmentGymId == null) {
targetReservation.updateTime(startAt, endAt);
Expand Down Expand Up @@ -114,13 +110,24 @@ private void validateUpdateTime(Long equipmentGymId, LocalDateTime startAt, Loca
}

private void validateTime(LocalDateTime startAt, LocalDateTime endAt, Authority authority) {
validateStartAtLowerBound(startAt);
validateLastTagAt(authority);
validateReservationIn2Hours(startAt, endAt);

Gym gym = authority.getGym();
gym.checkBusinessHour(startAt, endAt);
}

validateMyReservationOverlap(authority, startAt, endAt);
private void validateStartAtLowerBound(LocalDateTime startAt) {
if (startAt.isBefore(now().minusMinutes(1))) {
throw new CustomException(INVALID_RESERVATION_REQUEST);
}
}

private void validateLastTagAt(Authority authority) {
LocalDateTime tagAt = authority.getTagAt();
if (tagAt.isBefore(now().minusHours(2)))
throw new CustomException(EXPIRED_TAG);
}

private void validateMyReservationOverlap(Authority authority, LocalDateTime startAt, LocalDateTime endAt) {
Expand All @@ -131,6 +138,17 @@ private void validateMyReservationOverlap(Authority authority, LocalDateTime sta
}
}

private void validateMyReservationOverlapExcludingCurrent(Authority authority, LocalDateTime startAt,
LocalDateTime endAt,
Long reservationId) {
List<Reservation> reservations = reservationRepository.findAllByAuthorityIdAndStartAtAndEndAtExcludingCurrent(
authority.getId(),
startAt, endAt, reservationId);
if (!reservations.isEmpty()) {
throw new CustomException(INVALID_RESERVATION_REQUEST);
}
}

public void delete(Long reservationId) {
reservationRepository.deleteById(reservationId);
}
Expand Down

0 comments on commit 3847843

Please sign in to comment.