Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 예약 API Request Body 및 서비스 로직 리팩토링 #99

Merged
merged 14 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record ReservationInfoResponseDto(
String productName,
String productImageUrl,
String productAddress,
String productDetailAddress,
Long roomId,
String roomName,
String startDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;

Expand All @@ -10,16 +9,7 @@ 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
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,72 @@
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 new ReservationInfoResponseDto(
reservation.getId(),
reservationProduct.getId(),
product.getId(),
product.getName(),
product.getThumbnail(),
product.getAddress().getAddress(),
product.getAddress().getDetailAddress(),
room.getId(),
room.getName(),
DateTimeUtil.toString(reservationProduct.getStartDate()),
DateTimeUtil.toString(reservationProduct.getEndDate()),
DateTimeUtil.toString(room.getCheckIn()),
DateTimeUtil.toString(room.getCheckOut()),
reservationProduct.getPrice(),
reservation.getPayMethod().name()
);
}

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(
new ReservationProductResponseDto(
product.getName(),
room.getId(),
room.getName(),
room.getStandard(),
room.getCapacity(),
DateTimeUtil.toString(reservationProduct.getStartDate()),
DateTimeUtil.toString(reservationProduct.getEndDate()),
DateTimeUtil.toString(room.getCheckIn()),
DateTimeUtil.toString(room.getCheckOut()),
reservationProduct.getVisitorName(),
reservationProduct.getVisitorPhone(),
reservationProduct.getPrice()
)
);
}

return new SaveReservationResponseDto(
reservation.getId(),
reservationProductDtos,
reservation.getPayMethod(),
reservation.getTotalPrice(),
DateTimeUtil.toString(reservation.getCreatedAt())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
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;

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,11 @@
package com.fc.shimpyo_be.domain.reservationproduct.dto.response;

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

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 +14,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()
);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/fc/shimpyo_be/global/util/DateTimeUtil.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.fc.shimpyo_be.global.util;

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

public class DateTimeUtil {

public final static String LOCAL_DATE_PATTERN = "yyyy-MM-dd";
public final static String LOCAL_TIME_PATTERN = "HH:mm";
public final static String LOCAL_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

public final static String LOCAL_DATE_REGEX_PATTERN = "\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])";

Expand All @@ -24,6 +26,10 @@ public static String toString(LocalTime timeObject) {
return timeObject.format(DateTimeFormatter.ofPattern(LOCAL_TIME_PATTERN));
}

public static String toString(LocalDateTime dateTimeObject) {
return dateTimeObject.format(DateTimeFormatter.ofPattern(LOCAL_DATETIME_PATTERN));
}

public static LocalDate toLocalDate(String dateString) {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(LOCAL_DATE_PATTERN));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.fc.shimpyo_be.config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@TestConfiguration
public class TestDBCleanerConfig {

@Bean
Expand Down
Loading