| [700e2f9] | 1 | package com.finki.icare.service;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.PatientDTO;
|
|---|
| 4 | import com.finki.icare.exceptions.ICareException;
|
|---|
| 5 | import com.finki.icare.mapper.PatientMapper;
|
|---|
| 6 | import com.finki.icare.model.Patient;
|
|---|
| 7 | import com.finki.icare.model.Therapist;
|
|---|
| 8 | import com.finki.icare.repository.PatientRepository;
|
|---|
| 9 | import com.finki.icare.repository.TherapistRepository;
|
|---|
| 10 | import lombok.RequiredArgsConstructor;
|
|---|
| 11 | import org.springframework.stereotype.Service;
|
|---|
| 12 | import org.springframework.transaction.annotation.Transactional;
|
|---|
| 13 |
|
|---|
| 14 | import java.util.List;
|
|---|
| 15 |
|
|---|
| 16 | @Service
|
|---|
| 17 | @RequiredArgsConstructor
|
|---|
| 18 | public class PatientService {
|
|---|
| 19 |
|
|---|
| 20 | private final PatientRepository patientRepository;
|
|---|
| 21 | private final TherapistRepository therapistRepository;
|
|---|
| 22 | private final PatientMapper patientMapper;
|
|---|
| 23 |
|
|---|
| 24 | @Transactional
|
|---|
| 25 | public void setTherapist(Integer patientId, Integer therapistId) {
|
|---|
| 26 | Patient patient = patientRepository
|
|---|
| 27 | .findById(patientId)
|
|---|
| 28 | .orElseThrow(() -> ICareException.notFound("Patient not found"));
|
|---|
| 29 |
|
|---|
| 30 | Therapist therapist = therapistRepository
|
|---|
| 31 | .findById(therapistId)
|
|---|
| 32 | .orElseThrow(() -> ICareException.notFound("Therapist not found"));
|
|---|
| 33 |
|
|---|
| 34 | patient.setTherapist(therapist);
|
|---|
| 35 | patientRepository.save(patient);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | @Transactional
|
|---|
| 39 | public void removeTherapist(Integer patientId) {
|
|---|
| 40 | Patient patient = patientRepository
|
|---|
| 41 | .findById(patientId)
|
|---|
| 42 | .orElseThrow(() -> ICareException.notFound("Patient not found"));
|
|---|
| 43 |
|
|---|
| 44 | patient.setTherapist(null);
|
|---|
| 45 | patientRepository.save(patient);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public Integer getCurrentTherapistId(Integer patientId) {
|
|---|
| 49 | Patient patient = patientRepository
|
|---|
| 50 | .findById(patientId)
|
|---|
| 51 | .orElseThrow(() -> ICareException.notFound("Patient not found"));
|
|---|
| 52 |
|
|---|
| 53 | return patient.getTherapist() != null ? patient.getTherapist().getIdUser() : null;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | public List<PatientDTO> getAllPatients() {
|
|---|
| 57 | return patientRepository.findAll().stream()
|
|---|
| 58 | .map(patientMapper::toDTO)
|
|---|
| 59 | .toList();
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|