|
| 1 | +package com.somemore.community.service.comment; |
| 2 | + |
| 3 | +import com.somemore.community.domain.CommunityComment; |
| 4 | +import com.somemore.community.repository.comment.CommunityCommentRepository; |
| 5 | +import com.somemore.community.usecase.comment.DeleteCommunityCommentUseCase; |
| 6 | +import com.somemore.global.exception.BadRequestException; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import org.springframework.stereotype.Service; |
| 9 | +import org.springframework.transaction.annotation.Transactional; |
| 10 | + |
| 11 | +import java.util.UUID; |
| 12 | + |
| 13 | +import static com.somemore.global.exception.ExceptionMessage.*; |
| 14 | + |
| 15 | +@RequiredArgsConstructor |
| 16 | +@Transactional |
| 17 | +@Service |
| 18 | +public class DeleteCommunityCommentService implements DeleteCommunityCommentUseCase { |
| 19 | + |
| 20 | + private final CommunityCommentRepository communityCommentRepository; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void deleteCommunityComment(UUID writerId, Long id) { |
| 24 | + |
| 25 | + CommunityComment communityComment = getCommunityBoardById(id); |
| 26 | + |
| 27 | + validateWriter(communityComment, writerId); |
| 28 | + |
| 29 | + communityComment.markAsDeleted(); |
| 30 | + |
| 31 | + communityCommentRepository.save(communityComment); |
| 32 | + } |
| 33 | + |
| 34 | + private CommunityComment getCommunityBoardById(Long id) { |
| 35 | + return communityCommentRepository.findById(id) |
| 36 | + .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage())); |
| 37 | + } |
| 38 | + |
| 39 | + private void validateWriter(CommunityComment communityComment, UUID writerId) { |
| 40 | + if (communityComment.isWriter(writerId)) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + throw new BadRequestException(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage()); |
| 45 | + } |
| 46 | +} |
0 commit comments