-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathReservationService.java
More file actions
190 lines (153 loc) · 6.57 KB
/
ReservationService.java
File metadata and controls
190 lines (153 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.ceos23.spring_boot.service;
import com.ceos23.spring_boot.domain.Reservation;
import com.ceos23.spring_boot.domain.Screening;
import com.ceos23.spring_boot.domain.Seat;
import com.ceos23.spring_boot.domain.User;
import com.ceos23.spring_boot.exception.CustomException;
import com.ceos23.spring_boot.global.exception.ErrorCode;
import com.ceos23.spring_boot.infra.payment.PaymentGateway;
import com.ceos23.spring_boot.infra.payment.dto.PaymentData;
import com.ceos23.spring_boot.repository.ReservationRepository;
import com.ceos23.spring_boot.repository.ScreeningRepository;
import com.ceos23.spring_boot.repository.SeatRepository;
import com.ceos23.spring_boot.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Service
@RequiredArgsConstructor
public class ReservationService {
private static final int TICKET_PRICE = 15000;
private static final DateTimeFormatter PAYMENT_ID_FORMATTER =
DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
private final ReservationRepository reservationRepository;
private final UserRepository userRepository;
private final ScreeningRepository screeningRepository;
private final SeatRepository seatRepository;
private final PaymentGateway paymentGateway;
private final ReservationTransactionService reservationTransactionService;
@Transactional
public Reservation reserve(Long userId, Long screeningId, Long seatId) {
ensureSeatNotReserved(screeningId, seatId);
User user = loadUser(userId);
Screening screening = loadScreening(screeningId);
Seat seat = loadSeat(seatId);
Reservation reservation = Reservation.create(user, screening, seat);
try {
return reservationRepository.saveAndFlush(reservation);
} catch (DataIntegrityViolationException e) {
throw new CustomException(ErrorCode.SEAT_ALREADY_RESERVED);
}
}
public Reservation payReservation(Long reservationId) {
Reservation reservation = loadReservation(reservationId);
if (reservation.isExpired(LocalDateTime.now())) {
reservationTransactionService.deleteReservation(reservationId);
throw new CustomException(ErrorCode.RESERVATION_EXPIRED);
}
if (reservation.isPaid()) {
return reservation;
}
String paymentId = createPaymentId(reservation.getId());
try {
PaymentData payment = paymentGateway.pay(
paymentId,
createOrderName(reservation),
TICKET_PRICE,
createCustomData(reservation)
);
return reservationTransactionService.markPaid(
reservationId,
paymentId,
resolvePaidAt(payment)
);
} catch (CustomException e) {
return handlePaymentException(e, reservationId, reservation, paymentId);
}
}
public void cancel(Long reservationId) {
Reservation reservation = loadReservation(reservationId);
if (reservation.isPaid()) {
paymentGateway.cancel(reservation.getPaymentId());
}
reservationTransactionService.deleteReservation(reservationId);
}
private Reservation handlePaymentException(
CustomException e,
Long reservationId,
Reservation reservation,
String paymentId
) {
if (e.getErrorCode() == ErrorCode.PAYMENT_SERVER_ERROR) {
return retryPayment(reservationId, reservation, paymentId);
}
if (e.getErrorCode() == ErrorCode.PAYMENT_CONFLICT) {
PaymentData payment = paymentGateway.getPayment(paymentId);
return reservationTransactionService.markPaid(
reservationId,
paymentId,
resolvePaidAt(payment)
);
}
reservationTransactionService.deleteReservation(reservationId);
throw e;
}
private Reservation retryPayment(Long reservationId, Reservation reservation, String paymentId) {
try {
PaymentData payment = paymentGateway.pay(
paymentId,
createOrderName(reservation),
TICKET_PRICE,
createCustomData(reservation)
);
return reservationTransactionService.markPaid(
reservationId,
paymentId,
resolvePaidAt(payment)
);
} catch (CustomException retryException) {
reservationTransactionService.deleteReservation(reservationId);
throw new CustomException(ErrorCode.PAYMENT_RETRY_FAILED);
}
}
private void ensureSeatNotReserved(Long screeningId, Long seatId) {
boolean exists = reservationRepository.existsByScreeningIdAndSeatId(screeningId, seatId);
if (exists) {
throw new CustomException(ErrorCode.SEAT_ALREADY_RESERVED);
}
}
private Reservation loadReservation(Long reservationId) {
return reservationRepository.findById(reservationId)
.orElseThrow(() -> new CustomException(ErrorCode.RESERVATION_NOT_FOUND));
}
private User loadUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
}
private Screening loadScreening(Long screeningId) {
return screeningRepository.findById(screeningId)
.orElseThrow(() -> new CustomException(ErrorCode.SCREENING_NOT_FOUND));
}
private Seat loadSeat(Long seatId) {
return seatRepository.findWithLockById(seatId)
.orElseThrow(() -> new CustomException(ErrorCode.SEAT_NOT_FOUND));
}
private String createPaymentId(Long reservationId) {
return "RSV_" + reservationId + "_" + LocalDateTime.now().format(PAYMENT_ID_FORMATTER);
}
private String createOrderName(Reservation reservation) {
return "CGV 좌석 예매 #" + reservation.getId();
}
private String createCustomData(Reservation reservation) {
return "RESERVATION:" + reservation.getId();
}
private LocalDateTime resolvePaidAt(PaymentData payment) {
if (payment.paidAt() != null) {
return payment.paidAt();
}
return LocalDateTime.now();
}
}