|
| 1 | +package org.sfa.request.service.impl; |
| 2 | + |
| 3 | +import org.sfa.request.constant.SaayamStatusCode; |
| 4 | +import org.sfa.request.dto.CommentDTO; |
| 5 | +import org.sfa.request.exception.types.ResourceNotFoundException; |
| 6 | +import org.sfa.request.mapper.CommentMapper; |
| 7 | +import org.sfa.request.model.entity.Comment; |
| 8 | +import org.sfa.request.repository.CommentRepository; |
| 9 | +import org.sfa.request.repository.RequestRepository; |
| 10 | +import org.sfa.request.response.SaayamResponse; |
| 11 | +import org.sfa.request.service.api.CommentService; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +@Service |
| 16 | +public class CommentServiceImpl implements CommentService { |
| 17 | + |
| 18 | + private final CommentRepository commentRepository; |
| 19 | + private final CommentMapper commentMapper; |
| 20 | + private final RequestRepository requestRepository; |
| 21 | + |
| 22 | + @Autowired |
| 23 | + public CommentServiceImpl(CommentRepository commentRepository, CommentMapper commentMapper, RequestRepository requestRepository) { |
| 24 | + this.commentRepository = commentRepository; |
| 25 | + this.commentMapper = commentMapper; |
| 26 | + this.requestRepository = requestRepository; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public SaayamResponse<CommentDTO> createComment(String requestId, String authorName, String commentText) { |
| 31 | + boolean requestExistsForComment = requestRepository.existsById(requestId); |
| 32 | + if(!requestExistsForComment){ |
| 33 | + throw new ResourceNotFoundException("Request with id:" + requestId + "not found."); |
| 34 | + } |
| 35 | + |
| 36 | + Comment comment = commentMapper.toEntity(new CommentDTO(authorName, commentText)); |
| 37 | + |
| 38 | + Comment savedCommentForRequest = commentRepository.save(comment); |
| 39 | + |
| 40 | + CommentDTO commentDTO = commentMapper.toDTO(savedCommentForRequest); |
| 41 | + |
| 42 | + return SaayamResponse.success( |
| 43 | + SaayamStatusCode.SUCCESS, |
| 44 | + "Comment Created Successfully", |
| 45 | + commentDTO); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public SaayamResponse<CommentDTO> updateComment(String requestId, Long commentId, String commentText) { |
| 50 | + |
| 51 | + Comment commentToBeUpdated = commentRepository.findByRequestIdAndCommentId(requestId, commentId).orElseThrow(() -> |
| 52 | + new ResourceNotFoundException( |
| 53 | + "Comment not found with id:" + commentId |
| 54 | + + "does not belong to request with id:" + requestId)); |
| 55 | + |
| 56 | + commentToBeUpdated.setCommentText(commentText); |
| 57 | + |
| 58 | + Comment updateComment = commentRepository.save(commentToBeUpdated); |
| 59 | + |
| 60 | + CommentDTO commentDTO = commentMapper.toDTO(updateComment); |
| 61 | + |
| 62 | + return SaayamResponse.success( |
| 63 | + SaayamStatusCode.SUCCESS, |
| 64 | + "Comment Content Updated Successfully", |
| 65 | + commentDTO); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public SaayamResponse<CommentDTO> deleteComment(String requestId, Long commentId) { |
| 70 | + Comment commentToBeDeleted = commentRepository.findByRequestIdAndCommentId(requestId, commentId).orElseThrow(() -> |
| 71 | + new ResourceNotFoundException( |
| 72 | + "Comment not found with id:" + commentId |
| 73 | + + "does not belong to request with id:" + requestId + "and must already be deleted")); |
| 74 | + |
| 75 | + commentRepository.delete(commentToBeDeleted); |
| 76 | + |
| 77 | + return SaayamResponse.success( |
| 78 | + SaayamStatusCode.SUCCESS, |
| 79 | + "Comment Deleted Successfully", |
| 80 | + null); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public SaayamResponse<CommentDTO> getCommentById(String requestId, Long commentId) { |
| 85 | + Comment commentToBeUpdated = commentRepository.findByRequestIdAndCommentId(requestId, commentId).orElseThrow(() -> |
| 86 | + new ResourceNotFoundException( |
| 87 | + "Comment not found with id:" + commentId |
| 88 | + + "does not belong to request with id:" + requestId)); |
| 89 | + |
| 90 | + CommentDTO commentDTO = commentMapper.toDTO(commentToBeUpdated); |
| 91 | + |
| 92 | + return SaayamResponse.success( |
| 93 | + SaayamStatusCode.SUCCESS, |
| 94 | + "Comment Retrieved Successfully", |
| 95 | + commentDTO); |
| 96 | + } |
| 97 | +} |
0 commit comments