|
| 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 | +} |
0 commit comments