-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeliveryPartyService.java
More file actions
214 lines (169 loc) · 7.72 KB
/
Copy pathDeliveryPartyService.java
File metadata and controls
214 lines (169 loc) · 7.72 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
package com.leets.tdd.party.service;
import com.leets.tdd.party.domain.DeliveryParty;
import com.leets.tdd.party.domain.PartyParticipant;
import com.leets.tdd.party.domain.PartyParticipantRole;
import com.leets.tdd.party.domain.PartyParticipantStatus;
import com.leets.tdd.party.domain.PartyStatus;
import com.leets.tdd.party.dto.request.CreateDeliveryPartyRequest;
import com.leets.tdd.party.dto.request.UpdateDeliveryPartyRequest;
import com.leets.tdd.party.dto.response.CreateDeliveryPartyResponse;
import com.leets.tdd.party.dto.response.DeliveryPartyDetailResponse;
import com.leets.tdd.party.dto.response.JoinDeliveryPartyResponse;
import com.leets.tdd.party.exception.PartyErrorCode;
import com.leets.tdd.party.exception.PartyException;
import com.leets.tdd.party.repository.DeliveryPartyRepository;
import com.leets.tdd.party.repository.PartyParticipantRepository;
import com.leets.tdd.settlement.domain.SettlementStatus;
import com.leets.tdd.user.domain.User;
import com.leets.tdd.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class DeliveryPartyService {
private final DeliveryPartyRepository deliveryPartyRepository;
private final PartyParticipantRepository partyParticipantRepository;
private final UserRepository userRepository;
// 배달팟 생성 API
public CreateDeliveryPartyResponse createDeliveryParty(CreateDeliveryPartyRequest request) {
User user = userRepository.findAll()
.stream()
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("사용자가 없습니다."));
Long creatorId = user.getId();
DeliveryParty deliveryParty = new DeliveryParty(
creatorId,
request.getFoodCategoryId(),
request.getTitle(),
request.getDescription(),
request.getMinParticipants(),
request.getMaxParticipants(),
request.getOrderExpectedAt(),
PartyStatus.RECRUITING,
null,
SettlementStatus.NONE,
null,
null,
null,
LocalDateTime.now(),
LocalDateTime.now()
);
DeliveryParty savedDeliveryParty = deliveryPartyRepository.save(deliveryParty);
return new CreateDeliveryPartyResponse(
savedDeliveryParty.getId(),
savedDeliveryParty.getFoodCategoryId(),
savedDeliveryParty.getTitle(),
savedDeliveryParty.getDescription(),
savedDeliveryParty.getMinParticipants(),
savedDeliveryParty.getMaxParticipants(),
savedDeliveryParty.getOrderExpectedAt(),
savedDeliveryParty.getStatus().name(),
savedDeliveryParty.getCreatedAt()
);
}
// 배달팟 목록 조회 API
public List<CreateDeliveryPartyResponse> getDeliveryParties() {
List<DeliveryParty> deliveryParties =
deliveryPartyRepository.findAllByOrderByCreatedAtDesc();
return deliveryParties.stream()
.map(deliveryParty -> new CreateDeliveryPartyResponse(
deliveryParty.getId(),
deliveryParty.getFoodCategoryId(),
deliveryParty.getTitle(),
deliveryParty.getDescription(),
deliveryParty.getMinParticipants(),
deliveryParty.getMaxParticipants(),
deliveryParty.getOrderExpectedAt(),
deliveryParty.getStatus().name(),
deliveryParty.getCreatedAt()
))
.collect(Collectors.toList());
}
// 배달팟 상세 조회 API
public DeliveryPartyDetailResponse getDeliveryPartyDetail(Long partyId) {
DeliveryParty deliveryParty = deliveryPartyRepository.findById(partyId)
.orElseThrow(() -> new PartyException(PartyErrorCode.PARTY_NOT_FOUND));
return new DeliveryPartyDetailResponse(deliveryParty);
}
// 배달팟 참여 API
@Transactional
public JoinDeliveryPartyResponse joinDeliveryParty(Long partyId, Long currentUserId) {
DeliveryParty deliveryParty = deliveryPartyRepository.findWithLockById(partyId)
.orElseThrow(() -> new PartyException(PartyErrorCode.PARTY_NOT_FOUND));
if (deliveryParty.getStatus() != PartyStatus.RECRUITING) {
throw new PartyException(PartyErrorCode.RECRUITMENT_CLOSED);
}
// 방장은 생성 시점부터 참여 인원으로 간주한다.
if (deliveryParty.getCreatorId().equals(currentUserId)
|| partyParticipantRepository.existsByPartyIdAndUserIdAndStatus(
partyId, currentUserId, PartyParticipantStatus.JOINED)) {
throw new PartyException(PartyErrorCode.ALREADY_JOINED);
}
long currentParticipants = currentParticipants(partyId);
if (currentParticipants >= deliveryParty.getMaxParticipants()) {
throw new PartyException(PartyErrorCode.PARTY_FULL);
}
try {
partyParticipantRepository.saveAndFlush(new PartyParticipant(
partyId,
currentUserId,
PartyParticipantRole.MEMBER,
PartyParticipantStatus.JOINED,
LocalDateTime.now()
));
} catch (DataIntegrityViolationException exception) {
throw new PartyException(PartyErrorCode.JOIN_FAILED);
}
return new JoinDeliveryPartyResponse(
partyId,
currentParticipants + 1,
deliveryParty.getMaxParticipants()
);
}
private long currentParticipants(Long partyId) {
return 1 + partyParticipantRepository.countByPartyIdAndStatus(
partyId, PartyParticipantStatus.JOINED);
}
// 배달팟 수정 API
public void updateDeliveryParty(
Long partyId,
UpdateDeliveryPartyRequest request,
Long currentUserId
) {
DeliveryParty deliveryParty = deliveryPartyRepository.findById(partyId)
.orElseThrow(() -> new PartyException(PartyErrorCode.PARTY_NOT_FOUND));
if (!deliveryParty.getCreatorId().equals(currentUserId)) {
throw new PartyException(PartyErrorCode.NOT_OWNER);
}
if (deliveryParty.getStatus() != PartyStatus.RECRUITING) {
throw new PartyException(PartyErrorCode.INVALID_PARTY_STATUS);
}
deliveryParty.update(
request.getTitle(),
request.getDescription(),
request.getMaxParticipants(),
request.getOrderExpectedAt()
);
deliveryPartyRepository.save(deliveryParty);
}
// 배달팟 삭제(취소) API
public Long deleteDeliveryParty(
Long partyId,
Long currentUserId
) {
DeliveryParty deliveryParty = deliveryPartyRepository.findById(partyId)
.orElseThrow(() -> new PartyException(PartyErrorCode.PARTY_NOT_FOUND));
if (!deliveryParty.getCreatorId().equals(currentUserId)) {
throw new PartyException(PartyErrorCode.NOT_OWNER);
}
deliveryParty.cancel();
System.out.println("서비스 내부 상태 = " + deliveryParty.getStatus());
deliveryPartyRepository.save(deliveryParty);
return partyId;
}
}