Skip to content

Commit

Permalink
[FEAT] 게시글 당 댓글을 한 번만 작성 가능 (#184)
Browse files Browse the repository at this point in the history
* chore: 데이터베이스 pool 개수와 timeout 정의

* feat: 댓글 추가 작성 비즈니스 예외 정의

* feat: 댓글 재작성 확인 메서드 쿼리

* feat: 댓글 재작성 예외 비즈니스 로직 추가
  • Loading branch information
Profile-exe authored Nov 9, 2024
1 parent 802175c commit 82e22a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package econo.buddybridge.comment.exception;

import econo.buddybridge.common.exception.BusinessException;

public class CommentAlreadyWrittenException extends BusinessException {

public static final BusinessException EXCEPTION = new CommentAlreadyWrittenException();

private CommentAlreadyWrittenException() {
super(CommentErrorCode.COMMENT_ALREADY_WRITTEN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum CommentErrorCode implements ErrorCode {
COMMENT_UPDATE_NOT_ALLOWED("C002", HttpStatus.FORBIDDEN, "본인의 댓글만 수정할 수 있습니다."),
COMMENT_DELETE_NOT_ALLOWED("C003", HttpStatus.FORBIDDEN, "본인의 댓글만 삭제할 수 있습니다."),
COMMENT_INVALID_DIRECTION("C004", HttpStatus.BAD_REQUEST, "올바르지 않은 정렬 방식입니다."),
COMMENT_ALREADY_WRITTEN("C005", HttpStatus.BAD_REQUEST, "이미 댓글을 작성했습니다. 댓글은 하나만 작성할 수 있습니다.")
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package econo.buddybridge.comment.repository;

import econo.buddybridge.comment.entity.Comment;
import econo.buddybridge.member.entity.Member;
import econo.buddybridge.post.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositoryCustom {

boolean existsByPostAndAuthor(Post post, Member author);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import econo.buddybridge.comment.dto.CommentReqDto;
import econo.buddybridge.comment.dto.MyPageCommentCustomPage;
import econo.buddybridge.comment.entity.Comment;
import econo.buddybridge.comment.exception.CommentAlreadyWrittenException;
import econo.buddybridge.comment.exception.CommentDeleteNotAllowedException;
import econo.buddybridge.comment.exception.CommentInvalidDirectionException;
import econo.buddybridge.comment.exception.CommentNotFoundException;
Expand Down Expand Up @@ -58,11 +59,14 @@ public CommentCustomPage getComments(Long postId, Integer size, String order, Lo

@Transactional // 댓글 생성
public Long createComment(CommentReqDto commentReqDto, Long postId, Long memberId) {

Member member = memberService.findMemberByIdOrThrow(memberId);

Post post = postService.findPostByIdOrThrow(postId);

// 기존에 댓글을 작성한 적이 있는지 확인하고 있다면 댓글 작성 불가
if (commentRepository.existsByPostAndAuthor(post, member)) {
throw CommentAlreadyWrittenException.EXCEPTION;
}

Comment comment = commentReqToComment(commentReqDto, post, member);

// 게시글 작성자에게 댓글 알림 전송
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ spring:
username: ${MARIA_USER}
password: ${MARIA_PASSWORD}
driver-class-name: org.mariadb.jdbc.Driver
hikari:
minimum-idle: 2
maximum-pool-size: 8
connection-timeout: 30000 # connection 획득 시도 후 실패 시 대기 시간

jpa:
hibernate:
Expand Down

0 comments on commit 82e22a5

Please sign in to comment.