-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemberPickCommentService.java
More file actions
318 lines (254 loc) · 15.4 KB
/
MemberPickCommentService.java
File metadata and controls
318 lines (254 loc) · 15.4 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
package com.dreamypatisiel.devdevdev.domain.service.pick;
import static com.dreamypatisiel.devdevdev.domain.exception.PickExceptionMessage.INVALID_CAN_NOT_ACTION_DELETED_PICK_COMMENT_MESSAGE;
import static com.dreamypatisiel.devdevdev.domain.exception.PickExceptionMessage.INVALID_NOT_APPROVAL_STATUS_PICK_COMMENT_MESSAGE;
import static com.dreamypatisiel.devdevdev.domain.exception.PickExceptionMessage.INVALID_NOT_FOUND_PICK_COMMENT_MESSAGE;
import static com.dreamypatisiel.devdevdev.domain.exception.PickExceptionMessage.INVALID_NOT_FOUND_PICK_MESSAGE;
import static com.dreamypatisiel.devdevdev.domain.exception.PickExceptionMessage.INVALID_NOT_FOUND_PICK_VOTE_MESSAGE;
import com.dreamypatisiel.devdevdev.domain.entity.Member;
import com.dreamypatisiel.devdevdev.domain.entity.Pick;
import com.dreamypatisiel.devdevdev.domain.entity.PickComment;
import com.dreamypatisiel.devdevdev.domain.entity.PickCommentRecommend;
import com.dreamypatisiel.devdevdev.domain.entity.PickVote;
import com.dreamypatisiel.devdevdev.domain.entity.embedded.CommentContents;
import com.dreamypatisiel.devdevdev.domain.entity.enums.PickOptionType;
import com.dreamypatisiel.devdevdev.domain.policy.PickBestCommentsPolicy;
import com.dreamypatisiel.devdevdev.domain.policy.PickPopularScorePolicy;
import com.dreamypatisiel.devdevdev.domain.repository.pick.PickCommentRecommendRepository;
import com.dreamypatisiel.devdevdev.domain.repository.pick.PickCommentRepository;
import com.dreamypatisiel.devdevdev.domain.repository.pick.PickCommentSort;
import com.dreamypatisiel.devdevdev.domain.repository.pick.PickRepository;
import com.dreamypatisiel.devdevdev.domain.repository.pick.PickVoteRepository;
import com.dreamypatisiel.devdevdev.domain.service.pick.dto.PickCommentDto;
import com.dreamypatisiel.devdevdev.exception.NotFoundException;
import com.dreamypatisiel.devdevdev.global.common.MemberProvider;
import com.dreamypatisiel.devdevdev.global.common.TimeProvider;
import com.dreamypatisiel.devdevdev.openai.embeddings.EmbeddingsService;
import com.dreamypatisiel.devdevdev.web.dto.SliceCommentCustom;
import com.dreamypatisiel.devdevdev.web.dto.response.pick.PickCommentRecommendResponse;
import com.dreamypatisiel.devdevdev.web.dto.response.pick.PickCommentResponse;
import com.dreamypatisiel.devdevdev.web.dto.response.pick.PickCommentsResponse;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class MemberPickCommentService extends PickCommonService implements PickCommentService {
private final MemberProvider memberProvider;
private final PickRepository pickRepository;
private final PickVoteRepository pickVoteRepository;
private final PickCommentRepository pickCommentRepository;
public MemberPickCommentService(TimeProvider timeProvider, MemberProvider memberProvider,
EmbeddingsService embeddingsService, PickPopularScorePolicy pickPopularScorePolicy,
PickBestCommentsPolicy pickBestCommentsPolicy,
PickRepository pickRepository, PickVoteRepository pickVoteRepository,
PickCommentRepository pickCommentRepository,
PickCommentRecommendRepository pickCommentRecommendRepository) {
super(embeddingsService, pickBestCommentsPolicy, pickPopularScorePolicy, timeProvider, pickRepository,
pickCommentRepository, pickCommentRecommendRepository);
this.memberProvider = memberProvider;
this.pickRepository = pickRepository;
this.pickVoteRepository = pickVoteRepository;
this.pickCommentRepository = pickCommentRepository;
}
/**
* @Note: 픽픽픽 메인 댓글을 작성한다.
* @Author: 장세웅
* @Since: 2024.08.23
*/
@Transactional
public PickCommentResponse registerPickComment(Long pickId, PickCommentDto pickCommentDto, Authentication authentication) {
String contents = pickCommentDto.getContents();
Boolean isPickVotePublic = pickCommentDto.getIsPickVotePublic();
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
// 픽픽픽 조회
Pick findPick = pickRepository.findById(pickId)
.orElseThrow(() -> new NotFoundException(INVALID_NOT_FOUND_PICK_MESSAGE));
// 댓글 갯수 증가 및 인기점수 반영
findPick.incrementCommentTotalCount();
findPick.changePopularScore(pickPopularScorePolicy);
// 픽픽픽 게시글의 승인 상태 검증
validateIsApprovalPickContentStatus(findPick, INVALID_NOT_APPROVAL_STATUS_PICK_COMMENT_MESSAGE, REGISTER);
// 픽픽픽 선택지 투표 공개인 경우
if (isPickVotePublic) {
// 회원이 투표한 픽픽픽 투표 조회
PickVote findPickVote = pickVoteRepository.findWithPickAndPickOptionByPickIdAndMemberAndDeletedAtIsNull(
pickId, findMember)
.orElseThrow(() -> new NotFoundException(INVALID_NOT_FOUND_PICK_VOTE_MESSAGE));
// 픽픽픽 투표한 픽 옵션의 댓글 작성
PickComment pickComment = PickComment.createPublicVoteCommentByMember(new CommentContents(contents),
findMember, findPick, findPickVote);
pickCommentRepository.save(pickComment);
return new PickCommentResponse(pickComment.getId());
}
// 픽픽픽 선택지 투표 비공개인 경우
PickComment pickComment = PickComment.createPrivateVoteCommentByMember(new CommentContents(contents), findMember,
findPick);
pickCommentRepository.save(pickComment);
return new PickCommentResponse(pickComment.getId());
}
/**
* @Note: 픽픽픽 답글을 작성한다.
* @Author: 장세웅
* @Since: 2024.08.24
*/
@Transactional
public PickCommentResponse registerPickRepliedComment(Long pickParentCommentId,
Long pickCommentOriginParentId,
Long pickId,
PickCommentDto pickCommentDto,
Authentication authentication) {
String contents = pickCommentDto.getContents();
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
// 픽픽픽 댓글 로직 수행
PickReplyContext pickReplyContext = prepareForReplyRegistration(pickParentCommentId, pickCommentOriginParentId, pickId);
PickComment findParentPickComment = pickReplyContext.parentPickComment();
PickComment findOriginParentPickComment = pickReplyContext.originParentPickComment();
Pick findPick = pickReplyContext.pick();
// 픽픽픽 서브 댓글(답글) 생성
PickComment pickRepliedComment = PickComment.createRepliedCommentByMember(new CommentContents(contents),
findParentPickComment, findOriginParentPickComment, findMember, findPick);
pickCommentRepository.save(pickRepliedComment);
return new PickCommentResponse(pickRepliedComment.getId());
}
/**
* @Note: 회원 자신이 작성한 픽픽픽 댓글/답글을 수정한다. 픽픽픽 공개 여부는 수정할 수 없다.
* @Author: 장세웅
* @Since: 2024.08.10
*/
@Transactional
public PickCommentResponse modifyPickComment(Long pickCommentId, Long pickId,
PickCommentDto pickModifyCommentDto,
Authentication authentication) {
String contents = pickModifyCommentDto.getContents();
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
// 픽픽픽 댓글 조회(회원 본인이 댓글 작성, 삭제되지 않은 댓글)
PickComment findPickComment = pickCommentRepository.findWithPickByIdAndPickIdAndCreatedByIdAndDeletedAtIsNull(
pickCommentId, pickId, findMember.getId())
.orElseThrow(() -> new NotFoundException(INVALID_NOT_FOUND_PICK_COMMENT_MESSAGE));
// 픽픽픽 게시글의 승인 상태 검증
validateIsApprovalPickContentStatus(findPickComment.getPick(), INVALID_NOT_APPROVAL_STATUS_PICK_COMMENT_MESSAGE,
MODIFY);
// 댓글 수정
findPickComment.modifyCommentContents(new CommentContents(contents), timeProvider.getLocalDateTimeNow());
return new PickCommentResponse(findPickComment.getId());
}
/**
* @Note: 회원 자신이 작성한 픽픽픽 댓글/답글을 삭제한다. 소프트 삭제를 진행한다. 어드민은 모든 댓글/답글을 삭제할 수 있다.
* @Author: 장세웅
* @Since: 2024.08.11
*/
@Transactional
public PickCommentResponse deletePickComment(Long pickCommentId, Long pickId, Authentication authentication) {
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
// 어드민은 자신이 작성하지 않은 댓글도 삭제 가능
if (findMember.isAdmin()) {
// 픽픽픽 댓글 조회(삭제되지 않은 댓글)
PickComment findPickComment = pickCommentRepository.findByIdAndPickIdAndDeletedAtIsNull(
pickCommentId, pickId)
.orElseThrow(() -> new NotFoundException(INVALID_NOT_FOUND_PICK_COMMENT_MESSAGE));
// 소프트 삭제
findPickComment.changeDeletedAtByMember(timeProvider.getLocalDateTimeNow(), findMember);
return new PickCommentResponse(findPickComment.getId());
}
// 픽픽픽 댓글 조회(회원 본인이 댓글 작성, 삭제되지 않은 댓글)
PickComment findPickComment = pickCommentRepository.findWithPickByIdAndPickIdAndCreatedByIdAndDeletedAtIsNull(
pickCommentId, pickId, findMember.getId())
.orElseThrow(() -> new NotFoundException(INVALID_NOT_FOUND_PICK_COMMENT_MESSAGE));
// 픽픽픽 게시글의 승인 상태 검증
validateIsApprovalPickContentStatus(findPickComment.getPick(), INVALID_NOT_APPROVAL_STATUS_PICK_COMMENT_MESSAGE,
DELETE);
// 소프트 삭제
findPickComment.changeDeletedAtByMember(timeProvider.getLocalDateTimeNow(), findMember);
return new PickCommentResponse(findPickComment.getId());
}
/**
* @Note: 정렬 조건에 따라서 커서 방식으로 픽픽픽 댓글/답글을 조회한다.
* @Author: 장세웅
* @Since: 2024.08.25
*/
public SliceCommentCustom<PickCommentsResponse> findPickComments(Pageable pageable, Long pickId,
Long pickCommentId,
PickCommentSort pickCommentSort,
EnumSet<PickOptionType> pickOptionTypes,
Authentication authentication) {
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
// 픽픽픽 댓글/답글 조회
return super.findPickComments(pageable, pickId, pickCommentId, pickCommentSort, pickOptionTypes, findMember);
}
/**
* @Note: 회원이 픽픽픽 댓글/답글에 추천한다. 이미 추천 상태이면 취소한다.(소프트 삭제)
* @Author: 장세웅
* @Since: 2024.09.07
*/
@Transactional
public PickCommentRecommendResponse recommendPickComment(Long pickId, Long pickCommendId,
Authentication authentication) {
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
// 픽픽픽 댓글/답글 조회
PickComment findPickComment = pickCommentRepository.findWithPickByIdAndPickId(pickCommendId, pickId)
.orElseThrow(() -> new NotFoundException(INVALID_NOT_FOUND_PICK_COMMENT_MESSAGE));
// 픽픽픽 검증
validateIsApprovalPickContentStatus(findPickComment.getPick(), INVALID_NOT_APPROVAL_STATUS_PICK_COMMENT_MESSAGE,
RECOMMEND);
// 픽픽픽 댓글/답글 검증
validateIsDeletedPickComment(findPickComment, INVALID_CAN_NOT_ACTION_DELETED_PICK_COMMENT_MESSAGE, RECOMMEND);
return toggleOrCreatePickCommentRecommend(findPickComment, findMember);
}
/**
* @Note: 회원이 픽픽픽 베스트 댓글을 조회한다.
* @Author: 장세웅
* @Since: 2024.10.09
*/
@Override
public List<PickCommentsResponse> findPickBestComments(int size, Long pickId, Authentication authentication) {
// 회원 조회
Member findMember = memberProvider.getMemberByAuthentication(authentication);
return super.findPickBestComments(size, pickId, findMember);
}
private PickCommentRecommendResponse toggleOrCreatePickCommentRecommend(PickComment pickComment, Member member) {
// 픽픽픽 댓글/답글 추천 조회
Optional<PickCommentRecommend> optionalPickCommentRecommend = pickCommentRecommendRepository.findByPickCommentIdAndMemberId(
pickComment.getId(), member.getId());
// 댓글/답글에 추천이 존재하면 toggle
if (optionalPickCommentRecommend.isPresent()) {
PickCommentRecommend pickCommentRecommend = optionalPickCommentRecommend.get();
return togglePickCommentRecommend(pickComment, pickCommentRecommend);
}
return createPickCommentRecommend(pickComment, member);
}
private PickCommentRecommendResponse createPickCommentRecommend(PickComment pickComment, Member member) {
// 추천
PickCommentRecommend pickCommentRecommend = PickCommentRecommend.create(pickComment, member);
pickCommentRecommendRepository.save(pickCommentRecommend);
// 총 추천 갯수 증가
pickComment.incrementRecommendTotalCount();
return new PickCommentRecommendResponse(pickCommentRecommend.getRecommendedStatus(),
pickComment.getRecommendTotalCount().getCount());
}
private PickCommentRecommendResponse togglePickCommentRecommend(PickComment pickComment,
PickCommentRecommend pickCommentRecommend) {
// 추천 상태이면
if (pickCommentRecommend.isRecommended()) {
// 추천 취소
pickCommentRecommend.cancelRecommend();
pickComment.decrementRecommendTotalCount();
return new PickCommentRecommendResponse(pickCommentRecommend.getRecommendedStatus(),
pickComment.getRecommendTotalCount().getCount());
}
// 추천
pickCommentRecommend.recommend();
pickComment.incrementRecommendTotalCount();
return new PickCommentRecommendResponse(pickCommentRecommend.getRecommendedStatus(),
pickComment.getRecommendTotalCount().getCount());
}
}