Skip to content

Commit

Permalink
Merge pull request #38 from PLADI-ALM/feat/PDS-73-getBookings
Browse files Browse the repository at this point in the history
[PDS-73/feat] 예약 목록 조회(자원) API
  • Loading branch information
sojungpp authored Oct 3, 2023
2 parents 1b9e48a + 338ead7 commit b657b56
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ public class BookingController {
/**
* 예약 목록 조회
*/
@Operation(summary = "예약 목록 조회", description = "회의실 및 비품 예약 내역을 전체 조회한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "(S0001)요청에 성공했습니다."),
@ApiResponse(responseCode = "400", description = "(G0001)잘못된 요청입니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class))),
@ApiResponse(responseCode = "404", description = "(U0001)사용자를 찾을 수 없습니다.", content = @Content(schema = @Schema(implementation = ResponseCustom.class)))
})
@GetMapping
public ResponseCustom<Page<BookingRes>> getBookings(@RequestParam(required = false) String category,
@PageableDefault(size = 8) Pageable pageable){
// TODO 유저 ID 받아오는 로직 추가
public ResponseCustom<Page<BookingRes>> getBookings(
@Parameter(description = "(String) 카테고리 선택", example = "'office' / 'resource'") @RequestParam(required = false) String category,
@PageableDefault(size = 8) Pageable pageable){
// TODO 유저 ID 받아오는 로직 추가, category 검증 추가 (queryDSL 변경 후 적용)
Long userId = 1L;
return ResponseCustom.OK(bookingService.getBookings(userId, category, pageable));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
package com.example.pladialmserver.booking.dto.response;

import com.example.pladialmserver.booking.entity.OfficeBooking;
import com.example.pladialmserver.booking.entity.ResourceBooking;
import com.example.pladialmserver.global.utils.DateTimeUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

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

@Getter
@Builder
public class BookingRes {

@Schema(type = "Long", description = "회의실 Id / 자원 Id", example = "1")
private Long id;
@Schema(type = "String", description = "회의실명 / 자원명", example = "'회의실1' / '카메라1'")
private String name;
private String location;
@Schema(type = "String", description = "회의실 위치 / 카테고리", example = "'401호' / '촬영장비'")
private String detailInfo;
@Schema(type = "String", description = "예약일자(시작일)", example = "'2023-10-01 12:00' / '2023-10-01'")
private String startDateTime;
@Schema(type = "String", description = "예약일자(종료일)", example = "'2023-10-01 13:00' / '2023-10-03'")
private String endDateTime;
@Schema(type = "String", description = "상태", example = "'예약대기' / '예약중' / '사용중' / '사용완료' / '예약취소'")
private String status;

public static BookingRes toDto(OfficeBooking officeBooking) {
return BookingRes.builder()
.id(officeBooking.getOfficeBookingId())
.name(officeBooking.getOffice().getName())
.location(officeBooking.getOffice().getLocation())
.detailInfo(officeBooking.getOffice().getLocation())
.startDateTime(DateTimeUtil.dateAndTimeToString(officeBooking.getDate(), officeBooking.getStartTime()))
.endDateTime(DateTimeUtil.dateAndTimeToString(officeBooking.getDate(), officeBooking.getEndTime()))
.status(officeBooking.getStatus().getValue())
.build();
}

public static BookingRes toDto(ResourceBooking resourceBooking) {
return BookingRes.builder()
.id(resourceBooking.getResourceBookingId())
.name(resourceBooking.getResource().getName())
.detailInfo(resourceBooking.getResource().getCategory().getValue())
.startDateTime(DateTimeUtil.dateToString(resourceBooking.getStartDate()))
.endDateTime(DateTimeUtil.dateToString(resourceBooking.getEndDate()))
.status(resourceBooking.getStatus().getValue())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.pladialmserver.booking.repository.resourceBooking;

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

public interface ResourceBookingCustom {
Page<BookingRes> getBookingsByUser(User user, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

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

@Repository
public interface ResourceBookingRepository extends JpaRepository<ResourceBooking, Long> {
public interface ResourceBookingRepository extends JpaRepository<ResourceBooking, Long>, ResourceBookingCustom {

@Query("SELECT rb.resource.resourceId FROM ResourceBooking rb WHERE (rb.startDate <= :endDate AND rb.endDate >= :startDate) AND rb.resource.name LIKE %:resourceName%")
List<Long> findBookedResourceIdsByDateAndResourceName(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate, @Param("resourceName") String resourceName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.pladialmserver.booking.repository.resourceBooking;

import com.example.pladialmserver.booking.dto.response.BookingRes;
import com.example.pladialmserver.booking.entity.ResourceBooking;
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.util.List;
import java.util.stream.Collectors;

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


@RequiredArgsConstructor
public class ResourceBookingRepositoryImpl implements ResourceBookingCustom{
private final JPAQueryFactory jpaQueryFactory;

@Override
public Page<BookingRes> getBookingsByUser(User user, Pageable pageable) {
List<ResourceBooking> bookings = jpaQueryFactory.selectFrom(resourceBooking)
.where(resourceBooking.user.eq(user)
.and(resourceBooking.isEnable.eq(true)))
.orderBy(resourceBooking.createdAt.desc())
.fetch();

List<BookingRes> res = bookings.stream()
.map(BookingRes::toDto)
.collect(Collectors.toList());

int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), res.size());
return new PageImpl<>(res.subList(start, end), pageable, res.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ public class BookingService {
* 예약 목록 조회
*/
public Page<BookingRes> getBookings(Long userId, String category, Pageable pageable) {
// TODO 비품 테이블명 정한 후, category 분리 로직 추가
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(BaseResponseCode.USER_NOT_FOUND));
return officeBookingRepository.getBookingsByUser(user, pageable);

// TODO : QueryDSL로 상수 없애기
if(category.equals("office")) {
return officeBookingRepository.getBookingsByUser(user, pageable);
}
else if(category.equals("resource")) {
return resourceBookingRepository.getBookingsByUser(user, pageable);
} else {
throw new BaseException(BaseResponseCode.BAD_REQUEST);
}
}

/**
Expand Down

0 comments on commit b657b56

Please sign in to comment.