| [700e2f9] | 1 | package com.finki.icare.service;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.CommentDTO;
|
|---|
| 4 | import com.finki.icare.dto.CreateCommentRequest;
|
|---|
| 5 | import com.finki.icare.exceptions.ICareException;
|
|---|
| 6 | import com.finki.icare.mapper.CommentMapper;
|
|---|
| 7 | import com.finki.icare.model.Blog;
|
|---|
| 8 | import com.finki.icare.model.Comment;
|
|---|
| 9 | import com.finki.icare.model.Patient;
|
|---|
| 10 | import com.finki.icare.repository.BlogRepository;
|
|---|
| 11 | import com.finki.icare.repository.CommentRepository;
|
|---|
| 12 | import com.finki.icare.repository.PatientRepository;
|
|---|
| 13 | import lombok.RequiredArgsConstructor;
|
|---|
| 14 | import org.springframework.stereotype.Service;
|
|---|
| 15 | import org.springframework.transaction.annotation.Transactional;
|
|---|
| 16 |
|
|---|
| 17 | import java.time.OffsetDateTime;
|
|---|
| 18 |
|
|---|
| 19 | @Service
|
|---|
| 20 | @RequiredArgsConstructor
|
|---|
| 21 | public class CommentService {
|
|---|
| 22 |
|
|---|
| 23 | private final CommentRepository commentRepository;
|
|---|
| 24 | private final BlogRepository blogRepository;
|
|---|
| 25 | private final PatientRepository patientRepository;
|
|---|
| 26 | private final CommentMapper commentMapper;
|
|---|
| 27 |
|
|---|
| 28 | @Transactional
|
|---|
| 29 | public CommentDTO createComment(Integer blogId, CreateCommentRequest request, Integer patientId) {
|
|---|
| 30 | Blog blog = blogRepository
|
|---|
| 31 | .findById(blogId)
|
|---|
| 32 | .orElseThrow(() -> ICareException.notFound("Blog not found with id: " + blogId));
|
|---|
| 33 |
|
|---|
| 34 | Patient patient = patientRepository
|
|---|
| 35 | .findById(patientId)
|
|---|
| 36 | .orElseThrow(() -> ICareException.unauthorized("Only patients can comment"));
|
|---|
| 37 |
|
|---|
| 38 | Comment comment = new Comment();
|
|---|
| 39 | comment.setBlog(blog);
|
|---|
| 40 | comment.setPatient(patient);
|
|---|
| 41 | comment.setContent(request.getContent());
|
|---|
| 42 | comment.setDateOfComment(OffsetDateTime.now());
|
|---|
| 43 |
|
|---|
| 44 | Comment savedComment = commentRepository.save(comment);
|
|---|
| 45 | return commentMapper.toDTO(savedComment);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | @Transactional
|
|---|
| 49 | public CommentDTO updateComment(Integer commentId, String content, Integer patientId) {
|
|---|
| 50 | Comment comment = commentRepository
|
|---|
| 51 | .findById(commentId)
|
|---|
| 52 | .orElseThrow(() -> ICareException.notFound("Comment not found with id: " + commentId));
|
|---|
| 53 |
|
|---|
| 54 | if (!comment.getPatient().getIdUser().equals(patientId)) {
|
|---|
| 55 | throw ICareException.forbidden("You are not authorized to update this comment");
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | comment.setContent(content);
|
|---|
| 59 | Comment updatedComment = commentRepository.save(comment);
|
|---|
| 60 | return commentMapper.toDTO(updatedComment);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | @Transactional
|
|---|
| 64 | public void deleteComment(Integer commentId, Integer patientId) {
|
|---|
| 65 | Comment comment = commentRepository
|
|---|
| 66 | .findById(commentId)
|
|---|
| 67 | .orElseThrow(() -> ICareException.notFound("Comment not found with id: " + commentId));
|
|---|
| 68 |
|
|---|
| 69 | if (!comment.getPatient().getIdUser().equals(patientId)) {
|
|---|
| 70 | throw ICareException.forbidden("You are not authorized to delete this comment");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | commentRepository.delete(comment);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|