Skip to content

Commit

Permalink
refactor: 회원 도메인 연동 (#142)
Browse files Browse the repository at this point in the history
* feat: 멤버 예매 연동

* test: 테스트코드 멤버 연동

* test: 테스트코드 멤버 생성 수정

* refactor: booking의 member nullable=false 설정 추가

* fix: 불필요한 출력 제거

* fix: custom exception으로 처리
  • Loading branch information
byulcode authored Jan 7, 2024
1 parent 2d7a75b commit 26e891e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.pgms.apibooking.domain.booking.dto.response.BookingCreateResponse;
import com.pgms.apibooking.domain.booking.service.BookingService;
import com.pgms.coredomain.response.ApiResponse;
import com.pgms.coresecurity.security.resolver.CurrentAccount;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
Expand All @@ -29,9 +30,10 @@ public class BookingController {

@PostMapping
public ResponseEntity<ApiResponse<BookingCreateResponse>> createBooking(
@CurrentAccount Long memberId,
@RequestBody @Valid BookingCreateRequest request,
HttpServletRequest httpRequest) {
BookingCreateResponse createdBooking = bookingService.createBooking(request);
BookingCreateResponse createdBooking = bookingService.createBooking(request, memberId);
ApiResponse<BookingCreateResponse> response = ApiResponse.ok(createdBooking);
URI location = UriComponentsBuilder
.fromHttpUrl(httpRequest.getRequestURL().toString())
Expand All @@ -43,9 +45,10 @@ public ResponseEntity<ApiResponse<BookingCreateResponse>> createBooking(

@PostMapping("/{id}/cancel")
public ResponseEntity<Void> cancelBooking(
@CurrentAccount Long memberId,
@PathVariable String id,
@RequestBody @Valid BookingCancelRequest request) {
bookingService.cancelBooking(id, request);
bookingService.cancelBooking(id, request, memberId);
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pgms.apibooking.domain.booking.service;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -29,6 +31,8 @@
import com.pgms.coredomain.domain.booking.Ticket;
import com.pgms.coredomain.domain.event.repository.EventSeatRepository;
import com.pgms.coredomain.domain.event.repository.EventTimeRepository;
import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.repository.MemberRepository;

import lombok.RequiredArgsConstructor;

Expand All @@ -41,17 +45,19 @@ public class BookingService { //TODO: 테스트 코드 작성
private final EventSeatRepository eventSeatRepository;
private final BookingRepository bookingRepository;
private final TicketRepository ticketRepository;
private final MemberRepository memberRepository;
private final TossPaymentConfig tossPaymentConfig;
private final PaymentService paymentService;

public BookingCreateResponse createBooking(BookingCreateRequest request) {
public BookingCreateResponse createBooking(BookingCreateRequest request, Long memberId) {
Member member = getMemberById(memberId);
EventTime time = getBookableTimeWithEvent(request.timeId());
List<EventSeat> seats = getBookableSeatsWithArea(request.timeId(), request.seatIds());

ReceiptType receiptType = ReceiptType.fromDescription(request.receiptType());
validateDeliveryAddress(receiptType, request.deliveryAddress());

Booking booking = BookingCreateRequest.toEntity(request, time, seats, null); //TODO: 인증된 멤버 지정
Booking booking = BookingCreateRequest.toEntity(request, time, seats, member);

seats.forEach(seat -> booking.addTicket(
Ticket.builder()
Expand All @@ -73,10 +79,15 @@ public BookingCreateResponse createBooking(BookingCreateRequest request) {
return BookingCreateResponse.of(booking, tossPaymentConfig.getSuccessUrl(), tossPaymentConfig.getFailUrl());
}

public void cancelBooking(String id, BookingCancelRequest request) {
public void cancelBooking(String id, BookingCancelRequest request, Long memberId) {
Member member = getMemberById(memberId);
Booking booking = bookingRepository.findBookingInfoById(id)
.orElseThrow(() -> new BookingException(BookingErrorCode.BOOKING_NOT_FOUND));

if (!Objects.equals(member, booking.getMember())) {
throw new BookingException(BookingErrorCode.NOT_SAME_BOOKER);
}

if (!booking.isCancelable()) {
throw new BookingException(BookingErrorCode.UNCANCELABLE_BOOKING);
}
Expand All @@ -95,7 +106,7 @@ public void cancelBooking(String id, BookingCancelRequest request) {
);

booking.cancel(
BookingCancelRequest.toEntity(request, cancelAmount, "사용자", booking) //TODO: 취소 요청자 지정
BookingCancelRequest.toEntity(request, cancelAmount, member.getEmail(), booking)
);
}

Expand Down Expand Up @@ -143,4 +154,10 @@ private void validateRefundReceiveAccount(PaymentMethod paymentMethod,
throw new BookingException(BookingErrorCode.REFUND_ACCOUNT_REQUIRED);
}
}

private Member getMemberById(Long memberId) {
System.out.println("member id get " + memberId);
return memberRepository.findById(memberId)
.orElseThrow(() -> new NoSuchElementException("Member not found"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum BookingErrorCode {
UNBOOKABLE_SEAT_INCLUSION(HttpStatus.BAD_REQUEST, "UNBOOKABLE_SEAT_INCLUSION", "예매가 불가능한 좌석이 포함되어 있습니다."),
DELIVERY_ADDRESS_REQUIRED(HttpStatus.BAD_REQUEST, "DELIVERY_ADDRESS_REQUIRED", "배송지 정보를 입력해주세요."),

NOT_SAME_BOOKER(HttpStatus.BAD_REQUEST, "NOT_SAME_BOOKER", "해당 예약의 예약자가 아닙니다."),
UNCANCELABLE_BOOKING(HttpStatus.BAD_REQUEST, "UNCANCELABLE_BOOKING", "취소할 수 없는 예매입니다."),
REFUND_ACCOUNT_REQUIRED(HttpStatus.BAD_REQUEST, "REFUND_ACCOUNT_REQUIRED", "환불 받을 계좌 정보를 입력해주세요."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.Transactional;

import com.pgms.apibooking.domain.booking.service.BookingService;
import com.pgms.apibooking.config.TestConfig;
import com.pgms.apibooking.domain.booking.dto.request.BookingCancelRequest;
import com.pgms.apibooking.domain.booking.dto.request.BookingCreateRequest;
import com.pgms.apibooking.domain.payment.dto.request.RefundAccountRequest;
import com.pgms.apibooking.domain.booking.dto.response.BookingCreateResponse;
import com.pgms.apibooking.domain.booking.service.BookingService;
import com.pgms.apibooking.domain.payment.dto.request.RefundAccountRequest;
import com.pgms.apibooking.exception.BookingErrorCode;
import com.pgms.apibooking.exception.BookingException;
import com.pgms.apibooking.factory.BookingFactory;
Expand Down Expand Up @@ -49,6 +50,11 @@
import com.pgms.coredomain.domain.event.repository.EventSeatAreaRepository;
import com.pgms.coredomain.domain.event.repository.EventSeatRepository;
import com.pgms.coredomain.domain.event.repository.EventTimeRepository;
import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.domain.member.Role;
import com.pgms.coredomain.domain.member.enums.Provider;
import com.pgms.coredomain.domain.member.repository.MemberRepository;
import com.pgms.coredomain.domain.member.repository.RoleRepository;

@SpringBootTest
@Import(TestConfig.class)
Expand Down Expand Up @@ -77,6 +83,26 @@ class BookingServiceTest {

@Autowired
private BookingService bookingService;
@Autowired
private MemberRepository memberRepository;

@Autowired
private RoleRepository roleRepository;

private Member member;

@BeforeEach
void setup() {
Role role = roleRepository.save(new Role("ROLE_USER"));
member = memberRepository.save(Member.builder()
.email("[email protected]")
.password("test1234!")
.name("홍길동")
.provider(Provider.KAKAO)
.role(role)
.phoneNumber("010-123-456")
.build());
}

@Test
void 예매를_생성한다() {
Expand Down Expand Up @@ -130,7 +156,7 @@ class BookingServiceTest {
);

// when
BookingCreateResponse response = bookingService.createBooking(request);
BookingCreateResponse response = bookingService.createBooking(request, member.getId());

// then
Booking booking = bookingRepository.findBookingInfoById(response.bookingId()).get();
Expand Down Expand Up @@ -201,7 +227,7 @@ class BookingServiceTest {
);

// when & then
assertThatThrownBy(() -> bookingService.createBooking(request))
assertThatThrownBy(() -> bookingService.createBooking(request, member.getId()))
.isInstanceOf(BookingException.class)
.hasMessage(BookingErrorCode.UNBOOKABLE_EVENT.getMessage());
}
Expand Down Expand Up @@ -253,7 +279,7 @@ class BookingServiceTest {
);

// when & then
assertThatThrownBy(() -> bookingService.createBooking(request))
assertThatThrownBy(() -> bookingService.createBooking(request, member.getId()))
.isInstanceOf(BookingException.class)
.hasMessage(BookingErrorCode.NON_EXISTENT_SEAT_INCLUSION.getMessage());
}
Expand Down Expand Up @@ -303,7 +329,7 @@ class BookingServiceTest {
);

// when & then
assertThatThrownBy(() -> bookingService.createBooking(request))
assertThatThrownBy(() -> bookingService.createBooking(request, member.getId()))
.isInstanceOf(BookingException.class)
.hasMessage(BookingErrorCode.UNBOOKABLE_SEAT_INCLUSION.getMessage());
}
Expand Down Expand Up @@ -353,7 +379,7 @@ class BookingServiceTest {
);

// when & then
assertThatThrownBy(() -> bookingService.createBooking(request))
assertThatThrownBy(() -> bookingService.createBooking(request, member.getId()))
.isInstanceOf(BookingException.class)
.hasMessage(BookingErrorCode.DELIVERY_ADDRESS_REQUIRED.getMessage());
}
Expand Down Expand Up @@ -388,7 +414,7 @@ class BookingServiceTest {
eventSeatRepository.save(seat);

Booking booking = BookingFactory.generate(
null,
member,
time,
seat.getEventSeatArea().getPrice(),
BookingStatus.PAYMENT_COMPLETED
Expand All @@ -407,7 +433,7 @@ class BookingServiceTest {
);

// when
bookingService.cancelBooking(booking.getId(), request);
bookingService.cancelBooking(booking.getId(), request, member.getId());

// then
Booking canceledBooking = bookingRepository.findBookingInfoById(booking.getId()).get();
Expand Down Expand Up @@ -455,7 +481,7 @@ class BookingServiceTest {
eventSeatRepository.save(seat);

Booking booking = BookingFactory.generate(
null,
member,
time,
seat.getEventSeatArea().getPrice(),
BookingStatus.CANCELED
Expand All @@ -474,7 +500,7 @@ class BookingServiceTest {
);

// when & then
assertThatThrownBy(() -> bookingService.cancelBooking(booking.getId(), request))
assertThatThrownBy(() -> bookingService.cancelBooking(booking.getId(), request, member.getId()))
.isInstanceOf(BookingException.class)
.hasMessage(BookingErrorCode.UNCANCELABLE_BOOKING.getMessage());
}
Expand Down Expand Up @@ -509,7 +535,7 @@ class BookingServiceTest {
eventSeatRepository.save(seat);

Booking booking = BookingFactory.generate(
null,
member,
time,
seat.getEventSeatArea().getPrice(),
BookingStatus.PAYMENT_COMPLETED
Expand All @@ -529,7 +555,7 @@ class BookingServiceTest {
);

// when & then
assertThatThrownBy(() -> bookingService.cancelBooking(booking.getId(), request))
assertThatThrownBy(() -> bookingService.cancelBooking(booking.getId(), request, member.getId()))
.isInstanceOf(BookingException.class)
.hasMessage(BookingErrorCode.REFUND_ACCOUNT_REQUIRED.getMessage());
}
Expand Down Expand Up @@ -564,7 +590,7 @@ class BookingServiceTest {
eventSeatRepository.save(seat);

Booking booking = BookingFactory.generate(
null,
member,
time,
seat.getEventSeatArea().getPrice(),
BookingStatus.PAYMENT_COMPLETED
Expand All @@ -583,7 +609,7 @@ class BookingServiceTest {
);

// when
bookingService.cancelBooking(booking.getId(), request);
bookingService.cancelBooking(booking.getId(), request, member.getId());

// then
Booking canceledBooking = bookingRepository.findBookingInfoById(booking.getId()).get();
Expand Down Expand Up @@ -620,7 +646,7 @@ class BookingServiceTest {
eventSeatRepository.save(seat);

Booking booking = BookingFactory.generate(
null,
member,
time,
seat.getEventSeatArea().getPrice(),
BookingStatus.WAITING_FOR_PAYMENT
Expand All @@ -646,7 +672,7 @@ class BookingServiceTest {
);

// when
bookingService.cancelBooking(booking.getId(), request);
bookingService.cancelBooking(booking.getId(), request, member.getId());

// then
Booking canceledBooking = bookingRepository.findBookingInfoById(booking.getId()).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class Booking extends BaseEntity {
private Integer amount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) //TODO: nullalbe = false 추가
@JoinColumn(name = "member_id", nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down

0 comments on commit 26e891e

Please sign in to comment.