Skip to content

Commit

Permalink
Merge pull request #52 from PLADI-ALM/feat/PDS-71-getResourceBookedDate
Browse files Browse the repository at this point in the history
[PDS-71/feat] 자원 월별 예약 현황 조회
  • Loading branch information
psyeon1120 authored Oct 8, 2023
2 parents ff41729 + 7ec9aee commit 8321d64
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.example.pladialmserver.booking.repository.resourceBooking;

import com.example.pladialmserver.booking.dto.response.BookingRes;
import com.example.pladialmserver.resource.entity.Resource;
import com.example.pladialmserver.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

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

public interface ResourceBookingCustom {
Page<BookingRes> getBookingsByUser(User user, Pageable pageable);
List<String> getResourceBookedDate(Resource resource, LocalDate standardDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import com.example.pladialmserver.booking.dto.response.BookingRes;
import com.example.pladialmserver.booking.entity.ResourceBooking;
import com.example.pladialmserver.global.entity.BookingStatus;
import com.example.pladialmserver.global.utils.DateTimeUtil;
import com.example.pladialmserver.resource.entity.Resource;
import com.example.pladialmserver.user.entity.User;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -35,4 +40,33 @@ public Page<BookingRes> getBookingsByUser(User user, Pageable pageable) {
int end = Math.min((start + pageable.getPageSize()), res.size());
return new PageImpl<>(res.subList(start, end), pageable, res.size());
}

// 자원 월별 예약 현황 조회
@Override
public List<String> getResourceBookedDate(Resource resource, LocalDate standardDate) {
// 해당 월의 첫 날
LocalDate startDate = standardDate.withDayOfMonth(1);
// 해당 월의 마지막 날
LocalDate endDate = standardDate.withDayOfMonth(standardDate.lengthOfMonth());

// 해당 월의 예약 현황 조회
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(startDate, endDate))
.or(resourceBooking.endDate.between(startDate, endDate))))
.orderBy(resourceBooking.startDate.asc())
.fetch();

// 예약 시작 ~ 끝 날짜 반환
List<String> bookedDate = new ArrayList<>();
for (ResourceBooking b : bookings) {
List<String> date = b.getStartDate().datesUntil(b.getEndDate().plusDays(1))
.map(DateTimeUtil::dateToString)
.collect(Collectors.toList());
bookedDate.addAll(date);
}

return bookedDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public static String dateTimeToString(LocalDateTime localDateTime) {
public static String dateTimeToStringNullable(LocalDateTime localDateTime) {
return localDateTime == null ? null : dateTimeToString(localDateTime);
}

// string(YYYY-MM) -> localDate
public static LocalDate stringToLocalDate(String stringDate) {
return LocalDate.parse(stringDate, DateTimeFormatter.ISO_DATE);
}

// string(YYYY-MM) -> localDate (해당 월의 첫 날)
public static LocalDate stringToFirstLocalDate(String stringDate) {
return stringToLocalDate(stringDate + "-01");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.example.pladialmserver.global.Constants.DATE_PATTERN;

Expand Down Expand Up @@ -73,8 +75,19 @@ public ResponseCustom<ResourceDetailRes> getResourceDetail(


/**
* 자원 기간별 예약 현황 조회별
* 자원 월별 예약 현황 조회
*/
@Operation(summary = "자원 월별 예약 현황 조회", description = "월별로 자원 예약이 불가능한 날짜를 조회를 진행한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "(S0001)요청에 성공했습니다."),
@ApiResponse(responseCode = "400", description = "(R0003)존재하지 않는 자원입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class)))
})
@GetMapping("/{resourceId}/booking-state")
public ResponseCustom<List<String>> getResourceBookedDate(
@Parameter(description = "(Long) 자원 Id", example = "1") @PathVariable(name = "resourceId") Long resourceId,
@Parameter(description = "자원 예약 현황 조회 년도월 (YYYY-MM)",example = "2023-10") @RequestParam String month) {
return ResponseCustom.OK(resourceService.getResourceBookedDate(resourceId, month));
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.example.pladialmserver.global.exception.BaseException;
import com.example.pladialmserver.global.exception.BaseResponseCode;
import com.example.pladialmserver.global.utils.DateTimeUtil;
import com.example.pladialmserver.resource.dto.response.ResourceDetailRes;
import com.example.pladialmserver.resource.dto.response.ResourceRes;
import com.example.pladialmserver.resource.entity.Resource;
Expand Down Expand Up @@ -63,11 +64,16 @@ public ResourceDetailRes getResourceDetail(Long resourceId) {
}



/**
* 자원 기간별 예약 현황 조회
*/

public List<String> getResourceBookedDate(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);
}



Expand Down

0 comments on commit 8321d64

Please sign in to comment.