Skip to content

Commit

Permalink
Merge pull request #68 from shortboy7/fix/issue67/Fix-Facility-Inquery
Browse files Browse the repository at this point in the history
Feat: #67 시설물 조회 로직 오류 수정
  • Loading branch information
pletain authored Dec 5, 2023
2 parents a67e21e + bb626b0 commit d0b9e01
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public ResponseEntity<NextReservationResponse> getNextReservation(@PathVariable
}

//특정 시설물 이용상태 확인 기능
@GetMapping("/status/{facilityCode}")
public ResponseEntity<String> getFacilityStatus(@PathVariable String facilityCode){
FacilityStatus facilityStatus = facilityService.getFacilityStatus(facilityCode);
@GetMapping("/status/{buildingName}/{facilityCode}")
public ResponseEntity<String> getFacilityStatus(@PathVariable String buildingName, @PathVariable String facilityCode){
FacilityStatus facilityStatus = facilityService.getFacilityStatus(facilityCode, buildingName);

HttpStatus successStatus = SUCCESS_FACILITY_LOOKUP.getStatus();

return new ResponseEntity(facilityStatus, successStatus);
}

//시설물 이용 종료 기능
@GetMapping("/finish/{facilityCode}")
public ResponseEntity<String> endFacility(@PathVariable String facilityCode){
facilityService.endFacility(facilityCode);
@GetMapping("/finish/{buildingName}/{facilityCode}")
public ResponseEntity<String> endFacility(@PathVariable String buildingName, @PathVariable String facilityCode){
facilityService.endFacility(facilityCode, buildingName);

String successMessage = SUCCESS_FACILITY_FINISH.getMessage();
HttpStatus successStatus = SUCCESS_FACILITY_FINISH.getStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import backend.spectrum.dguonoff.DAO.Facility;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import backend.spectrum.dguonoff.DAO.Reservation;
import backend.spectrum.dguonoff.DAO.model.FacilityStatus;
import backend.spectrum.dguonoff.domain.reservation.dto.constraint.DateConstraint;
import backend.spectrum.dguonoff.domain.reservation.dto.constraint.MaxReservationConstraint;
Expand All @@ -20,27 +17,28 @@

@Repository
public interface FacilityRepository extends JpaRepository<Facility, Long> {
@Query("select f from Facility f where f.code = ?1 AND f.building.name = ?2")

@Query("select f from Facility f where f.code = ?1 and f.building.name = ?2")
Optional<Facility> findByCodeAndBuilding(String code, String buildingName);
List<Facility> findAllByBuilding_NameAndFloorAndIsBookable(String buildingName, Integer floor, Boolean bookable);
List<Facility> findAllByBuilding_NameAndFloor(String buildingName, Integer floor);
Optional<Facility> findByBuilding_NameAndCode(String buildingName, String code);

@Query("select new backend.spectrum.dguonoff.domain.reservation.dto.constraint.DateConstraint(f.category.reservationStart, f.category.reservationDeadline) from Facility f where f.code = ?1")
Optional<DateConstraint> findDateConstraint(@NonNull String facilityCode);
@Query("select new backend.spectrum.dguonoff.domain.reservation.dto.constraint.DateConstraint(f.category.reservationStart, f.category.reservationDeadline) from Facility f where f.code = ?1 and f.building.name = ?2")
Optional<DateConstraint> findDateConstraint(@NonNull String facilityCode, @NonNull String buildingName);

@Query("select new backend.spectrum.dguonoff.domain.reservation.dto.constraint.MaxReservationConstraint(f.category.maxReservation, f.category.maxReservationPeriod, f.category.name) from Facility f where f.code = ?1")
Optional<MaxReservationConstraint> findReservationConstraint(@NonNull String facilityCode);
@Query("select new backend.spectrum.dguonoff.domain.reservation.dto.constraint.MaxReservationConstraint(f.category.maxReservation, f.category.maxReservationPeriod, f.category.name) from Facility f where f.code = ?1 and f.building.name = ?2")
Optional<MaxReservationConstraint> findReservationConstraint(@NonNull String facilityCode, @NonNull String buildingName);

@Query("select new backend.spectrum.dguonoff.domain.reservation.dto.constraint.UsageConstraint(f.category.max_time, f.category.minUser, f.capacity) from Facility f where f.code = ?1")
Optional<UsageConstraint> findFacilityUsageConstraint(String facilityCode);
@Query("select new backend.spectrum.dguonoff.domain.reservation.dto.constraint.UsageConstraint(f.category.max_time, f.category.minUser, f.capacity) from Facility f where f.code = ?1 and f.building.name = ?2")
Optional<UsageConstraint> findFacilityUsageConstraint(@NonNull String facilityCode, @NonNull String buildingName);

@Query("select f.state from Facility f where f.code = ?1")
Optional<FacilityStatus> findStatusByFacilityCode(String facilityCode);
Optional<FacilityStatus> findStatusByFacilityCode(@NonNull String facilityCode, @NonNull String buildingName);

@Transactional
@Modifying
@Query("update Facility f set f.state = ?1 where f.code = ?2")
int updateFacilityStatus(FacilityStatus state, String facilityCode);
@Query("update Facility f set f.state = ?1 where f.code = ?2 and f.building.name = ?3")
int updateFacilityStatus(@NonNull FacilityStatus state, @NonNull String facilityCode, @NonNull String buildingName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@ private static List<FacilityOutlineDTO> getFacilityOutlines(String buildingName,
}

//시설물의 이용 제약조건을 반환하는 함수
public UsageConstraint getUsageConstraint(String facilityCode) {
UsageConstraint constraint = facilityRepository.findFacilityUsageConstraint(facilityCode)
public UsageConstraint getUsageConstraint(String facilityCode, String buildingName) {
UsageConstraint constraint = facilityRepository.findFacilityUsageConstraint(facilityCode, buildingName)
.orElseThrow(() -> new FacilityNotFoundException(ErrorCode.NOT_EXIST_FACILITY));
return constraint;
}

//특정 시설물의 이용상태 반환 함수
public FacilityStatus getFacilityStatus(String facilityCode) {
FacilityStatus status = facilityRepository.findStatusByFacilityCode(facilityCode)
public FacilityStatus getFacilityStatus(String facilityCode, String buildingName) {
FacilityStatus status = facilityRepository.findStatusByFacilityCode(facilityCode, buildingName)
.orElseThrow(() -> new FacilityNotFoundException(ErrorCode.NOT_EXIST_FACILITY));
return status;
}

//시설물 이용 상태 종료 함수
public void endFacility(String facilityCode) {
facilityRepository.updateFacilityStatus(FacilityStatus.EMPTY, facilityCode);
public void endFacility(String facilityCode, String buildingName) {
facilityRepository.updateFacilityStatus(FacilityStatus.EMPTY, facilityCode, buildingName);
}

//시설물의 다음 예약 정보를 반환하는 함수
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ public ResponseEntity<String> rejectReservation(@PathVariable Long reservationId


//예약 가능 확인하기 기능
@GetMapping("/available/{facilityCode}/{date}")
public ResponseEntity<AvailabilityResponse> checkReservationAvailability(@PathVariable String facilityCode, @PathVariable String date, Principal principal){
@GetMapping("/available/{buildingName}/{facilityCode}/{date}")
public ResponseEntity<AvailabilityResponse> checkReservationAvailability(@PathVariable String buildingName, @PathVariable String facilityCode, @PathVariable String date, Principal principal){
String userId = principal.getName();
LocalDate parsedDate = LocalDate.parse(date);

//예약 신청 가능 기간 검증
DateConstraint constraint = reservationService.getAvailableDate(facilityCode);
DateConstraint constraint = reservationService.getAvailableDate(facilityCode, buildingName);

//최대 예약 횟수 초과 검증
reservationService.validateMaxReservation(facilityCode, parsedDate, userId);
reservationService.validateMaxReservation(facilityCode, buildingName, parsedDate, userId);

HttpStatus successStatus = AVAILABLE_FACILITY.getStatus();
String successMessage = AVAILABLE_FACILITY.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public interface ReservationRepository extends JpaRepository<Reservation, Long>
Optional<List<Reservation>> findReservationsAfterYesterday(String code, LocalDate date, ReservationStatus status);
@Query("select r.event from Reservation r where r.reservationId = ?1")
Optional<Event> findEventById(Long reservationId);
@Query("select r from Reservation r where r.facility.code = ?1 and r.date = ?2")
List<Reservation> findByFacilityCodeAndDate(String code, LocalDate date);
@Query("select r from Reservation r where r.facility.code = ?1 and r.facility.building.name = ?2 and r.date = ?3")
List<Reservation> findByFacilityCodeAndDate(String code, String buildingName, LocalDate date);

@Query("select count(r) from Reservation r " +
"where r.hotUserId.id = ?1 and r.date between ?2 and ?3 and r.facility.category.name = ?4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public class ReservationService {


//예약할 수 있는 날짜 기준일을 반환해주는 함수
public DateConstraint getAvailableDate(String facilityCode) {
public DateConstraint getAvailableDate(String facilityCode, String buildingName) {
DateConstraint constraint = facilityRepository
.findDateConstraint(facilityCode)
.findDateConstraint(facilityCode, buildingName)
.orElseThrow(
() -> new FacilityNotFoundException(ErrorCode.NOT_EXIST_FACILITY));
return constraint;
}

//시설물 이용 횟수 초과를 확인하는 함수
public void validateMaxReservation(String facilityCode, LocalDate date, String userId) {
public void validateMaxReservation(String facilityCode, String buildingName, LocalDate date, String userId) {

//관리자인 경우 이용 횟수 초과를 확인하지 않음
if (!userRepository.findById(userId).get().getRole().equals(NORMAL)) {
Expand All @@ -86,7 +86,7 @@ public void validateMaxReservation(String facilityCode, LocalDate date, String u

//시설물의 예약 제한 정보를 가져옴
MaxReservationConstraint constraint = facilityRepository
.findReservationConstraint(facilityCode)
.findReservationConstraint(facilityCode, buildingName)
.orElseThrow(
() -> new FacilityNotFoundException(ErrorCode.NOT_EXIST_FACILITY));

Expand Down Expand Up @@ -149,11 +149,11 @@ public void registerReservation(ReservationRequest reservationRequest, String us


//예약 유효성 검사
validateMaxReservation(facilityCode, reservationDate, userId); // 최대 예약 횟수를 초과한 경우 예외 발생
validateAvailableDate(reservationDate, facilityCode); //예약 가능 날짜인지 검사
validateMaxReservation(facilityCode, buildingName, reservationDate, userId); // 최대 예약 횟수를 초과한 경우 예외 발생
validateAvailableDate(reservationDate, facilityCode, buildingName); //예약 가능 날짜인지 검사
validateTime(startTime, endTime); //예약 시간이 유효한지 검사
validateTimeConflict(startTime, endTime, facilityCode, reservationDate); //다른 예약과 시간이 곂치는지 검사
validateUsageConstraint(startTime, endTime, facilityCode, guestList); //시설물 이용 제한을 위반한 경우 예외 발생
validateTimeConflict(startTime, endTime, facilityCode, buildingName,reservationDate); //다른 예약과 시간이 곂치는지 검사
validateUsageConstraint(startTime, endTime, facilityCode, buildingName, guestList); //시설물 이용 제한을 위반한 경우 예외 발생

//예약자의 권한에 따라 예약 상태 설정
if(user.getRole().equals(NORMAL)) {
Expand Down Expand Up @@ -388,8 +388,8 @@ private void validateTime(LocalTime startTime, LocalTime endTime) {
}

//예약 가능 날짜인지 검사하는 함수
private void validateAvailableDate(LocalDate reservationDate, String facilityCode) {
DateConstraint availableDate = getAvailableDate(facilityCode);
private void validateAvailableDate(LocalDate reservationDate, String facilityCode, String buildingName) {
DateConstraint availableDate = getAvailableDate(facilityCode, buildingName);
LocalDate currentDate = LocalDate.now();

LocalDate maxDate = reservationDate.minusDays(availableDate.getMaxDate());
Expand All @@ -401,8 +401,8 @@ private void validateAvailableDate(LocalDate reservationDate, String facilityCod
}

//기존 예약과 시간이 곂치는지 검증하는 함수
private void validateTimeConflict(LocalTime startTime, LocalTime endTime, String facilityCode, LocalDate reservationDate) {
List<Reservation> reservationList = reservationRepository.findByFacilityCodeAndDate(facilityCode, reservationDate);
private void validateTimeConflict(LocalTime startTime, LocalTime endTime, String facilityCode, String buildingName, LocalDate reservationDate) {
List<Reservation> reservationList = reservationRepository.findByFacilityCodeAndDate(facilityCode, buildingName, reservationDate);
for (Reservation reservation : reservationList) {
if (startTime.isBefore(reservation.getEndTime()) && endTime.isAfter(reservation.getStartTime())) {
throw new InvalidReservationException(ErrorCode.TIME_CONFLICT); //다른 예약과 시간이 곂치는 경우 예외 발생
Expand All @@ -411,8 +411,8 @@ private void validateTimeConflict(LocalTime startTime, LocalTime endTime, String
}

//시설물 이용 제한을 위반했는지 검증하는 함수
private void validateUsageConstraint(LocalTime startTime, LocalTime endTime, String facilityCode, List<String> guestList) {
UsageConstraint usageConstraint = facilityService.getUsageConstraint(facilityCode);
private void validateUsageConstraint(LocalTime startTime, LocalTime endTime, String facilityCode, String buildingName, List<String> guestList) {
UsageConstraint usageConstraint = facilityService.getUsageConstraint(facilityCode, buildingName);
long usageTime = Duration.between(startTime, endTime).toHours();

//예약 유효성 검사: 최대 이용 시간, 최대 이용 인원, 최소 이용 인원을 초과한 경우 예외 발생
Expand Down Expand Up @@ -442,10 +442,10 @@ public void updateFacilityStatus() {
if(reservationInfo.getDate().isEqual(currentDate)) { //예약 날짜가 현재 날짜와 같은 경우
if(reservationInfo.getStartTime().isBefore(currentTime) && reservationInfo.getEndTime().isAfter(currentTime)) { //예약 시간이 현재 시간 사이인 경우
//예약된 시설물의 상태를 사용중으로 변경
facilityRepository.updateFacilityStatus(USING, reservationInfo.getFacilityCode());
facilityRepository.updateFacilityStatus(USING, reservationInfo.getFacilityCode(), reservationInfo.getBuildingName());
} else if(reservationInfo.getEndTime().isBefore(currentTime)) { //예약 시간이 현재 시간보다 이른 경우
//예약된 시설물의 상태를 사용 가능으로 변경
facilityRepository.updateFacilityStatus(EMPTY, reservationInfo.getFacilityCode());
facilityRepository.updateFacilityStatus(EMPTY, reservationInfo.getFacilityCode(), reservationInfo.getBuildingName());
}
}
}
Expand Down

0 comments on commit d0b9e01

Please sign in to comment.