Skip to content

Commit

Permalink
Merge pull request #99 from Shimpyo-House/feature/reservation-refacto…
Browse files Browse the repository at this point in the history
…ring

refactor: 예약 API Request Body 및 서비스 로직 리팩토링
  • Loading branch information
jo0oy authored Dec 12, 2023
2 parents 6fe9b91 + c28d038 commit 8fe6e32
Show file tree
Hide file tree
Showing 20 changed files with 603 additions and 434 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.fc.shimpyo_be.domain.reservation.dto;

import lombok.Builder;

import java.util.List;
import java.util.Map;

@Builder
public record CheckAvailableRoomsResultDto(
boolean isAvailable,
List<Long> unavailableIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Builder;

@Builder
public record PreoccupyRoomItemRequestDto(
@NotNull
Long roomId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;

@Builder
public record PreoccupyRoomsRequestDto(

@Valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Builder;

@Builder
public record ReleaseRoomItemRequestDto(
@NotNull
Long roomId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;

@Builder
public record ReleaseRoomsRequestDto(
@Valid
@Size(min = 1, max = 3, message = "최소 1개, 최대 3개의 객실 요청 정보가 필요합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;

import java.util.List;

@Builder
public record SaveReservationRequestDto(
@Valid
@Size(min = 1, max = 3, message = "최소 1개, 최대 3개의 객실 예약이 가능합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.fc.shimpyo_be.domain.reservation.dto.response;

import lombok.Builder;

@Builder
public record ReservationInfoResponseDto(
Long reservationId,
Long reservationProductId,
Long productId,
String productName,
String productImageUrl,
String productAddress,
String productDetailAddress,
Long roomId,
String roomName,
String startDate,
String endDate,
String checkIn,
String checkOut,
Integer price,
String payMethod
String payMethod,
String createdAt
) {
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package com.fc.shimpyo_be.domain.reservation.dto.response;

import com.fc.shimpyo_be.domain.reservation.dto.request.SaveReservationRequestDto;
import com.fc.shimpyo_be.domain.reservation.entity.PayMethod;
import com.fc.shimpyo_be.domain.reservationproduct.dto.response.ReservationProductResponseDto;
import lombok.Builder;

import java.util.List;

@Builder
public record SaveReservationResponseDto(
Long reservationId,
List<ReservationProductResponseDto> reservationProducts,
PayMethod payMethod,
Integer totalPrice
Integer totalPrice,
String createdAt
) {
public SaveReservationResponseDto(Long reservationId, SaveReservationRequestDto requestDto) {
this(
reservationId,
requestDto.reservationProducts()
.stream()
.map(ReservationProductResponseDto::new).toList(),
requestDto.payMethod(),
requestDto.totalPrice()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.fc.shimpyo_be.domain.reservation.dto.response;

import lombok.Builder;

import java.util.List;

@Builder
public record ValidationResultResponseDto(
boolean isAvailable,
List<Long> unavailableIds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fc.shimpyo_be.domain.member.entity.Member;
import com.fc.shimpyo_be.domain.member.exception.MemberNotFoundException;
import com.fc.shimpyo_be.domain.member.repository.MemberRepository;
import com.fc.shimpyo_be.domain.product.entity.Product;
import com.fc.shimpyo_be.domain.product.exception.RoomNotFoundException;
import com.fc.shimpyo_be.domain.reservation.dto.request.ReleaseRoomItemRequestDto;
import com.fc.shimpyo_be.domain.reservation.dto.request.ReleaseRoomsRequestDto;
Expand All @@ -14,6 +13,7 @@
import com.fc.shimpyo_be.domain.reservation.entity.Reservation;
import com.fc.shimpyo_be.domain.reservation.exception.InvalidRequestException;
import com.fc.shimpyo_be.domain.reservation.repository.ReservationRepository;
import com.fc.shimpyo_be.domain.reservation.util.ReservationMapper;
import com.fc.shimpyo_be.domain.reservationproduct.dto.request.ReservationProductRequestDto;
import com.fc.shimpyo_be.domain.reservationproduct.entity.ReservationProduct;
import com.fc.shimpyo_be.domain.reservationproduct.repository.ReservationProductRepository;
Expand All @@ -32,7 +32,6 @@
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -77,52 +76,27 @@ public SaveReservationResponseDto saveReservation(Long memberId, SaveReservation
}

// 예약 저장
Long reservationId
= reservationRepository.save(
Reservation.builder()
.member(member)
.reservationProducts(reservationProducts)
.payMethod(request.payMethod())
.totalPrice(request.totalPrice())
.build()).getId();

return new SaveReservationResponseDto(reservationId, request);
Reservation reservation = reservationRepository.save(
Reservation.builder()
.member(member)
.reservationProducts(reservationProducts)
.payMethod(request.payMethod())
.totalPrice(request.totalPrice())
.build()
);

return ReservationMapper.from(reservation);
}

@Transactional(readOnly = true)
public Page<ReservationInfoResponseDto> getReservationInfoList(Long memberId, Pageable pageable) {
log.info("{} ::: {}", getClass().getSimpleName(), "getReservationInfoList");

List<Long> reservationIds = reservationRepository.findIdsByMemberId(memberId);
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");

return reservationProductRepository
.findAllInReservationIds(reservationIds, pageable)
.map(
reservationProduct -> {
Reservation reservation = reservationProduct.getReservation();
Room room = reservationProduct.getRoom();
Product product = room.getProduct();

return new ReservationInfoResponseDto(
reservation.getId(),
reservationProduct.getId(),
product.getId(),
product.getName(),
product.getThumbnail(),
product.getAddress().getAddress() + " " + product.getAddress().getDetailAddress(),
room.getId(),
room.getName(),
dateFormatter.format(reservationProduct.getStartDate()),
dateFormatter.format(reservationProduct.getEndDate()),
timeFormatter.format(room.getCheckIn()),
timeFormatter.format(room.getCheckOut()),
reservationProduct.getPrice(),
reservation.getPayMethod().name()
);
}
);
.map(ReservationMapper::from);
}

public ValidationResultResponseDto validate(Long memberId, List<ReservationProductRequestDto> reservationProducts) {
Expand All @@ -137,20 +111,20 @@ public ValidationResultResponseDto validate(Long memberId, List<ReservationProdu
LocalDate endDate = DateTimeUtil.toLocalDate(reservationProduct.endDate());

List<String> keys = new LinkedList<>();
while(targetDate.isBefore(endDate)) {
while (targetDate.isBefore(endDate)) {
keys.add(String.format(REDIS_ROOM_KEY_FORMAT, reservationProduct.roomId(), targetDate));
targetDate = targetDate.plusDays(1);
}

List<Object> values = opsForValue.multiGet(keys);
String memberIdValue = String.valueOf(memberId);

if(ObjectUtils.isEmpty(values)) {
if (ObjectUtils.isEmpty(values)) {
throw new InvalidRequestException(ErrorCode.INVALID_RESERVATION_REQUEST);
}

for (Object value : values) {
if(!memberIdValue.equals(value)) {
if (!memberIdValue.equals(value)) {
isValid = false;
invalidRoomIds.add(Long.valueOf((String) value));
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.fc.shimpyo_be.domain.reservation.util;

import com.fc.shimpyo_be.domain.product.entity.Product;
import com.fc.shimpyo_be.domain.reservation.dto.response.ReservationInfoResponseDto;
import com.fc.shimpyo_be.domain.reservation.dto.response.SaveReservationResponseDto;
import com.fc.shimpyo_be.domain.reservation.entity.Reservation;
import com.fc.shimpyo_be.domain.reservationproduct.dto.response.ReservationProductResponseDto;
import com.fc.shimpyo_be.domain.reservationproduct.entity.ReservationProduct;
import com.fc.shimpyo_be.domain.room.entity.Room;
import com.fc.shimpyo_be.global.util.DateTimeUtil;

import java.util.ArrayList;
import java.util.List;

public class ReservationMapper {

public static ReservationInfoResponseDto from(ReservationProduct reservationProduct) {
Reservation reservation = reservationProduct.getReservation();
Room room = reservationProduct.getRoom();
Product product = room.getProduct();

return ReservationInfoResponseDto.builder()
.reservationId(reservation.getId())
.reservationProductId(reservationProduct.getId())
.productId(product.getId())
.productName(product.getName())
.productImageUrl(product.getThumbnail())
.productAddress(product.getAddress().getAddress())
.productDetailAddress(product.getAddress().getDetailAddress())
.roomId(room.getId())
.roomName(room.getName())
.startDate(DateTimeUtil.toString(reservationProduct.getStartDate()))
.endDate(DateTimeUtil.toString(reservationProduct.getEndDate()))
.checkIn(DateTimeUtil.toString(room.getCheckIn()))
.checkOut(DateTimeUtil.toString(room.getCheckOut()))
.price(reservationProduct.getPrice())
.payMethod(reservation.getPayMethod().name())
.createdAt(DateTimeUtil.toString(reservation.getCreatedAt()))
.build();
}

public static SaveReservationResponseDto from(Reservation reservation) {
List<ReservationProductResponseDto> reservationProductDtos = new ArrayList<>();
for (ReservationProduct reservationProduct : reservation.getReservationProducts()) {

Room room = reservationProduct.getRoom();
Product product = room.getProduct();

reservationProductDtos.add(
ReservationProductResponseDto.builder()
.productName(product.getName())
.roomId(room.getId())
.roomName(room.getName())
.standard(room.getStandard())
.capacity(room.getCapacity())
.startDate(DateTimeUtil.toString(reservationProduct.getStartDate()))
.endDate(DateTimeUtil.toString(reservationProduct.getEndDate()))
.checkIn(DateTimeUtil.toString(room.getCheckIn()))
.checkOut(DateTimeUtil.toString(room.getCheckOut()))
.visitorName(reservationProduct.getVisitorName())
.visitorPhone(reservationProduct.getVisitorPhone())
.price(reservationProduct.getPrice())
.build()
);
}

return SaveReservationResponseDto.builder()
.reservationId(reservation.getId())
.reservationProducts(reservationProductDtos)
.payMethod(reservation.getPayMethod())
.totalPrice(reservation.getTotalPrice())
.createdAt(DateTimeUtil.toString(reservation.getCreatedAt()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
package com.fc.shimpyo_be.domain.reservationproduct.dto.request;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Builder;

@Builder
public record ReservationProductRequestDto(
@NotNull
Long roomId,
@NotBlank
String productName,
@NotBlank
String roomName,
@NotNull
Integer standard,
@NotNull
Integer max,
@Pattern(regexp = "^\\d{4}-\\d{2}-\\d{2}$", message = "올바른 날짜 형식이 아닙니다.(yyyy-MM-dd 형식으로 입력하세요.)")
String startDate,
@Pattern(regexp = "^\\d{4}-\\d{2}-\\d{2}$", message = "올바른 날짜 형식이 아닙니다.(yyyy-MM-dd 형식으로 입력하세요.)")
String endDate,
@Pattern(regexp = "^\\d{2}:\\d{2}$", message = "올바른 시간 형식이 아닙니다.(HH:mm 형식으로 입력하세요.)")
String checkIn,
@Pattern(regexp = "^\\d{2}:\\d{2}$", message = "올바른 시간 형식이 아닙니다.(HH:mm 형식으로 입력하세요.)")
String checkOut,
String visitorName,
String visitorPhone,
@Min(value = 0, message = "객실 이용 금액은 음수일 수 없습니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.fc.shimpyo_be.domain.reservationproduct.dto.response;

import com.fc.shimpyo_be.domain.reservationproduct.dto.request.ReservationProductRequestDto;
import lombok.Builder;

@Builder
public record ReservationProductResponseDto(
Long roomId,
String productName,
Long roomId,
String roomName,
Integer standard,
Integer max,
Integer capacity,
String startDate,
String endDate,
String checkIn,
Expand All @@ -16,20 +17,4 @@ public record ReservationProductResponseDto(
String visitorPhone,
Integer price
) {
public ReservationProductResponseDto(ReservationProductRequestDto requestDto) {
this(
requestDto.roomId(),
requestDto.productName(),
requestDto.roomName(),
requestDto.standard(),
requestDto.max(),
requestDto.startDate(),
requestDto.endDate(),
requestDto.checkIn(),
requestDto.checkOut(),
requestDto.visitorName(),
requestDto.visitorPhone(),
requestDto.price()
);
}
}
Loading

0 comments on commit 8fe6e32

Please sign in to comment.