source: backend/src/main/java/com/finki/icare/service/PatientService.java

main
Last change on this file was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[700e2f9]1package com.finki.icare.service;
2
3import com.finki.icare.dto.PatientDTO;
4import com.finki.icare.exceptions.ICareException;
5import com.finki.icare.mapper.PatientMapper;
6import com.finki.icare.model.Patient;
7import com.finki.icare.model.Therapist;
8import com.finki.icare.repository.PatientRepository;
9import com.finki.icare.repository.TherapistRepository;
10import lombok.RequiredArgsConstructor;
11import org.springframework.stereotype.Service;
12import org.springframework.transaction.annotation.Transactional;
13
14import java.util.List;
15
16@Service
17@RequiredArgsConstructor
18public 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}
Note: See TracBrowser for help on using the repository browser.