88import com .back .domain .post .comment .entity .PostComment ;
99import com .back .domain .post .comment .repository .PostCommentRepository ;
1010import com .back .domain .post .post .entity .Post ;
11- import com .back .domain .post .post .repository . PostRepository ;
11+ import com .back .domain .post .post .service . PostService ;
1212import com .back .global .exception .ServiceException ;
1313import jakarta .transaction .Transactional ;
1414import lombok .RequiredArgsConstructor ;
2020@ Service
2121@ RequiredArgsConstructor
2222public class PostCommentService {
23- private final PostRepository postRepository ;
23+ private final PostService postService ;
2424 private final PostCommentRepository postCommentRepository ;
2525
2626 @ Transactional
2727 public void createComment (Member member , Long postId , CommentCreateRequest commentCreateRequest ) {
28- Post post = postRepository .findById (postId ).orElseThrow (() -> new ServiceException ("400" , "해당 Id의 게시글이 없습니다." ));
29-
30- if ( commentCreateRequest .comment () == null || commentCreateRequest .comment ().isEmpty ()) {
31- throw new ServiceException ("400" , "댓글은 비어 있을 수 없습니다." );
32- }
28+ validateComment (commentCreateRequest .comment ());
29+ Post post = postService .findPostById (postId );
3330
3431 PostComment postComment = PostComment .builder ()
3532 .post (post )
@@ -49,111 +46,113 @@ public List<CommentAllResponse> getAllPostCommentResponse(Long postId) {
4946 validatePostExists (postId );
5047
5148 List <PostComment > listPostComment = postCommentRepository .findCommentsWithMemberByPostId (postId );
52-
5349 return listPostComment .stream ()
5450 .map (CommentAllResponse ::from )
5551 .toList ();
5652 }
5753
5854 @ Transactional
5955 public void removePostComment (Long postId , CommentDeleteRequest commentDeleteRequest , Member member ) {
60- validatePostExists (postId );
61-
62- PostComment postComment = getPostCommentById (commentDeleteRequest .commentId ());
56+ PostComment postComment = findCommentById (commentDeleteRequest .commentId ());
6357 Member author = postComment .getMember ();
6458
65-
66- if (!Objects .equals (member .getId (), author .getId ())) {
67- throw new ServiceException ("400" , "삭제 권한이 없습니다." );
68- }
69-
70- // if(postComment.getIsAdopted()) {
71- // throw new ServiceException("400", "채택된 댓글은 삭제할 수 없습니다.");
72- // }
59+ validateAuthorized (member ,author );
60+ validatePostExists (postId );
7361
7462 postCommentRepository .delete (postComment );
75-
7663 }
7764
7865 @ Transactional
7966 public void updatePostComment (Long postId , CommentModifyRequest commentModifyRequest , Member member ) {
8067 validatePostExists (postId );
8168
82- PostComment postComment = getPostCommentById (commentModifyRequest .commentId ());
69+ PostComment postComment = findCommentById (commentModifyRequest .commentId ());
8370 Member author = postComment .getMember ();
8471
85-
86- if (!Objects .equals (member .getId (), author .getId ())) {
87- throw new ServiceException ("400" , "수정 권한이 없습니다." );
88- }
89-
90- if ( commentModifyRequest .content () == null || commentModifyRequest .content ().isEmpty ()) {
91- throw new ServiceException ("400" , "댓글은 비어 있을 수 없습니다." );
92- }
72+ validateAuthorized (member , author );
73+ validateComment (commentModifyRequest .content ());
9374
9475 postComment .updateContent (commentModifyRequest .content ());
9576 }
9677
78+ @ Transactional
79+ public void adoptComment (Long commentId , Member member ) {
80+ PostComment postComment = findCommentById (commentId );
9781
82+ Post post = postComment .getPost ();
9883
99- private void validatePostExists ( Long postId ) {
100- if ( postId == null || postId <= 0 ) {
101- throw new ServiceException ( "400" , "유효하지 않은 게시글 Id입니다." );
102- }
84+ validateIsPostAuthor ( post , member );
85+ validatePostType ( post );
86+ validateAlreadyAdoptedComment ( postComment );
87+ validateAlreadyExistsAdoptedComment ( post );
10388
104- if (! postRepository . existsById ( postId )) {
105- throw new ServiceException ( "400" , "해당 Id의 게시글이 없습니다." );
106- }
89+ postComment . adoptComment ();
90+ post . updateResolveStatus ( true );
91+ }
10792
93+ @ Transactional
94+ public CommentAllResponse getAdoptedComment (Long postId ) {
95+ Post post = postService .findPostById (postId );
10896
109- }
97+ validatePostType ( post );
11098
111- private PostComment getPostCommentById ( Long commentId ) {
112- return postCommentRepository . findById ( commentId ). orElseThrow (() -> new ServiceException ( "400" , "해당 Id의 댓글이 없습니다." ) );
99+ PostComment postComment = validateAdoptedComment ( post );
100+ return CommentAllResponse . from ( postComment );
113101 }
114102
115- @ Transactional
116- public void adoptComment (Long commentId , Member member ) {
117- PostComment postComment = postCommentRepository .findById (commentId )
118- .orElseThrow (() -> new ServiceException ("400" , "해당 Id의 댓글이 없습니다." ));
119103
120- Post post = postComment .getPost ();
121104
105+
106+
107+ // ========== 헬퍼 메소드들 ========== //
108+ private void validateIsPostAuthor (Post post , Member member ){
122109 if (!post .isAuthor (member )) {
123110 throw new ServiceException ("400" , "채택 권한이 없습니다." );
124111 }
112+ }
125113
126- if (post .getPostType () != Post .PostType .QUESTIONPOST ) {
127- throw new ServiceException ("400" , "질문 게시글에만 댓글 채택이 가능합니다." );
128- }
129-
114+ private void validateAlreadyAdoptedComment (PostComment postComment ){
130115 if (postComment .getIsAdopted ()) {
131116 throw new ServiceException ("400" , "이미 채택된 댓글입니다." );
132117 }
118+ }
133119
134- // 이미 채택된 댓글이 있는지 확인
135- boolean alreadyAdopted = postCommentRepository .existsByPostAndIsAdoptedTrue (post );
136- if (alreadyAdopted ) {
120+ private void validateAlreadyExistsAdoptedComment (Post post ) {
121+ if (postCommentRepository .existsByPostAndIsAdoptedTrue (post )) {
137122 throw new ServiceException ("400" , "이미 채택된 댓글이 있습니다." );
138123 }
124+ }
139125
140- postComment .adoptComment ();
141-
142- post .updateResolveStatus (true );
126+ private void validateAuthorized (Member member , Member author ) {
127+ if (!Objects .equals (member .getId (), author .getId ())) {
128+ throw new ServiceException ("400" , "변경 권한이 없습니다." );
129+ }
143130 }
144131
145- @ Transactional
146- public CommentAllResponse getAdoptedComment ( Long postId ) {
147- Post post = postRepository . findById ( postId )
148- . orElseThrow (() -> new ServiceException ( "400" , "해당 Id의 게시글이 없습니다." ));
132+ private PostComment validateAdoptedComment ( Post post ) {
133+ return postCommentRepository . findByPostAndIsAdoptedTrue ( post )
134+ . orElseThrow (() -> new ServiceException ( "400" , "채택된 댓글이 없습니다." ));
135+ }
149136
137+ private void validatePostType (Post post ) {
150138 if (post .getPostType () != Post .PostType .QUESTIONPOST ) {
151139 throw new ServiceException ("400" , "질문 게시글만 채택된 댓글을 가질 수 있습니다." );
152140 }
141+ }
153142
154- PostComment postComment = postCommentRepository .findByPostAndIsAdoptedTrue (post )
155- .orElseThrow (() -> new ServiceException ("400" , "채택된 댓글이 없습니다." ));
143+ private void validateComment (String comment ) {
144+ if (comment == null || comment .isEmpty ()) {
145+ throw new ServiceException ("400" , "댓글은 비어 있을 수 없습니다." );
146+ }
147+ }
156148
157- return CommentAllResponse .from (postComment );
149+ private void validatePostExists (Long postId ) {
150+ if (!postService .existsById (postId )) {
151+ throw new ServiceException ("400" , "유효하지 않은 게시글 Id입니다." );
152+ }
153+ }
154+
155+ private PostComment findCommentById (Long commentId ) {
156+ return postCommentRepository .findById (commentId ).orElseThrow (() -> new ServiceException ("400" , "해당 Id의 댓글이 없습니다." ));
158157 }
159158}
0 commit comments