Skip to content

Commit

Permalink
feat: 자신의 예약 조회 시 페이지네이션 정보 주도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Jan 8, 2024
1 parent 15a3b5c commit 3739f86
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.example.busan.reservation.dto.ReservationResponse;
import com.example.busan.reservation.dto.UpdateReservationRequest;
import com.example.busan.reservation.service.ReservationService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
Expand All @@ -20,8 +21,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/reservations")
public class ReservationController {
Expand Down Expand Up @@ -55,9 +54,9 @@ public ResponseEntity<Void> update(@PathVariable("reservationId") final Long id,
}

@GetMapping
public ResponseEntity<List<ReservationResponse>> findAll(@Authorized final Authentication authentication,
public ResponseEntity<Page<ReservationResponse>> findAll(@Authorized final Authentication authentication,
@PageableDefault final Pageable pageable) {
final List<ReservationResponse> response = reservationService.findAll(authentication.email(), pageable);
final Page<ReservationResponse> response = reservationService.findAll(authentication.email(), pageable);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.busan.reservation.domain;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand Down Expand Up @@ -34,5 +35,5 @@ boolean existDuplicatedTime(@Param("startTime") LocalDateTime startTime,

Optional<Reservation> findByIdAndReservationEmailAndStatusIsNot(Long id, String email, Status status);

List<Reservation> findAllByReservationEmail(String reservationEmail, Pageable pageable);
Page<Reservation> findAllByReservationEmail(String reservationEmail, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.example.busan.reservation.dto.UpdateReservationRequest;
import com.example.busan.room.domain.Room;
import com.example.busan.room.domain.RoomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -69,22 +70,21 @@ public void update(final Long id, final String currentMemberEmail, final UpdateR
reservation.update(request.roomId(), request.startTime(), request.endTime());
}

public List<ReservationResponse> findAll(final String currentMemberEmail, final Pageable pageable) {
public Page<ReservationResponse> findAll(final String currentMemberEmail, final Pageable pageable) {
final Member member = getMember(currentMemberEmail);
final List<Reservation> reservations = getReservations(currentMemberEmail, pageable);
final Map<Long, Room> rooms = getRooms(reservations);
final Page<Reservation> reservations = getReservations(currentMemberEmail, pageable);
final Map<Long, Room> rooms = getRooms(reservations.getContent());

return reservations.stream()
.map(reservation -> ReservationResponse.of(reservation, member, rooms.get(reservation.getRoomId())))
.toList();
return reservations
.map(reservation -> ReservationResponse.of(reservation, member, rooms.get(reservation.getRoomId())));
}

private Member getMember(final String currentMemberEmail) {
return memberRepository.findById(currentMemberEmail)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 로그인 유저입니다."));
}

private List<Reservation> getReservations(final String currentMemberEmail, final Pageable pageable) {
private Page<Reservation> getReservations(final String currentMemberEmail, final Pageable pageable) {
final PageRequest pageRequest = PageRequest.of(
pageable.getPageNumber(), pageable.getPageSize(), Sort.by("startTime").descending());

Expand Down
127 changes: 79 additions & 48 deletions src/main/resources/static/api/openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ paths:
현재 유저 정보 조회하기:
value: "{\"name\":\"연어\",\"phone\":\"01012341234\",\"email\":\"\
[email protected]\",\"role\":\"USER\",\"company\":\"우형\",\"region\"\
:\"BUSAN\",\"createdAt\":\"2024-01-08T12:46:55.613352\"}"
:\"BUSAN\",\"createdAt\":\"2024-01-08T13:39:40.626045\"}"
post:
tags:
- members
Expand Down Expand Up @@ -276,18 +276,22 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/reservations1059448458'
$ref: '#/components/schemas/reservations-2055281597'
examples:
자신의 회의실 예약 목록 최신 순으로 보기:
value: "[{\"id\":1,\"status\":\"RESERVED\",\"cancelReason\":null,\"\
startTime\":\"2024-01-08T12:46:57.400076\",\"endTime\":\"2024-01-08T14:46:57.400081\"\
value: "{\"content\":[{\"id\":1,\"status\":\"RESERVED\",\"cancelReason\"\
:null,\"startTime\":\"2024-01-08T13:39:42.354426\",\"endTime\"\
:\"2024-01-08T15:39:42.354432\",\"name\":\"황재현\",\"phone\":\"\
01012341234\",\"reservedAt\":\"2024-01-08T13:39:42.354442\",\"\
roomId\":1,\"roomName\":\"대회의실\",\"company\":\"요기요\"},{\"id\"\
:2,\"status\":\"CANCELED\",\"cancelReason\":\"쓰기 싫어졌어요..\",\"\
startTime\":\"2024-01-08T13:39:42.354451\",\"endTime\":\"2024-01-08T15:39:42.354453\"\
,\"name\":\"황재현\",\"phone\":\"01012341234\",\"reservedAt\":\"\
2024-01-08T12:46:57.400091\",\"roomId\":1,\"roomName\":\"대회의실\"\
,\"company\":\"요기요\"},{\"id\":2,\"status\":\"CANCELED\",\"cancelReason\"\
:\"쓰기 싫어졌어요..\",\"startTime\":\"2024-01-08T12:46:57.400099\",\"\
endTime\":\"2024-01-08T14:46:57.400101\",\"name\":\"황재현\",\"phone\"\
:\"01012341234\",\"reservedAt\":\"2024-01-08T12:46:57.400103\"\
,\"roomId\":1,\"roomName\":\"대회의실\",\"company\":\"토스뱅크\"}]"
2024-01-08T13:39:42.354455\",\"roomId\":1,\"roomName\":\"대회의실\"\
,\"company\":\"토스뱅크\"}],\"pageable\":\"INSTANCE\",\"last\":true,\"\
totalPages\":1,\"totalElements\":2,\"first\":true,\"size\":2,\"\
number\":0,\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\"\
:true},\"numberOfElements\":2,\"empty\":false}"
post:
tags:
- reservations
Expand Down Expand Up @@ -479,44 +483,6 @@ components:
roomId:
type: number
description: 회의실 ID
reservations1059448458:
type: array
items:
type: object
properties:
phone:
type: string
description: 예약자 휴대폰 번호
name:
type: string
description: 예약자 성함
company:
type: string
description: 예약자 회사
startTime:
type: string
description: 시작 시각
endTime:
type: string
description: 종료 시각
id:
type: number
description: 예약 ID
reservedAt:
type: string
description: 예약 일시
cancelReason:
type: string
description: 취소 상태일 경우 취소 이유(취소가 아니면 null)
roomName:
type: string
description: 회의실 이름
roomId:
type: number
description: 회의실 ID
status:
type: string
description: 예약 상태
auth-login1098228858:
type: object
properties:
Expand Down Expand Up @@ -598,6 +564,71 @@ components:
reason:
type: string
description: 취소 사유
reservations-2055281597:
type: object
properties:
number:
type: number
description: 현재 페이지
size:
type: number
description: 한 페이지당 사이즈
numberOfElements:
type: number
description: 현재 응답 데이터 요소 개수
last:
type: boolean
description: 마지막 페이지인지
totalPages:
type: number
description: 전체 페이지 개수
first:
type: boolean
description: 첫 페이지인지
content:
type: array
items:
type: object
properties:
phone:
type: string
description: 예약자 휴대폰 번호
name:
type: string
description: 예약자 성함
company:
type: string
description: 예약자 회사
startTime:
type: string
description: 시작 시각
endTime:
type: string
description: 종료 시각
id:
type: number
description: 예약 ID
reservedAt:
type: string
description: 예약 일시
cancelReason:
type: string
description: 취소 상태일 경우 취소 이유(취소가 아니면 null)
roomName:
type: string
description: 회의실 이름
roomId:
type: number
description: 회의실 ID
status:
type: string
description: 예약 상태
totalElements:
type: number
description: 전체 DB 데이터 요소 개수
empty:
type: boolean
description: 빈 데이터인지
members-phone932474475:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
Expand Down Expand Up @@ -137,7 +138,7 @@ void findAll() throws Exception {
"황재현", "01012341234", LocalDateTime.now(), 1L, "대회의실", "토스뱅크");

given(reservationService.findAll(any(), any()))
.willReturn(List.of(reservation1, reservation2));
.willReturn(new PageImpl<>(List.of(reservation1, reservation2)));

//when
final MockHttpServletResponse response = mockMvc.perform(
Expand All @@ -151,17 +152,27 @@ void findAll() throws Exception {
parameterWithName("page").description("페이지는 1부터 시작 (디폴트값 1)").optional(),
parameterWithName("size").description("페이지별 사이즈 (디폴트값 10)").optional()),
responseFields(
fieldWithPath("[].id").description("예약 ID"),
fieldWithPath("[].status").description("예약 상태"),
fieldWithPath("[].cancelReason").description("취소 상태일 경우 취소 이유(취소가 아니면 null)").optional(),
fieldWithPath("[].startTime").description("시작 시각"),
fieldWithPath("[].endTime").description("종료 시각"),
fieldWithPath("[].name").description("예약자 성함"),
fieldWithPath("[].phone").description("예약자 휴대폰 번호"),
fieldWithPath("[].reservedAt").description("예약 일시"),
fieldWithPath("[].roomId").description("회의실 ID"),
fieldWithPath("[].company").description("예약자 회사"),
fieldWithPath("[].roomName").description("회의실 이름"))))
fieldWithPath("content.[].id").description("예약 ID"),
fieldWithPath("content.[].status").description("예약 상태"),
fieldWithPath("content.[].cancelReason").description("취소 상태일 경우 취소 이유(취소가 아니면 null)").optional(),
fieldWithPath("content.[].startTime").description("시작 시각"),
fieldWithPath("content.[].endTime").description("종료 시각"),
fieldWithPath("content.[].name").description("예약자 성함"),
fieldWithPath("content.[].phone").description("예약자 휴대폰 번호"),
fieldWithPath("content.[].reservedAt").description("예약 일시"),
fieldWithPath("content.[].roomId").description("회의실 ID"),
fieldWithPath("content.[].company").description("예약자 회사"),
fieldWithPath("content.[].roomName").description("회의실 이름"),
fieldWithPath("last").description("마지막 페이지인지"),
fieldWithPath("first").description("첫 페이지인지"),
fieldWithPath("empty").description("빈 데이터인지"),
fieldWithPath("totalPages").description("전체 페이지 개수"),
fieldWithPath("totalElements").description("전체 DB 데이터 요소 개수"),
fieldWithPath("numberOfElements").description("현재 응답 데이터 요소 개수"),
fieldWithPath("size").description("한 페이지당 사이즈"),
fieldWithPath("number").description("현재 페이지"),
fieldWithPath("pageable").description("요청한 페이지네이션 정보").ignored(),
fieldWithPath("sort.*").description("정렬 정보").ignored())))
.andReturn()
.getResponse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ void findAllByReservationEmail() {
final PageRequest pageRequest = PageRequest.of(0, 2, Sort.by("startTime").descending());

//when
final List<Reservation> response = reservationRepository.findAllByReservationEmail("[email protected]", pageRequest);
final List<Reservation> response = reservationRepository.findAllByReservationEmail("[email protected]", pageRequest)
.getContent();

//then
assertThat(response).hasSize(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ void findAll() {
createReservation(room.getId());

//when
final List<ReservationResponse> response = reservationService.findAll("[email protected]", Pageable.ofSize(10));
final List<ReservationResponse> response = reservationService.findAll("[email protected]", Pageable.ofSize(10))
.getContent();

//then
assertSoftly(soft -> {
Expand All @@ -238,7 +239,8 @@ void findAll2() {
createCanceledReservation(room.getId());

//when
final List<ReservationResponse> response = reservationService.findAll("[email protected]", Pageable.ofSize(10));
final List<ReservationResponse> response = reservationService.findAll("[email protected]", Pageable.ofSize(10))
.getContent();

//then
assertSoftly(soft -> {
Expand Down

0 comments on commit 3739f86

Please sign in to comment.