| 1 | package com.finki.icare.service;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.BlogDTO;
|
|---|
| 4 | import com.finki.icare.dto.CreateBlogRequest;
|
|---|
| 5 | import com.finki.icare.dto.UpdateBlogRequest;
|
|---|
| 6 | import com.finki.icare.exceptions.ICareException;
|
|---|
| 7 | import com.finki.icare.mapper.BlogMapper;
|
|---|
| 8 | import com.finki.icare.mapper.CommentMapper;
|
|---|
| 9 | import com.finki.icare.model.Blog;
|
|---|
| 10 | import com.finki.icare.model.Patient;
|
|---|
| 11 | import com.finki.icare.repository.BlogRepository;
|
|---|
| 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 | import java.util.List;
|
|---|
| 19 |
|
|---|
| 20 | @Service
|
|---|
| 21 | @RequiredArgsConstructor
|
|---|
| 22 | public class BlogService {
|
|---|
| 23 |
|
|---|
| 24 | private final BlogRepository blogRepository;
|
|---|
| 25 | private final PatientRepository patientRepository;
|
|---|
| 26 | private final BlogMapper blogMapper;
|
|---|
| 27 | private final CommentMapper commentMapper;
|
|---|
| 28 |
|
|---|
| 29 | @Transactional(readOnly = true)
|
|---|
| 30 | public List<BlogDTO> getAllBlogs(Integer patientId) {
|
|---|
| 31 | getPatient(patientId);
|
|---|
| 32 |
|
|---|
| 33 | return blogRepository
|
|---|
| 34 | .findAllByOrderByDateOfPostDesc()
|
|---|
| 35 | .stream()
|
|---|
| 36 | .map(blog -> blogMapper.toDTO(blog, patientId, false, commentMapper))
|
|---|
| 37 | .toList();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Transactional(readOnly = true)
|
|---|
| 41 | public BlogDTO getBlogById(Integer blogId, Integer patientId) {
|
|---|
| 42 | getPatient(patientId);
|
|---|
| 43 | Blog blog = getBlog(blogId);
|
|---|
| 44 |
|
|---|
| 45 | return blogMapper.toDTO(blog, patientId, true, commentMapper);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | @Transactional
|
|---|
| 49 | public BlogDTO createBlog(CreateBlogRequest request, Integer patientId) {
|
|---|
| 50 | Patient patient = getPatient(patientId);
|
|---|
| 51 |
|
|---|
| 52 | Blog blog = new Blog();
|
|---|
| 53 | blog.setTitle(request.getTitle());
|
|---|
| 54 | blog.setContent(request.getContent());
|
|---|
| 55 | blog.setPatient(patient);
|
|---|
| 56 | blog.setDateOfPost(OffsetDateTime.now());
|
|---|
| 57 |
|
|---|
| 58 | Blog savedBlog = blogRepository.save(blog);
|
|---|
| 59 | return blogMapper.toDTO(savedBlog, patientId, false, commentMapper);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Transactional
|
|---|
| 63 | public BlogDTO updateBlog(Integer blogId, UpdateBlogRequest request, Integer patientId) {
|
|---|
| 64 | Blog blog = getBlog(blogId);
|
|---|
| 65 |
|
|---|
| 66 | if (!blog.getPatient().getIdUser().equals(patientId)) {
|
|---|
| 67 | throw ICareException.forbidden("You are not authorized to update this blog");
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | blog.setTitle(request.getTitle());
|
|---|
| 71 | blog.setContent(request.getContent());
|
|---|
| 72 |
|
|---|
| 73 | Blog updatedBlog = blogRepository.save(blog);
|
|---|
| 74 | return blogMapper.toDTO(updatedBlog, patientId, true, commentMapper);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | @Transactional
|
|---|
| 78 | public void deleteBlog(Integer blogId, Integer patientId) {
|
|---|
| 79 | Blog blog = getBlog(blogId);
|
|---|
| 80 |
|
|---|
| 81 | if (!blog.getPatient().getIdUser().equals(patientId)) {
|
|---|
| 82 | throw ICareException.forbidden("You are not authorized to delete this blog");
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | blogRepository.delete(blog);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Transactional
|
|---|
| 89 | public void toggleLike(Integer blogId, Integer patientId) {
|
|---|
| 90 | Blog blog = getBlog(blogId);
|
|---|
| 91 | Patient patient = getPatient(patientId);
|
|---|
| 92 |
|
|---|
| 93 | if (patient.getLikedBlogs() == null) {
|
|---|
| 94 | patient.setLikedBlogs(new java.util.ArrayList<>());
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | boolean alreadyLiked = patient
|
|---|
| 98 | .getLikedBlogs()
|
|---|
| 99 | .stream()
|
|---|
| 100 | .anyMatch(b -> b.getIdBlog().equals(blogId));
|
|---|
| 101 |
|
|---|
| 102 | if (alreadyLiked) {
|
|---|
| 103 | patient.getLikedBlogs().removeIf(b -> b.getIdBlog().equals(blogId));
|
|---|
| 104 | } else {
|
|---|
| 105 | patient.getLikedBlogs().add(blog);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | patientRepository.save(patient);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | private Patient getPatient(Integer patientId) {
|
|---|
| 112 | return patientRepository
|
|---|
| 113 | .findById(patientId)
|
|---|
| 114 | .orElseThrow(() -> ICareException.forbidden("Only patients can access blogs"));
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | private Blog getBlog(Integer blogId) {
|
|---|
| 118 | return blogRepository
|
|---|
| 119 | .findById(blogId)
|
|---|
| 120 | .orElseThrow(() -> ICareException.notFound("Blog not found with id: " + blogId));
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|