| 1 | package com.finki.icare.service;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.CreateTherapyRequest;
|
|---|
| 4 | import com.finki.icare.dto.TherapyDTO;
|
|---|
| 5 | import com.finki.icare.dto.UpdateTherapyRequest;
|
|---|
| 6 | import com.finki.icare.enums.UserType;
|
|---|
| 7 | import com.finki.icare.exceptions.ICareException;
|
|---|
| 8 | import com.finki.icare.mapper.TherapyMapper;
|
|---|
| 9 | import com.finki.icare.model.Consultation;
|
|---|
| 10 | import com.finki.icare.model.Therapy;
|
|---|
| 11 | import com.finki.icare.repository.ConsultationRepository;
|
|---|
| 12 | import com.finki.icare.repository.TherapyRepository;
|
|---|
| 13 | import lombok.RequiredArgsConstructor;
|
|---|
| 14 | import org.springframework.stereotype.Service;
|
|---|
| 15 | import org.springframework.transaction.annotation.Transactional;
|
|---|
| 16 |
|
|---|
| 17 | import java.util.ArrayList;
|
|---|
| 18 | import java.util.List;
|
|---|
| 19 |
|
|---|
| 20 | @Service
|
|---|
| 21 | @RequiredArgsConstructor
|
|---|
| 22 | public class TherapyService {
|
|---|
| 23 |
|
|---|
| 24 | private final TherapyRepository therapyRepository;
|
|---|
| 25 | private final ConsultationRepository consultationRepository;
|
|---|
| 26 | private final TherapyMapper therapyMapper;
|
|---|
| 27 |
|
|---|
| 28 | public List<TherapyDTO> getTherapiesByConsultation(Integer consultationId, Integer currentUserId, String userType) {
|
|---|
| 29 | Consultation consultation = consultationRepository.findById(consultationId)
|
|---|
| 30 | .orElseThrow(() -> ICareException.notFound("Consultation not found"));
|
|---|
| 31 |
|
|---|
| 32 | if (!UserType.THERAPIST.equals(userType) || !consultation.getTherapist().getIdUser().equals(currentUserId)) {
|
|---|
| 33 | throw ICareException.forbidden("You do not have permission to view therapies for this consultation");
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | List<Therapy> therapies = therapyRepository.findByConsultationId(consultationId);
|
|---|
| 37 | return therapies.stream()
|
|---|
| 38 | .map(therapyMapper::toDTO)
|
|---|
| 39 | .toList();
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | public TherapyDTO createTherapy(Integer consultationId, CreateTherapyRequest request, Integer currentUserId, String userType) {
|
|---|
| 43 | if (!UserType.THERAPIST.equals(userType)) {
|
|---|
| 44 | throw ICareException.forbidden("Only therapists can create therapies");
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | Consultation consultation = consultationRepository.findById(consultationId)
|
|---|
| 48 | .orElseThrow(() -> ICareException.notFound("Consultation not found"));
|
|---|
| 49 |
|
|---|
| 50 | if (!consultation.getTherapist().getIdUser().equals(currentUserId)) {
|
|---|
| 51 | throw ICareException.forbidden("You can only add therapies to your own consultations");
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | validateTherapyRequest(request.getName(), request.getDose(), request.getExpDate());
|
|---|
| 55 |
|
|---|
| 56 | Therapy therapy = new Therapy();
|
|---|
| 57 | therapy.setConsultation(consultation);
|
|---|
| 58 | therapy.setName(request.getName());
|
|---|
| 59 | therapy.setDose(request.getDose());
|
|---|
| 60 | therapy.setExpDate(request.getExpDate());
|
|---|
| 61 |
|
|---|
| 62 | Therapy savedTherapy = therapyRepository.save(therapy);
|
|---|
| 63 | return therapyMapper.toDTO(savedTherapy);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public TherapyDTO updateTherapy(Integer therapyId, UpdateTherapyRequest request, Integer currentUserId, String userType) {
|
|---|
| 67 | if (!UserType.THERAPIST.equals(userType)) {
|
|---|
| 68 | throw ICareException.forbidden("Only therapists can update therapies");
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | Therapy therapy = therapyRepository.findById(therapyId)
|
|---|
| 72 | .orElseThrow(() -> ICareException.notFound("Therapy not found"));
|
|---|
| 73 |
|
|---|
| 74 | if (!therapy.getConsultation().getTherapist().getIdUser().equals(currentUserId)) {
|
|---|
| 75 | throw ICareException.forbidden("You can only update therapies from your own consultations");
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | if (request.getName() != null && !request.getName().trim().isEmpty()) {
|
|---|
| 79 | therapy.setName(request.getName());
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (request.getDose() != null && !request.getDose().trim().isEmpty()) {
|
|---|
| 83 | therapy.setDose(request.getDose());
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | if (request.getExpDate() != null) {
|
|---|
| 87 | therapy.setExpDate(request.getExpDate());
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | Therapy updatedTherapy = therapyRepository.save(therapy);
|
|---|
| 91 | return therapyMapper.toDTO(updatedTherapy);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | public void deleteTherapy(Integer therapyId, Integer currentUserId, String userType) {
|
|---|
| 95 | if (!UserType.THERAPIST.equals(userType)) {
|
|---|
| 96 | throw ICareException.forbidden("Only therapists can delete therapies");
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | Therapy therapy = therapyRepository.findById(therapyId)
|
|---|
| 100 | .orElseThrow(() -> ICareException.notFound("Therapy not found"));
|
|---|
| 101 |
|
|---|
| 102 | if (!therapy.getConsultation().getTherapist().getIdUser().equals(currentUserId)) {
|
|---|
| 103 | throw ICareException.forbidden("You can only delete therapies from your own consultations");
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | therapyRepository.delete(therapy);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | private void validateTherapyRequest(String name, String dose, java.time.LocalDate expDate) {
|
|---|
| 110 | if (name == null || name.trim().isEmpty()) {
|
|---|
| 111 | throw ICareException.badRequest("Therapy name is required");
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (dose == null || dose.trim().isEmpty()) {
|
|---|
| 115 | throw ICareException.badRequest("Therapy dose is required");
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (expDate == null) {
|
|---|
| 119 | throw ICareException.badRequest("Therapy expiration date is required");
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | @Transactional
|
|---|
| 124 | public void createTherapiesForConsultation(Consultation consultation, List<CreateTherapyRequest> therapyRequests) {
|
|---|
| 125 | if (therapyRequests == null || therapyRequests.isEmpty()) {
|
|---|
| 126 | return;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | List<Therapy> therapies = new ArrayList<>();
|
|---|
| 130 |
|
|---|
| 131 | for (CreateTherapyRequest request : therapyRequests) {
|
|---|
| 132 | validateTherapyRequest(request.getName(), request.getDose(), request.getExpDate());
|
|---|
| 133 |
|
|---|
| 134 | Therapy therapy = new Therapy();
|
|---|
| 135 | therapy.setConsultation(consultation);
|
|---|
| 136 | therapy.setName(request.getName());
|
|---|
| 137 | therapy.setDose(request.getDose());
|
|---|
| 138 | therapy.setExpDate(request.getExpDate());
|
|---|
| 139 | therapies.add(therapy);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | therapyRepository.saveAll(therapies);
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|