-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentServiceImpl.java
More file actions
141 lines (116 loc) · 5.79 KB
/
CommentServiceImpl.java
File metadata and controls
141 lines (116 loc) · 5.79 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
package com.example.sharemind.comment.application;
import com.example.sharemind.comment.domain.Comment;
import com.example.sharemind.comment.dto.request.CommentCreateRequest;
import com.example.sharemind.comment.dto.response.CommentGetResponse;
import com.example.sharemind.comment.exception.CommentErrorCode;
import com.example.sharemind.comment.exception.CommentException;
import com.example.sharemind.comment.repository.CommentRepository;
import com.example.sharemind.commentLike.repository.CommentLikeRepository;
import com.example.sharemind.counselor.application.CounselorService;
import com.example.sharemind.counselor.domain.Counselor;
import com.example.sharemind.customer.application.CustomerService;
import com.example.sharemind.customer.domain.Customer;
import com.example.sharemind.customer.domain.Level;
import com.example.sharemind.post.application.PostService;
import com.example.sharemind.post.content.PostStatus;
import com.example.sharemind.post.domain.Post;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {
private static final Boolean COMMENT_IS_NOT_LIKED = false;
private static final Boolean COMMENT_IS_NOT_WRITTEN = false;
private final PostService postService;
private final CounselorService counselorService;
private final CustomerService customerService;
private final CommentRepository commentRepository;
private final CommentLikeRepository commentLikeRepository;
@Override
public List<CommentGetResponse> getCounselorComments(Long postId, Long customerId) {
Post post = postService.checkAndGetCounselorPost(postId, customerId);
Customer customer = customerService.getCustomerByCustomerId(customerId);
List<Comment> comments = commentRepository.findByPostAndIsActivatedIsTrue(post);
return comments.stream()
.map(comment -> CommentGetResponse.of(comment,
commentLikeRepository.existsByCommentAndCustomerAndIsActivatedIsTrue(
comment, customer),
comment.getCounselor().equals(customer.getCounselor())))
.toList();
}
@Override
public List<CommentGetResponse> getCustomerComments(Long postId, Long customerId) {
Post post = postService.getPostByPostId(postId);
post.checkReadAuthority(customerId);
List<Comment> comments = commentRepository.findByPostAndIsActivatedIsTrue(post);
if (customerId != 0) {
Customer customer = customerService.getCustomerByCustomerId(customerId);
return comments.stream()
.map(comment -> CommentGetResponse.of(comment,
commentLikeRepository.existsByCommentAndCustomerAndIsActivatedIsTrue(
comment, customer), COMMENT_IS_NOT_WRITTEN))
.toList();
}
return comments.stream()
.map(comment -> CommentGetResponse.of(comment, COMMENT_IS_NOT_LIKED,
COMMENT_IS_NOT_WRITTEN))
.toList();
}
@Transactional
@Override
public void createComment(CommentCreateRequest commentCreateRequest, Long customerId) {
Post post = postService.checkAndGetCounselorPost(commentCreateRequest.getPostId(),
customerId);
Customer customer = post.getCustomer();
Counselor counselor = counselorService.getCounselorByCustomerId(customerId);
counselorService.checkCounselorAndCustomerSame(customer, counselor);
if (commentRepository.findByPostAndCounselorAndIsActivatedIsTrue(post, counselor) != null) {
throw new CommentException(CommentErrorCode.COMMENT_ALREADY_REGISTERED,
counselor.getNickname());
}
commentRepository.save(commentCreateRequest.toEntity(post, counselor));
post.increaseTotalComment();
counselor.increaseTotalConsult();
if (post.getIsPublic()) {
Level counselorlevel = counselor.getLevel();
counselorlevel.increasePostAnswer();
}
}
@Override
public Comment getCommentByCommentId(Long commentId) {
return commentRepository.findByCommentIdAndIsActivatedIsTrue(commentId).orElseThrow(
() -> new CommentException(CommentErrorCode.COMMENT_NOT_FOUND,
commentId.toString()));
}
@Transactional
@Override
public void updateCustomerChosenComment(Long postId, Long commentId, Long customerId) {
Customer customer = customerService.getCustomerByCustomerId(customerId);
Post post = postService.getPostByPostId(postId);
post.checkWriteAuthority(customer);
post.checkPostProceedingOrTimeOut();
Comment comment = getCommentByCommentId(commentId);
comment.checkCommentIsForPost(post);
comment.updateIsChosen();
if (post.getIsPublic()) {
Level customerLevel = customer.getLevel();
customerLevel.increasePostChoose();
Level counselorLevel = comment.getCounselor().getLevel();
counselorLevel.increasePostChosen();
if (post.getIsPopular()) {
counselorLevel.increasePostPopularityChosen();
}
}
post.updatePostStatus(PostStatus.COMPLETED);
}
@Override
public Boolean getIsCommentOwner(Long postId, Long customerId) {
Post post = postService.getPostByPostId(postId);
Counselor counselor = counselorService.getCounselorByCustomerId(customerId);
return commentRepository.findByPostAndCounselorAndIsActivatedIsTrue(post, counselor)
!= null;
}
}