-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathReservation.java
More file actions
213 lines (175 loc) · 7.8 KB
/
Copy pathReservation.java
File metadata and controls
213 lines (175 loc) · 7.8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cgv_23rd.ceos.entity.reservation;
import cgv_23rd.ceos.entity.BaseEntity;
import cgv_23rd.ceos.entity.enums.PaymentStatus;
import cgv_23rd.ceos.entity.enums.ReservationStatus;
import cgv_23rd.ceos.entity.movie.MovieScreen;
import cgv_23rd.ceos.entity.theater.Seat;
import cgv_23rd.ceos.entity.user.User;
import cgv_23rd.ceos.global.apiPayload.code.GeneralErrorCode;
import cgv_23rd.ceos.global.apiPayload.exception.GeneralException;
import jakarta.persistence.*;
import lombok.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE) // 빌더용, 외부 생성 방지
@Builder(access = AccessLevel.PRIVATE)
@Table(
indexes = {
@Index(name = "idx_reservation_status_created_at", columnList = "status, createdAt")
}
)
public class Reservation extends BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "movie_screen_id", nullable = false)
private MovieScreen movieScreen;
private Integer totalPrice;
@Enumerated(EnumType.STRING)
private ReservationStatus status;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private PaymentStatus paymentStatus;
@Column(length = 100)
private String paymentId;
@OneToMany(mappedBy = "reservation", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ReservationSeat> reservationSeats = new ArrayList<>();
public static Reservation create(User user, MovieScreen movieScreen, LocalDateTime now) {
// 상영 시작 여부 검사 로직을 엔티티 내부로 이동
if (movieScreen.getStartAt().isBefore(now)) {
throw new GeneralException(GeneralErrorCode.MOVIE_ALREADY_STARTED);
}
return Reservation.builder()
.user(user)
.movieScreen(movieScreen)
.status(ReservationStatus.대기)
.paymentStatus(PaymentStatus.READY)
.totalPrice(0)
.paymentId(null)
.reservationSeats(new ArrayList<>())
.build();
}
public void assignPaymentId(String paymentId) {
if (this.status != ReservationStatus.대기) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY);
}
if (this.paymentStatus == PaymentStatus.PROCESSING || this.paymentStatus == PaymentStatus.PAID) {
throw new GeneralException(GeneralErrorCode.PAYMENT_ALREADY_PROCESSED, "이미 결제가 진행 중이거나 완료된 예매입니다.");
}
if (this.paymentId != null && !this.paymentId.isBlank()) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "이미 결제 식별자가 할당된 예매입니다.");
}
this.paymentId = paymentId;
this.paymentStatus = PaymentStatus.PROCESSING;
}
public void confirm() {
if (this.status != ReservationStatus.대기) {
throw new GeneralException(GeneralErrorCode.PAYMENT_ALREADY_PROCESSED);
}
if (this.paymentId == null || this.paymentId.isBlank()) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "결제 식별자가 없는 예매입니다.");
}
if (this.paymentStatus != PaymentStatus.PAID) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "결제 완료 상태의 예매만 확정할 수 있습니다.");
}
this.status = ReservationStatus.완료;
}
public void markPaymentPaid() {
validateTransitionTo(PaymentStatus.PAID, EnumSet.of(PaymentStatus.PROCESSING), EnumSet.of(ReservationStatus.대기));
this.paymentStatus = PaymentStatus.PAID;
}
public void markPaymentFailed() {
if (this.paymentStatus == PaymentStatus.FAILED) {
return;
}
validateTransitionTo(PaymentStatus.FAILED, EnumSet.of(PaymentStatus.PROCESSING), EnumSet.of(ReservationStatus.대기));
this.paymentStatus = PaymentStatus.FAILED;
}
public void markPaymentUnknown() {
if (this.paymentStatus == PaymentStatus.UNKNOWN) {
return;
}
validateTransitionTo(PaymentStatus.UNKNOWN, EnumSet.of(PaymentStatus.PROCESSING), EnumSet.of(ReservationStatus.대기));
this.paymentStatus = PaymentStatus.UNKNOWN;
}
public void markPaymentCancelled() {
if (this.paymentStatus == PaymentStatus.CANCELLED) {
return;
}
validateTransitionTo(PaymentStatus.CANCELLED,
EnumSet.of(PaymentStatus.PAID, PaymentStatus.UNKNOWN),
EnumSet.of(ReservationStatus.대기, ReservationStatus.완료));
this.paymentStatus = PaymentStatus.CANCELLED;
}
public void validateCancelable(LocalDateTime now) {
if (this.status == ReservationStatus.취소) {
throw new GeneralException(GeneralErrorCode.RESERVATION_ALREADY_CANCELED);
}
if (this.movieScreen.getStartAt().isBefore(now)) {
throw new GeneralException(GeneralErrorCode.MOVIE_ALREADY_STARTED);
}
}
// 예매 취소 편의 메서드
public void cancel(LocalDateTime now) {
validateCancelable(now);
this.status = ReservationStatus.취소;
this.reservationSeats.clear();
}
public boolean isOwnedBy(Long userId) {
return this.user.getId().equals(userId);
}
public void addSeat(Seat seat) {
if (!seat.getScreen().getId().equals(this.movieScreen.getScreen().getId())) {
throw new GeneralException(GeneralErrorCode.SEAT_SCREEN_INVALID);
}
Integer price = this.movieScreen.getScreen().getScreenType().getBasePrice();
ReservationSeat reservationSeat = ReservationSeat.builder()
.reservation(this)
.seat(seat)
.movieScreen(this.movieScreen)
.price(price)
.build();
this.reservationSeats.add(reservationSeat);
this.totalPrice += price;
}
public String getMovieTitle() {
return this.movieScreen.getMovie().getTitle();
}
public String getTheaterName() {
return this.movieScreen.getScreen().getTheater().getName();
}
public List<String> getSeatLabels() {
return this.reservationSeats.stream()
.map(rs -> rs.getSeat().getRowName() + rs.getSeat().getColNum())
.toList();
}
public String getScreenName() {
return this.movieScreen.getScreen().getName();
}
private void validatePaymentIdExists() {
if (this.paymentId == null || this.paymentId.isBlank()) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "결제 식별자가 없는 예매입니다.");
}
}
private void validateTransitionTo(PaymentStatus targetStatus,
EnumSet<PaymentStatus> allowedPaymentStatuses,
EnumSet<ReservationStatus> allowedReservationStatuses) {
validatePaymentIdExists();
if (!allowedReservationStatuses.contains(this.status)) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY,
"현재 예매 상태에서는 결제 상태를 " + targetStatus + "로 변경할 수 없습니다.");
}
if (!allowedPaymentStatuses.contains(this.paymentStatus)) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY,
"현재 결제 상태에서는 " + targetStatus + "로 변경할 수 없습니다. current=" + this.paymentStatus);
}
}
}