Skip to content

Commit cdb984d

Browse files
committed
feat: 댓글 서비스 로직 구현
1 parent e278ae4 commit cdb984d

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.back.web7_9_codecrete_be.domain.community.comment.service;
2+
3+
import com.back.web7_9_codecrete_be.domain.community.comment.dto.request.CommentCreateRequest;
4+
import com.back.web7_9_codecrete_be.domain.community.comment.dto.response.CommentPageResponse;
5+
import com.back.web7_9_codecrete_be.domain.community.comment.dto.response.CommentResponse;
6+
import com.back.web7_9_codecrete_be.domain.community.comment.entity.Comment;
7+
import com.back.web7_9_codecrete_be.domain.community.comment.repository.CommentRepository;
8+
import com.back.web7_9_codecrete_be.domain.community.post.entity.Post;
9+
import com.back.web7_9_codecrete_be.domain.community.post.repository.PostRepository;
10+
import com.back.web7_9_codecrete_be.global.error.code.CommentErrorCode;
11+
import com.back.web7_9_codecrete_be.global.error.code.PostErrorCode;
12+
import com.back.web7_9_codecrete_be.global.error.exception.BusinessException;
13+
import lombok.RequiredArgsConstructor;
14+
import org.springframework.data.domain.Page;
15+
import org.springframework.data.domain.PageRequest;
16+
import org.springframework.data.domain.Pageable;
17+
import org.springframework.data.domain.Sort;
18+
import org.springframework.stereotype.Service;
19+
import org.springframework.transaction.annotation.Transactional;
20+
21+
@Service
22+
@RequiredArgsConstructor
23+
@Transactional(readOnly = true)
24+
public class CommentService {
25+
26+
private final CommentRepository commentRepository;
27+
private final PostRepository postRepository;
28+
29+
// 댓글 생성
30+
@Transactional
31+
public Long create(Long postId, CommentCreateRequest req, Long userId) {
32+
Post post = postRepository.findById(postId)
33+
.orElseThrow(() -> new BusinessException(PostErrorCode.POST_NOT_FOUND));
34+
35+
Comment comment = Comment.create(
36+
post,
37+
userId,
38+
req.getContent()
39+
);
40+
41+
return commentRepository.save(comment).getCommentId();
42+
}
43+
44+
// 댓글 조회
45+
public CommentPageResponse<CommentResponse> getComments(Long postId, int page) {
46+
47+
Pageable pageable = PageRequest.of(
48+
page - 1,
49+
20,
50+
Sort.by(Sort.Direction.ASC, "createdDate")
51+
);
52+
53+
Page<CommentResponse> result =
54+
commentRepository.findByPost_PostId(postId, pageable)
55+
.map(CommentResponse::from);
56+
57+
return CommentPageResponse.from(result);
58+
}
59+
60+
// 댓글 삭제
61+
@Transactional
62+
public void delete(Long commentId, Long userId) {
63+
Comment comment = commentRepository.findById(commentId)
64+
.orElseThrow(() -> new BusinessException(CommentErrorCode.COMMENT_NOT_FOUND));
65+
66+
if (!comment.getUserId().equals(userId)) {
67+
throw new BusinessException(CommentErrorCode.NO_COMMENT_PERMISSION);
68+
}
69+
70+
commentRepository.delete(comment);
71+
}
72+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.back.web7_9_codecrete_be.global.error.code;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.http.HttpStatus;
6+
7+
@Getter
8+
@RequiredArgsConstructor
9+
public enum CommentErrorCode implements ErrorCode {
10+
11+
// 댓글 조회
12+
COMMENT_NOT_FOUND(
13+
HttpStatus.NOT_FOUND,
14+
"C-100",
15+
"댓글을 찾을 수 없습니다."
16+
),
17+
18+
// 댓글 작성
19+
COMMENT_CONTENT_EMPTY(
20+
HttpStatus.BAD_REQUEST,
21+
"C-110",
22+
"댓글 내용은 비어 있을 수 없습니다."
23+
),
24+
25+
// 권한 관련
26+
NO_COMMENT_PERMISSION(
27+
HttpStatus.FORBIDDEN,
28+
"C-120",
29+
"댓글에 대한 권한이 없습니다."
30+
),
31+
32+
// 삭제 관련
33+
COMMENT_ALREADY_DELETED(
34+
HttpStatus.BAD_REQUEST,
35+
"C-130",
36+
"이미 삭제된 댓글입니다."
37+
);
38+
39+
private final HttpStatus status;
40+
private final String code;
41+
private final String message;
42+
}

0 commit comments

Comments
 (0)