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] 프로젝트 구조 변경 및 서비스 코드 수정 #14

Merged
merged 25 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5f749ca
structure: 디렉토리 구조 변경 (도메인별 → 계층별)
uijin31 Dec 9, 2024
857b4b0
refactor: UserRepository, PlaceRepository에 대한 제어권을 UserService, Place…
uijin31 Dec 9, 2024
056faaa
test: 서비스 분리에 따른 테스트 코드 변경 및 PlaceService, UserService 단위 테스트 작성
uijin31 Dec 9, 2024
9b35295
test: BookingServiceTest → BookingServiceUnitTest 클래스명 변경
uijin31 Dec 9, 2024
4d29db6
test: verify() 메서드를 통해 메서드 호출 검증 추가
uijin31 Dec 9, 2024
2fee9d9
structure: 컨트롤러와 애플리케이션 사이의 Response DTO 위치를 애플리케이션으로 변경
uijin31 Dec 9, 2024
b926940
refactor: 생성자 파라미터 순서 변경
uijin31 Dec 9, 2024
b0a14e8
feat: Booking 생성자 파라미터 검증 추가
uijin31 Dec 9, 2024
e3576c3
feat: 예약 후 상태 반환 메서드를 BookingSlot에서 BookingStatus로 변경
uijin31 Dec 9, 2024
20e8bfd
feat: 예약 인원 정보가 없는 경우 1인을 기본값으로 하도록 변경
uijin31 Dec 9, 2024
178c452
refactor: 필드를 직접 참조하지 않고, 메서드를 호출하도록 변경
uijin31 Dec 9, 2024
0086566
refactor: 오타 수정 (결재 → 결제)
uijin31 Dec 9, 2024
5f6b1e2
refactor: 메서드명 변경(getStatusAfterBook → getStatusAfterBooking)
uijin31 Dec 9, 2024
00658a0
test: BookingSlot 예약 기능 단위 테스트 작성
uijin31 Dec 9, 2024
04a781b
test: BookingStatus.getStatusAfterBooking 메서드 단위 테스트 작성
uijin31 Dec 9, 2024
dc3ba58
test: Booking 생성 단위 테스트 작성
uijin31 Dec 9, 2024
d6e8029
refactor: if 대신 requireNonNull() 메서드 적용
uijin31 Dec 10, 2024
c22a745
refactor: 리소스가 없는 경우 예외 처리 로직을 각 비즈니스에서 해결하도록 변경
uijin31 Dec 26, 2024
bdf04f1
refactor: 매직 넘버 제거 (default party size: 1)
uijin31 Dec 26, 2024
dd5906a
feat: book() → setBooked() 변경
uijin31 Dec 26, 2024
165a746
test: 서비스 코드 변경에 따른 테스트 코드 변경
uijin31 Dec 26, 2024
3064e73
style: 스네이크 케이스를 카멜 케이스로 변경
uijin31 Dec 26, 2024
df6f8ee
test: 도메인 코드 변경에 따른 테스트 코드 변경
uijin31 Dec 26, 2024
3b08999
test: 도메인 코드 변경에 따른 테스트 코드 변경
uijin31 Dec 26, 2024
a0642b5
chore: merge main
uijin31 Dec 30, 2024
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
@@ -1,15 +1,13 @@
package com.nowait.booking.application;
package com.nowait.application;

import com.nowait.booking.domain.model.Booking;
import com.nowait.booking.domain.model.BookingSlot;
import com.nowait.booking.domain.repository.BookingRepository;
import com.nowait.booking.domain.repository.BookingSlotRepository;
import com.nowait.booking.dto.TimeSlotDto;
import com.nowait.booking.dto.response.BookingRes;
import com.nowait.booking.dto.response.DailyBookingStatusRes;
import com.nowait.place.domain.repository.PlaceRepository;
import com.nowait.user.domain.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import com.nowait.application.dto.response.booking.BookingRes;
import com.nowait.application.dto.response.booking.DailyBookingStatusRes;
import com.nowait.application.dto.response.booking.TimeSlotDto;
import com.nowait.application.event.BookingEventPublisher;
import com.nowait.domain.model.booking.Booking;
import com.nowait.domain.model.booking.BookingSlot;
import com.nowait.domain.repository.BookingRepository;
import com.nowait.domain.repository.BookingSlotRepository;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
Expand All @@ -25,9 +23,9 @@ public class BookingService {

private final BookingSlotRepository bookingSlotRepository;
private final BookingRepository bookingRepository;
private final UserRepository userRepository;
private final PlaceRepository placeRepository;
private final BookingEventPublisher bookingEventPublisher;
private final UserService userService;
private final PlaceService placeService;

public DailyBookingStatusRes getDailyBookingStatus(Long placeId, LocalDate date) {
List<BookingSlot> bookingSlots = bookingSlotRepository.findAllByPlaceIdAndDate(
Expand All @@ -45,11 +43,11 @@ public DailyBookingStatusRes getDailyBookingStatus(Long placeId, LocalDate date)
@Transactional
public BookingRes book(Long loginId, Long placeId, LocalDate date, LocalTime time,
Integer partySize) {
validateUserExist(loginId, "존재하지 않는 사용자의 요청입니다.");
validatePlaceExist(placeId, "존재하지 않는 식당입니다.");
userService.validateUserExist(loginId, "존재하지 않는 사용자의 요청입니다.");
placeService.validatePlaceExist(placeId, "존재하지 않는 식당입니다.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이름이 validateUserExist이니까, errorMessage도 userService가 가지고 있는 것이 더 바람직하지 않을까요? :-)

Copy link
Collaborator Author

@uijin31 uijin31 Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loginId를 확인하는 것이면, "존재하지 않는 사용자의 요청입니다"가 필요하고, 다른 서비스에서 사용자를 조회할 때는 "존재하지 않는 예약자입니다" 이런식으로 사용하는 입장에서 다른 에러 메시지를 전달해주고 싶을 것 같다고 생각해서 errorMessage도 매개변수에 포함 시켰습니다!
차라리 메서드를 오버로딩 해서 필요할 때만 errorMessage를 추가하도록 하는 것이 나을까요? 아니면 에러 메시지를 구체적으로 내려주지 말고 하나로 통일하는 것이 나을까요..🤔 아니면 혹시 메서드명이 잘못된 것일까요?😅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아, 이해했습니다. 이런 경우라면, vlidateUserExist를 boolean으로 하고, 그걸 받아서 에러를 내보내는 로직을 BookingService에 넣는 것이 더 좋을 것 같다는 생각이 드네요. 😎

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c22a745 반영했습니다:)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the above

BookingSlot slot = findAvailableSlot(placeId, date, time);
Booking booking = bookingRepository.save(Booking.of(loginId, partySize, slot));
Booking booking = bookingRepository.save(Booking.of(loginId, slot, partySize));

bookingEventPublisher.publishBookedEvent(booking, placeId);

Expand All @@ -61,18 +59,6 @@ private boolean isAvailable(List<BookingSlot> slots) {
return slots.stream().anyMatch(slot -> !slot.isBooked());
}

private void validateUserExist(Long userId, String errorMessage) {
if (!userRepository.existsById(userId)) {
throw new EntityNotFoundException(errorMessage);
}
}

private void validatePlaceExist(Long placeId, String errorMessage) {
if (!placeRepository.existsById(placeId)) {
throw new EntityNotFoundException(errorMessage);
}
}

private BookingSlot findAvailableSlot(Long placeId, LocalDate date, LocalTime time) {
return bookingSlotRepository.findFirstByPlaceIdAndDateAndTimeAndIsBookedFalse(placeId, date,
time).orElseThrow(() -> new IllegalArgumentException("예약 가능한 테이블이 없습니다."));
Expand Down
21 changes: 21 additions & 0 deletions nowait-api/src/main/java/com/nowait/application/PlaceService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.nowait.application;

import com.nowait.domain.repository.PlaceRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PlaceService {

private final PlaceRepository placeRepository;

public void validatePlaceExist(Long placeId, String errorMessage) {
if (!placeRepository.existsById(placeId)) {
throw new EntityNotFoundException(errorMessage);
}
}
}
22 changes: 22 additions & 0 deletions nowait-api/src/main/java/com/nowait/application/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nowait.application;

import com.nowait.domain.repository.UserRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserService {

private final UserRepository userRepository;

public void validateUserExist(Long userId, String errorMessage) {
if (!userRepository.existsById(userId)) {
throw new EntityNotFoundException(errorMessage);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.auth.dto.response;
package com.nowait.application.dto.response.auth;

public record GetLoginPageRes(
String loginPageUrl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.auth.dto.response;
package com.nowait.application.dto.response.auth;

public record LoginRes(
String accessToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nowait.booking.dto.response;
package com.nowait.application.dto.response.booking;

import com.nowait.booking.domain.model.Booking;
import com.nowait.booking.domain.model.BookingSlot;
import com.nowait.domain.model.booking.Booking;
import com.nowait.domain.model.booking.BookingSlot;

public record BookingRes(
Long bookingId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nowait.booking.dto.response;
package com.nowait.application.dto.response.booking;

import com.nowait.booking.dto.TimeSlotDto;
import java.time.LocalDate;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.booking.dto.response;
package com.nowait.application.dto.response.booking;

import static com.fasterxml.jackson.annotation.JsonFormat.Shape.STRING;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.booking.dto.response;
package com.nowait.application.dto.response.booking;

public record GetDepositInfoRes(
Long bookingId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.booking.dto;
package com.nowait.application.dto.response.booking;

import static com.fasterxml.jackson.annotation.JsonFormat.Shape.STRING;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nowait.application.dto.response.payment;

public record PayDepositRes(
Long paymentId,
String paymentStatus,
String paymentMethod,
String url
) {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.common.event;
package com.nowait.application.event;

public record BookedEvent(
Long bookingId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nowait.booking.application;
package com.nowait.application.event;

import com.nowait.booking.domain.model.Booking;
import com.nowait.common.event.BookedEvent;
import com.nowait.domain.model.booking.Booking;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Async;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.common.config;
package com.nowait.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.nowait.auth.api;
package com.nowait.controller.api;

import com.nowait.auth.dto.request.ReissueTokenReq;
import com.nowait.auth.dto.response.GetLoginPageRes;
import com.nowait.auth.dto.response.LoginRes;
import com.nowait.common.api.dto.ApiResult;
import com.nowait.application.dto.response.auth.GetLoginPageRes;
import com.nowait.application.dto.response.auth.LoginRes;
import com.nowait.controller.api.dto.request.ReissueTokenReq;
import com.nowait.controller.api.dto.response.ApiResult;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.nowait.booking.api;
package com.nowait.controller.api;

import com.nowait.booking.application.BookingService;
import com.nowait.booking.dto.TimeSlotDto;
import com.nowait.booking.dto.request.BookingReq;
import com.nowait.booking.dto.response.BookingRes;
import com.nowait.booking.dto.response.DailyBookingStatusRes;
import com.nowait.booking.dto.response.GetBookingInfoRes;
import com.nowait.booking.dto.response.GetDepositInfoRes;
import com.nowait.common.api.dto.ApiResult;
import com.nowait.application.BookingService;
import com.nowait.application.dto.response.booking.BookingRes;
import com.nowait.application.dto.response.booking.DailyBookingStatusRes;
import com.nowait.application.dto.response.booking.GetBookingInfoRes;
import com.nowait.application.dto.response.booking.GetDepositInfoRes;
import com.nowait.application.dto.response.booking.TimeSlotDto;
import com.nowait.controller.api.dto.request.BookingReq;
import com.nowait.controller.api.dto.response.ApiResult;
import jakarta.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nowait.notification.api;
package com.nowait.controller.api;

import com.nowait.common.api.dto.ApiResult;
import com.nowait.notification.dto.request.SetVacancyNotificationReq;
import com.nowait.controller.api.dto.request.SetVacancyNotificationReq;
import com.nowait.controller.api.dto.response.ApiResult;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down
32 changes: 32 additions & 0 deletions nowait-api/src/main/java/com/nowait/controller/api/PaymentApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nowait.controller.api;

import com.nowait.application.dto.response.payment.PayDepositRes;
import com.nowait.controller.api.dto.request.PayDepositReq;
import com.nowait.controller.api.dto.response.ApiResult;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/payments")
@RequiredArgsConstructor
public class PaymentApi {

/**
* 예약금 결제 API
*
* @param request 예약금 결제 요청
* @return 예약금 결제 결과
*/
@PostMapping("/deposit")
public ApiResult<PayDepositRes> payDeposit(
@RequestBody @Valid PayDepositReq request
) {
// TODO: 예약금 결제 비즈니스 로직 호출
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return ApiResult.ok(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.booking.dto.request;
package com.nowait.controller.api.dto.request;

import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nowait.controller.api.dto.request;

public record PayDepositReq(
Long bookingId,
String paymentMethod,
Integer amount,
String currency
) {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.auth.dto.request;
package com.nowait.controller.api.dto.request;

import jakarta.validation.constraints.NotBlank;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.notification.dto.request;
package com.nowait.controller.api.dto.request;

import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nowait.common.api.dto;
package com.nowait.controller.api.dto.response;

import org.springframework.http.HttpStatus;

/**
* API 응답을 표준화하기 위한 제네릭 레코드(Record)
*
* <p>
* 이 클래스는 API 응답에서 반환되는 상태 코드, 상태 메시지, 응답 데이터 등을 포함하여
* 일관된 응답 형식을 제공하기 위해 설계되었습니다.
*/
Expand All @@ -14,6 +14,7 @@ public record ApiResult<T>(
String message,
T data
) {

private ApiResult(HttpStatus status, String message, T data) {
this(status.value(), status, message, data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nowait.common.domain.model;
package com.nowait.domain.model;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
Expand Down
Loading
Loading