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