-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeliveryPartyService.java
More file actions
184 lines (148 loc) · 6.84 KB
/
Copy pathDeliveryPartyService.java
File metadata and controls
184 lines (148 loc) · 6.84 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
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.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.LeaveDeliveryPartyResponse;
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 LeaveDeliveryPartyResponse leaveDeliveryParty(Long partyId, Long currentUserId) {
DeliveryParty deliveryParty = deliveryPartyRepository.findWithLockById(partyId)
.orElseThrow(() -> new PartyException(PartyErrorCode.PARTY_NOT_FOUND));
if (deliveryParty.getCreatorId().equals(currentUserId)) {
throw new PartyException(PartyErrorCode.HOST_CANNOT_LEAVE);
}
PartyParticipant participant = partyParticipantRepository.findByPartyIdAndUserId(partyId, currentUserId)
.orElseThrow(() -> new PartyException(PartyErrorCode.NOT_PARTICIPANT));
if (!participant.isJoined()) {
throw new PartyException(PartyErrorCode.NOT_PARTICIPANT);
}
if (deliveryParty.getStatus() != PartyStatus.RECRUITING) {
throw new PartyException(PartyErrorCode.LEAVE_NOT_ALLOWED);
}
participant.cancel(LocalDateTime.now());
try {
partyParticipantRepository.saveAndFlush(participant);
} catch (DataIntegrityViolationException exception) {
throw new PartyException(PartyErrorCode.LEAVE_FAILED);
}
return new LeaveDeliveryPartyResponse(
partyId,
currentParticipants(partyId),
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);
}
// 모집 중(RECRUITING) 상태에서만 수정 가능
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);
}
}