Skip to content

Commit

Permalink
Merge pull request #219 from PLADI-ALM/fix/PDS-149-getResourceBooking…
Browse files Browse the repository at this point in the history
…ByDate

[PDS-194/fix] 장비 및 차량 월별 예약 현황 조회 미사용 로직 삭제
  • Loading branch information
chaerlo127 authored Nov 21, 2023
2 parents 2a36369 + f676f5a commit 156de5e
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public interface CarBookingCustom {
boolean existsDateTime(Car car, LocalDateTime startDateTime, LocalDateTime endDateTime);

List<String> getCarBookedDate(Car car, LocalDate standardDate, LocalDate date);
List<String> getCarBookedDate(Car car, LocalDate standardDate);

ProductBookingRes findCarBookingByDate(Car car, LocalDateTime dateTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.example.pladialmserver.booking.entity.QCarBooking.carBooking;
import static com.example.pladialmserver.booking.entity.QResourceBooking.resourceBooking;

@RequiredArgsConstructor
public class CarBookingRepositoryImpl implements CarBookingCustom {
Expand Down Expand Up @@ -67,42 +64,26 @@ public ProductBookingRes findCarBookingByDate(Car car, LocalDateTime standardDat
}

@Override
public List<String> getCarBookedDate(Car car, LocalDate standardDate, LocalDate date) {
public List<String> getCarBookedDate(Car car, LocalDate standardDate) {
// 해당 월의 첫 날 (00:00)
LocalDateTime startDateTime = standardDate.withDayOfMonth(1).atStartOfDay();
// 다음 월의 첫 날 (00:00)
LocalDateTime endDateTime = standardDate.plusMonths(1).atStartOfDay();

// 이후 가장 가까운 예약 날짜&시간
if (date != null) {
// 조회 일의 첫 날 (00:00)
LocalDateTime dateTime = date.atStartOfDay();
// 가장 가까운 예약 현황 조회
CarBooking booking = jpaQueryFactory.selectFrom(carBooking)
.where(carBooking.car.eq(car),
carBooking.status.in(BookingStatus.WAITING, BookingStatus.BOOKED, BookingStatus.USING),
carBooking.startDate.after(dateTime))
.orderBy(carBooking.startDate.asc())
.fetchFirst();

return Optional.ofNullable(booking)
.map(b -> Collections.singletonList(DateTimeUtil.dateTimeToString(b.getStartDate())))
.orElse(null);
} else {
// 해당 월의 예약 현황 조회
List<CarBooking> bookings = jpaQueryFactory.selectFrom(carBooking)
.where(carBooking.car.eq(car)
.and(carBooking.status.in(BookingStatus.WAITING, BookingStatus.BOOKED, BookingStatus.USING))
.and((carBooking.startDate.between(startDateTime, endDateTime))
.or(carBooking.endDate.between(startDateTime, endDateTime)))
).orderBy(carBooking.startDate.asc())
.fetch();

// 예약이 모두 된 날짜(첫 날 0시 ~ 다음 날 0시) 반환
List<String> bookedDate = new ArrayList<>();
int index = 0;
boolean isContinuity = false;
LocalDateTime standard = null;
// 해당 월의 예약 현황 조회
List<CarBooking> bookings = jpaQueryFactory.selectFrom(carBooking)
.where(carBooking.car.eq(car)
.and(carBooking.status.in(BookingStatus.WAITING, BookingStatus.BOOKED, BookingStatus.USING))
.and((carBooking.startDate.between(startDateTime, endDateTime))
.or(carBooking.endDate.between(startDateTime, endDateTime)))
).orderBy(carBooking.startDate.asc())
.fetch();

// 예약이 모두 된 날짜(첫 날 0시 ~ 다음 날 0시) 반환
List<String> bookedDate = new ArrayList<>();
int index = 0;
boolean isContinuity = false;
LocalDateTime standard = null;

for (CarBooking b : bookings) {
index++;
Expand Down Expand Up @@ -140,7 +121,6 @@ public List<String> getCarBookedDate(Car car, LocalDate standardDate, LocalDate
standard = b.getEndDate();
}
return bookedDate;
}
}

private boolean isMidnight(LocalDateTime localDateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface ResourceBookingCustom {
Page<BookingRes> getBookingsByUser(User user, Pageable pageable);
boolean existsDateTime(Resource resource, LocalDateTime startDateTime, LocalDateTime endDateTime);
List<String> getResourceBookedDate(Resource resource, LocalDate standardDate, LocalDate date);
List<String> getResourceBookedDate(Resource resource, LocalDate standardDate);
void updateBookingStatusForResigning(User user);

ProductBookingRes findResourceBookingByDate(Resource resource, LocalDateTime dateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,17 @@ public Page<BookingRes> getBookingsByUser(User user, Pageable pageable) {

// 장비 월별 예약 현황 조회
@Override
public List<String> getResourceBookedDate(Resource resource, LocalDate standardDate, LocalDate date) {
public List<String> getResourceBookedDate(Resource resource, LocalDate standardDate) {
// 해당 월의 첫 날 (00:00)
LocalDateTime startDateTime = standardDate.withDayOfMonth(1).atStartOfDay();
// 다음 월의 첫 날 (00:00)
LocalDateTime endDateTime = standardDate.plusMonths(1).atStartOfDay();

// 이후 가장 가까운 예약 날짜&시간
if (date != null) {
// 조회 일의 첫 날 (00:00)
LocalDateTime dateTime = date.atStartOfDay();
// 가장 가까운 예약 현황 조회
ResourceBooking booking = jpaQueryFactory.selectFrom(resourceBooking)
.where(resourceBooking.resource.eq(resource),
resourceBooking.status.in(BookingStatus.WAITING, BookingStatus.BOOKED, BookingStatus.USING),
resourceBooking.startDate.after(dateTime))
.orderBy(resourceBooking.startDate.asc())
.fetchFirst();

return Optional.ofNullable(booking)
.map(b -> Collections.singletonList(DateTimeUtil.dateTimeToString(b.getStartDate())))
.orElse(null);
} else {
// 해당 월의 예약 현황 조회
List<ResourceBooking> bookings = jpaQueryFactory.selectFrom(resourceBooking)
.where(resourceBooking.resource.eq(resource)
.and(resourceBooking.status.in(BookingStatus.WAITING, BookingStatus.BOOKED, BookingStatus.USING))
.and((resourceBooking.startDate.between(startDateTime, endDateTime))
// 해당 월의 예약 현황 조회
List<ResourceBooking> bookings = jpaQueryFactory.selectFrom(resourceBooking)
.where(resourceBooking.resource.eq(resource)
.and(resourceBooking.status.in(BookingStatus.WAITING, BookingStatus.BOOKED, BookingStatus.USING))
.and((resourceBooking.startDate.between(startDateTime, endDateTime))
.or(resourceBooking.endDate.between(startDateTime, endDateTime)))
).orderBy(resourceBooking.startDate.asc())
.fetch();
Expand Down Expand Up @@ -132,7 +116,6 @@ public List<String> getResourceBookedDate(Resource resource, LocalDate standardD
standard = b.getEndDate();
}
return bookedDate;
}
}

private boolean isMidnight(LocalDateTime localDateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ public ResponseCustom<List<String>> getCarBookedDate(
@Parameter(description = "(Long) 차량 Id", example = "1") @PathVariable(name = "carId") Long carId,
@Parameter(description = "차량 예약 현황 조회 년도월 (YYYY-MM)", example = "2023-10") @RequestParam String month,
@Parameter(description = "차량 예약 현황 조회 날짜 (YYYY-MM-DD)", example = "2023-10-23") @RequestParam(required = false) @DateTimeFormat(pattern = DATE_PATTERN) LocalDate date) {
return ResponseCustom.OK(carService.getProductBookedDate(carId, month, date));
return ResponseCustom.OK(carService.getProductBookedDate(carId, month));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public ProductBookingRes getProductBookingByDate(Long resourceId, LocalDateTime
}

@Override
public List<String> getProductBookedDate(Long resourceId, String month, LocalDate date) {
public List<String> getProductBookedDate(Long resourceId, String month) {
Car car = carRepository.findById(resourceId)
.orElseThrow(() -> new BaseException(BaseResponseCode.CAR_NOT_FOUND));
// 예약 현황 조회할 월
LocalDate standardDate = DateTimeUtil.stringToFirstLocalDate(month);
return carBookingRepository.getCarBookedDate(car, standardDate, date);
return carBookingRepository.getCarBookedDate(car, standardDate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ResponseCustom<ProductDetailRes> getResourceDetail(
/**
* 장비 월별 예약 현황 조회
*/
@Operation(summary = "장비 월별 예약 현황 조회 (박소정)", description = "월별로 장비 예약이 불가능한 날짜를 조회를 진행한다. /n 일 기준 예약이 아예 불가한 날짜 반환")
@Operation(summary = "장비 월별 예약 현황 조회 (박소정)", description = "해당 월의 장비 예약이 불가능한 날짜를 조회한다. (YYYY-MM-DD)")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "(S0001)요청에 성공했습니다."),
@ApiResponse(responseCode = "400", description = "(R0003)존재하지 않는 장비입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class)))
Expand All @@ -101,9 +101,8 @@ public ResponseCustom<ProductDetailRes> getResourceDetail(
public ResponseCustom<List<String>> getResourceBookedDate(
@Account User user,
@Parameter(description = "(Long) 장비 Id", example = "1") @PathVariable(name = "resourceId") Long resourceId,
@Parameter(description = "장비 예약 현황 조회 년도월 (YYYY-MM)", example = "2023-10") @RequestParam String month,
@Parameter(description = "장비 예약 현황 조회 날짜 (YYYY-MM-DD)", example = "2023-10-23") @RequestParam(required = false) @DateTimeFormat(pattern = DATE_PATTERN) LocalDate date) {
return ResponseCustom.OK(resourceService.getProductBookedDate(resourceId, month, date));
@Parameter(description = "장비 예약 현황 조회 년도월 (YYYY-MM)", example = "2023-10") @RequestParam String month) {
return ResponseCustom.OK(resourceService.getProductBookedDate(resourceId, month));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ public ProductDetailRes getProductDetail(Long resourceId) {
* 장비 기간별 예약 현황 조회
*/
@Override
public List<String> getProductBookedDate(Long resourceId, String month, LocalDate date) {
public List<String> getProductBookedDate(Long resourceId, String month) {
Resource resource = resourceRepository.findById(resourceId)
.orElseThrow(() -> new BaseException(BaseResponseCode.RESOURCE_NOT_FOUND));
// 예약 현황 조회할 월
LocalDate standardDate = DateTimeUtil.stringToFirstLocalDate(month);
return resourceBookingRepository.getResourceBookedDate(resource, standardDate, date);
return resourceBookingRepository.getResourceBookedDate(resource, standardDate);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ProductService {

ProductBookingRes getProductBookingByDate(Long carId, LocalDateTime dateTime);

List<String> getProductBookedDate(Long carId, String month, LocalDate date);
List<String> getProductBookedDate(Long carId, String month);

Page<AdminResourcesRes> getResourcesByAdmin(User user, String keyword, Pageable pageable);

Expand Down

0 comments on commit 156de5e

Please sign in to comment.