From b62759e30a5931ebcafb293aa8625ada880ce54e Mon Sep 17 00:00:00 2001 From: sohyundoh Date: Sun, 7 Jan 2024 15:43:41 +0900 Subject: [PATCH] =?UTF-8?q?#22=20[feat]=20=EB=8C=93=EA=B8=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/repository/CommentRepository.java | 12 ++++++ .../mile/comment/service/CommentService.java | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 module-domain/src/main/java/com/mile/comment/repository/CommentRepository.java diff --git a/module-domain/src/main/java/com/mile/comment/repository/CommentRepository.java b/module-domain/src/main/java/com/mile/comment/repository/CommentRepository.java new file mode 100644 index 00000000..b55a2f57 --- /dev/null +++ b/module-domain/src/main/java/com/mile/comment/repository/CommentRepository.java @@ -0,0 +1,12 @@ +package com.mile.comment.repository; + +import com.mile.comment.domain.Comment; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface CommentRepository extends JpaRepository { + + @Query("select c.user.id from Comment c where c = :comment") + Long findUserIdByComment(@Param(value = "comment") final Comment comment); +} diff --git a/module-domain/src/main/java/com/mile/comment/service/CommentService.java b/module-domain/src/main/java/com/mile/comment/service/CommentService.java index 4e2718cc..7e4135be 100644 --- a/module-domain/src/main/java/com/mile/comment/service/CommentService.java +++ b/module-domain/src/main/java/com/mile/comment/service/CommentService.java @@ -1,7 +1,49 @@ package com.mile.comment.service; +import com.mile.comment.domain.Comment; +import com.mile.comment.repository.CommentRepository; +import com.mile.exception.message.ErrorMessage; +import com.mile.exception.model.ForbiddenException; +import com.mile.exception.model.NotFoundException; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service +@RequiredArgsConstructor public class CommentService { + + private final CommentRepository commentRepository; + + @Transactional + public void deleteComment( + final Long commentId, + final Long userId + ) { + Comment comment = findById(commentId); + authenticateUser(comment, userId); + delete(comment); + } + + private void delete( + final Comment comment + ) { + commentRepository.delete(comment); + } + private Comment findById( + final Long commentId + ) { + return commentRepository.findById(commentId) + .orElseThrow( + () -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND) + ); + } + private void authenticateUser( + final Comment comment, + final Long userId + ) { + if(!commentRepository.findUserIdByComment(comment).equals(userId)) { + throw new ForbiddenException(ErrorMessage.COMMENT_ACCESS_ERROR); + } + } }