-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGiftBoxService.java
More file actions
352 lines (297 loc) · 13.8 KB
/
GiftBoxService.java
File metadata and controls
352 lines (297 loc) · 13.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.dilly.gift.application;
import com.dilly.admin.adaptor.AdminGiftBoxReader;
import com.dilly.exception.ErrorCode;
import com.dilly.exception.GiftBoxAccessDeniedException;
import com.dilly.exception.GiftBoxAlreadyOpenedException;
import com.dilly.exception.UnsupportedException;
import com.dilly.gift.adaptor.BoxReader;
import com.dilly.gift.adaptor.EnvelopeReader;
import com.dilly.gift.adaptor.GiftBoxReader;
import com.dilly.gift.adaptor.GiftBoxStickerReader;
import com.dilly.gift.adaptor.GiftBoxStickerWriter;
import com.dilly.gift.adaptor.GiftBoxWriter;
import com.dilly.gift.adaptor.LastViewedAdminTypeReader;
import com.dilly.gift.adaptor.LastViewedAdminTypeWriter;
import com.dilly.gift.adaptor.LetterWriter;
import com.dilly.gift.adaptor.PhotoReader;
import com.dilly.gift.adaptor.PhotoWriter;
import com.dilly.gift.adaptor.ReceiverReader;
import com.dilly.gift.application.strategy.GiftBoxActionProvider;
import com.dilly.gift.application.strategy.GiftBoxStrategy;
import com.dilly.gift.domain.Box;
import com.dilly.gift.domain.gift.Gift;
import com.dilly.gift.domain.gift.GiftType;
import com.dilly.gift.domain.giftbox.DeliverStatus;
import com.dilly.gift.domain.giftbox.GiftBox;
import com.dilly.gift.domain.giftbox.MemberRole;
import com.dilly.gift.domain.giftbox.admin.AdminGiftBox;
import com.dilly.gift.domain.giftbox.admin.AdminType;
import com.dilly.gift.domain.giftbox.admin.LastViewedAdminType;
import com.dilly.gift.domain.letter.Envelope;
import com.dilly.gift.domain.letter.Letter;
import com.dilly.gift.domain.receiver.Receiver;
import com.dilly.gift.dto.request.DeliverStatusRequest;
import com.dilly.gift.dto.request.GiftBoxRequest;
import com.dilly.gift.dto.response.BoxResponse;
import com.dilly.gift.dto.response.EnvelopeResponse;
import com.dilly.gift.dto.response.GiftBoxIdResponse;
import com.dilly.gift.dto.response.GiftBoxResponse;
import com.dilly.gift.dto.response.GiftBoxesResponse;
import com.dilly.gift.dto.response.GiftResponseDto.GiftResponse;
import com.dilly.gift.dto.response.KakaoImgResponse;
import com.dilly.gift.dto.response.MainGiftBoxResponse;
import com.dilly.gift.dto.response.PhotoResponseDto.PhotoResponse;
import com.dilly.gift.dto.response.StickerResponse;
import com.dilly.gift.dto.response.WaitingGiftBoxResponse;
import com.dilly.global.util.validator.UuidValidator;
import com.dilly.member.application.MemberService;
import com.dilly.member.domain.Member;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class GiftBoxService {
private final GiftBoxActionProvider giftBoxActionProvider;
private final MemberService memberService;
private final GiftBoxReader giftBoxReader;
private final GiftBoxWriter giftBoxWriter;
private final BoxReader boxReader;
private final EnvelopeReader envelopeReader;
private final LetterWriter letterWriter;
private final PhotoReader photoReader;
private final PhotoWriter photoWriter;
private final GiftBoxStickerReader giftBoxStickerReader;
private final GiftBoxStickerWriter giftBoxStickerWriter;
private final ReceiverReader receiverReader;
private final AdminGiftBoxReader adminGiftBoxReader;
private final LastViewedAdminTypeReader lastViewedAdminTypeReader;
private final LastViewedAdminTypeWriter lastViewedAdminTypeWriter;
public GiftBoxIdResponse createGiftBox(GiftBoxRequest giftBoxRequest) {
Member sender = memberService.getMember();
Box box = boxReader.findById(giftBoxRequest.boxId());
Envelope envelope = envelopeReader.findById(giftBoxRequest.envelopeId());
Letter letter = letterWriter.save(giftBoxRequest.letterContent(), envelope);
GiftBox giftBox;
if (giftBoxRequest.gift() == null) {
giftBox = giftBoxWriter.save(box, letter, sender,
giftBoxRequest.name(), giftBoxRequest.youtubeUrl(),
giftBoxRequest.senderName(), giftBoxRequest.receiverName());
} else {
Gift gift = Gift.of(GiftType.valueOf(giftBoxRequest.gift().type().toUpperCase()),
giftBoxRequest.gift().url());
giftBox = giftBoxWriter.save(box, letter, gift, sender,
giftBoxRequest.name(), giftBoxRequest.youtubeUrl(),
giftBoxRequest.senderName(), giftBoxRequest.receiverName()
);
}
giftBoxRequest.photos()
.forEach(
photoRequest -> photoWriter.save(giftBox, photoRequest.photoUrl(),
photoRequest.description(), photoRequest.sequence())
);
giftBoxRequest.stickers()
.forEach(stickerRequest ->
giftBoxStickerWriter.save(giftBox, stickerRequest.id(), stickerRequest.location())
);
return GiftBoxIdResponse.of(giftBox.getId(), giftBox.getUuid(),
giftBox.getBox().getKakaoMessageImgUrl());
}
@RedissonLock(value = "#giftBoxId")
public GiftBoxResponse openGiftBox(Long giftBoxId) {
Member member = memberService.getMember();
GiftBox giftBox = giftBoxReader.findById(giftBoxId);
MemberRole memberRole = getMemberRole(member, giftBox);
final GiftBoxStrategy giftBoxStrategy = giftBoxActionProvider.getStrategy(memberRole);
giftBoxStrategy.open(member, giftBox);
return toGiftBoxResponse(giftBox);
}
// TODO: 삭제된 선물박스, 이미 열린 선물박스 등 로직 구체화
public GiftBoxResponse openGiftBoxForWeb(String giftBoxId) {
GiftBox giftBox;
if (UuidValidator.isValidUUID(giftBoxId)) {
giftBox = giftBoxReader.findByUuid(giftBoxId);
} else {
try {
giftBox = giftBoxReader.findById(Long.parseLong(giftBoxId));
List<Receiver> receiver = receiverReader.findByGiftBox(giftBox);
boolean hasReceiver = !receiver.isEmpty();
if (hasReceiver) {
throw new GiftBoxAlreadyOpenedException();
}
} catch (NumberFormatException e) {
throw new UnsupportedException(ErrorCode.INVALID_INPUT_VALUE);
}
}
// LocalDateTime sentDate = giftBox.getUpdatedAt();
// LocalDateTime now = LocalDateTime.now();
// if (now.minusDays(7).isAfter(sentDate)) {
// throw new UnsupportedException(ErrorCode.GIFTBOX_URL_EXPIRED);
// }
return toGiftBoxResponse(giftBox);
}
// TODO: 성능 개선 필요
public Slice<GiftBoxesResponse> getGiftBoxes(LocalDateTime lastGiftBoxDate, String type,
Pageable pageable) {
Member member = memberService.getMember();
if (type == null) {
type = "all";
}
Slice<GiftBox> giftBoxSlice;
List<GiftBoxesResponse> giftBoxesResponses = new ArrayList<>();
switch (type) {
case "sent" -> {
giftBoxSlice = giftBoxReader.searchSentGiftBoxesBySlice(member,
lastGiftBoxDate, pageable);
giftBoxesResponses = sliceToDto(giftBoxSlice, type, member);
}
case "received" -> {
giftBoxSlice = giftBoxReader.searchReceivedGiftBoxesBySlice(member,
lastGiftBoxDate, pageable);
giftBoxesResponses = sliceToDto(giftBoxSlice, type, member);
}
case "all" -> {
Comparator<Object> comparator = getCreatedAtComparator(member);
giftBoxSlice = giftBoxReader.searchAllGiftBoxesBySlice(member,
lastGiftBoxDate, comparator, pageable);
giftBoxesResponses = sliceToDto(giftBoxSlice, type, member);
}
default -> giftBoxSlice = new SliceImpl<>(List.of(), pageable, false);
}
return new SliceImpl<>(giftBoxesResponses, pageable, giftBoxSlice.hasNext());
}
private List<GiftBoxesResponse> sliceToDto(Slice<GiftBox> giftBoxSlice, String type,
Member member) {
switch (type) {
case "sent" -> {
return giftBoxSlice.getContent().stream()
.map(GiftBoxesResponse::of)
.toList();
}
case "received" -> {
return giftBoxSlice.getContent().stream()
.map(giftBox -> {
Receiver receiver = receiverReader.findByMemberAndGiftBox(member, giftBox);
return GiftBoxesResponse.of(receiver);
})
.toList();
}
case "all" -> {
return giftBoxSlice.getContent().stream()
.map(giftBox -> {
Receiver receiver = receiverReader.findByMemberAndGiftBox(member, giftBox);
return GiftBoxesResponse.of(giftBox, member, receiver);
})
.toList();
}
default -> throw new UnsupportedException(ErrorCode.UNSUPPORTED_GIFTBOX_TYPE);
}
}
private Comparator<Object> getCreatedAtComparator(Member member) {
return Comparator.comparing((Object obj) -> {
GiftBox giftBox = (GiftBox) obj;
LocalDateTime createdAt;
if (giftBox.getSender().equals(member)) {
createdAt = giftBox.getCreatedAt();
} else {
Receiver receiver = receiverReader.findByMemberAndGiftBox(member, giftBox);
createdAt = receiver.getCreatedAt();
}
return createdAt;
});
}
public String deleteGiftBox(Long giftBoxId) {
Member member = memberService.getMember();
GiftBox giftBox = giftBoxReader.findById(giftBoxId);
MemberRole memberRole = getMemberRole(member, giftBox);
final GiftBoxStrategy giftBoxStrategy = giftBoxActionProvider.getStrategy(memberRole);
giftBoxStrategy.delete(member, giftBox);
return "선물박스가 삭제되었습니다";
}
private MemberRole getMemberRole(Member member, GiftBox giftBox) {
List<Member> receivers = receiverReader.findByGiftBox(giftBox).stream()
.map(Receiver::getMember)
.toList();
if (giftBox.getSender().equals(member)) {
return MemberRole.SENDER;
}
if (receivers.isEmpty()) {
return MemberRole.POTENTIAL_RECEIVER;
}
if (receivers.contains(member)) {
return MemberRole.RECEIVER;
}
return MemberRole.STRANGER;
}
public String updateDeliverStatus(Long giftBoxId, DeliverStatusRequest deliverStatusRequest) {
Member member = memberService.getMember();
GiftBox giftBox = giftBoxReader.findById(giftBoxId);
if (!giftBox.getSender().equals(member)) {
throw new GiftBoxAccessDeniedException();
}
DeliverStatus deliverStatus;
try {
deliverStatus = DeliverStatus.valueOf(deliverStatusRequest.deliverStatus());
} catch (IllegalArgumentException e) {
throw new UnsupportedException(ErrorCode.UNSUPPORTED_DELIVER_TYPE);
}
giftBox.updateDeliverStatus(deliverStatus);
return "선물박스 전송 상태가 변경되었습니다";
}
public List<WaitingGiftBoxResponse> getWaitingGiftBoxes() {
Member member = memberService.getMember();
return giftBoxReader.findTop6BySenderAndDeliverStatusAndSenderDeletedOrderByCreatedAtDesc(
member, DeliverStatus.WAITING).stream()
.map(WaitingGiftBoxResponse::from)
.toList();
}
public KakaoImgResponse getKakaoMessageImgUrl(Long giftBoxId) {
GiftBox giftBox = giftBoxReader.findById(giftBoxId);
return KakaoImgResponse.from(giftBox.getBox().getKakaoMessageImgUrl());
}
public MainGiftBoxResponse getMainGiftBox() {
Member member = memberService.getMember();
Optional<LastViewedAdminType> lastViewedAdminType = lastViewedAdminTypeReader.findByMember(
member);
Optional<AdminGiftBox> adminGiftBox = adminGiftBoxReader.findByAdminType(
AdminType.ONBOARDING);
MainGiftBoxResponse mainGiftBoxResponse = MainGiftBoxResponse.builder().build();
if (lastViewedAdminType.isEmpty()) {
lastViewedAdminTypeWriter.save(LastViewedAdminType.of(member, AdminType.ONBOARDING));
if (adminGiftBox.isPresent()) {
mainGiftBoxResponse = MainGiftBoxResponse.from(adminGiftBox.get().getGiftBox());
}
}
return mainGiftBoxResponse;
}
public GiftBoxResponse toGiftBoxResponse(GiftBox giftBox) {
BoxResponse boxResponse = BoxResponse.from(giftBox.getBox());
EnvelopeResponse envelopeResponse = EnvelopeResponse.from(
giftBox.getLetter().getEnvelope());
List<PhotoResponse> photos = photoReader.findAllByGiftBox(giftBox).stream()
.map(PhotoResponse::from)
.sorted(Comparator.comparingInt(PhotoResponse::sequence))
.toList();
List<StickerResponse> stickers = giftBoxStickerReader.findAllByGiftBox(giftBox).stream()
.map(StickerResponse::from)
.sorted(Comparator.comparingInt(StickerResponse::location))
.toList();
GiftResponse giftResponse = null;
if (giftBox.getGift() != null) {
giftResponse = GiftResponse.from(giftBox.getGift());
}
return GiftBoxResponse.of(giftBox, boxResponse, envelopeResponse, photos, stickers,
giftResponse);
}
}