source: backend/src/main/java/com/finki/icare/service/ConsultationService.java@ 700e2f9

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

Init

  • Property mode set to 100644
File size: 6.3 KB
Line 
1package com.finki.icare.service;
2
3import com.finki.icare.dto.ConsultationDTO;
4import com.finki.icare.dto.CreateConsultationRequest;
5import com.finki.icare.dto.UpdateConsultationRequest;
6import com.finki.icare.enums.UserType;
7import com.finki.icare.exceptions.ICareException;
8import com.finki.icare.mapper.ConsultationMapper;
9import com.finki.icare.model.Consultation;
10import com.finki.icare.model.Patient;
11import com.finki.icare.model.Therapist;
12import com.finki.icare.repository.ConsultationRepository;
13import com.finki.icare.repository.PatientRepository;
14import com.finki.icare.repository.TherapistRepository;
15import lombok.RequiredArgsConstructor;
16import org.springframework.stereotype.Service;
17import org.springframework.transaction.annotation.Transactional;
18
19import java.util.List;
20
21@Service
22@RequiredArgsConstructor
23public class ConsultationService {
24
25 private final ConsultationRepository consultationRepository;
26 private final PatientRepository patientRepository;
27 private final TherapistRepository therapistRepository;
28 private final TherapyService therapyService;
29 private final ConsultationMapper consultationMapper;
30
31 public List<ConsultationDTO> getTherapistConsultations(Integer therapistId, Integer currentUserId, String userType) {
32 if (!UserType.THERAPIST.equals(userType) || !therapistId.equals(currentUserId)) {
33 throw ICareException.forbidden("You do not have permission to view these consultations");
34 }
35
36 List<Consultation> consultations = consultationRepository.findByTherapistId(therapistId);
37 return consultations.stream()
38 .map(consultationMapper::toDTO)
39 .toList();
40 }
41
42 public List<ConsultationDTO> getPatientConsultations(Integer patientId, Integer currentUserId, String userType) {
43 if (!UserType.PATIENT.equals(userType) || !patientId.equals(currentUserId)) {
44 throw ICareException.forbidden("You do not have permission to view these consultations");
45 }
46
47 List<Consultation> consultations = consultationRepository.findByPatientId(patientId);
48 return consultations.stream()
49 .map(consultationMapper::toDTO)
50 .toList();
51 }
52
53 @Transactional
54 public ConsultationDTO createConsultation(CreateConsultationRequest request, Integer currentUserId, String userType) {
55 if (!UserType.THERAPIST.equals(userType)) {
56 throw ICareException.forbidden("Only therapists can create consultation records");
57 }
58
59 Patient patient = patientRepository
60 .findById(request.getPatientId())
61 .orElseThrow(() -> ICareException.notFound("Patient not found"));
62
63 Therapist therapist = therapistRepository
64 .findById(currentUserId)
65 .orElseThrow(() -> ICareException.notFound("Therapist not found"));
66
67 if (request.getPrice() == null || request.getPrice().compareTo(java.math.BigDecimal.ZERO) <= 0) {
68 throw ICareException.badRequest("Price must be a positive value");
69 }
70
71 if (request.getDate() == null) {
72 throw ICareException.badRequest("Consultation date is required");
73 }
74
75 Consultation consultation = new Consultation();
76 consultation.setPatient(patient);
77 consultation.setTherapist(therapist);
78 consultation.setDate(request.getDate());
79 consultation.setPrice(request.getPrice());
80 consultation.setAdvice(request.getAdvice());
81 consultation.setDateOfPayment(request.getDateOfPayment());
82
83 Consultation savedConsultation = consultationRepository.save(consultation);
84
85 therapyService.createTherapiesForConsultation(savedConsultation, request.getTherapies());
86
87 return consultationMapper
88 .toDTO(consultationRepository.findById(savedConsultation.getIdConsultation())
89 .orElseThrow(() -> ICareException.notFound("Consultation not found")));
90 }
91
92 @Transactional
93 public ConsultationDTO updateConsultation(Integer consultationId, UpdateConsultationRequest request, Integer currentUserId, String userType) {
94 if (!UserType.THERAPIST.equals(userType)) {
95 throw ICareException.forbidden("Only therapists can update consultation records");
96 }
97
98 Consultation consultation = consultationRepository.findById(consultationId)
99 .orElseThrow(() -> ICareException.notFound("Consultation not found"));
100
101 if (!consultation.getTherapist().getIdUser().equals(currentUserId)) {
102 throw ICareException.forbidden("You can only update your own consultation records");
103 }
104
105 if (request.getDate() != null) {
106 consultation.setDate(request.getDate());
107 }
108
109 if (request.getPrice() != null) {
110 if (request.getPrice().compareTo(java.math.BigDecimal.ZERO) <= 0) {
111 throw ICareException.badRequest("Price must be a positive value");
112 }
113 consultation.setPrice(request.getPrice());
114 }
115
116 if (request.getAdvice() != null) {
117 consultation.setAdvice(request.getAdvice());
118 }
119
120 consultation.setDateOfPayment(request.getDateOfPayment());
121
122 Consultation updatedConsultation = consultationRepository.save(consultation);
123
124 if (request.getTherapies() != null && !request.getTherapies().isEmpty()) {
125 therapyService.createTherapiesForConsultation(updatedConsultation, request.getTherapies());
126 }
127
128 return consultationMapper
129 .toDTO(consultationRepository.findById(updatedConsultation.getIdConsultation())
130 .orElseThrow(() -> ICareException.notFound("Consultation not found")));
131 }
132
133 public void deleteConsultation(Integer consultationId, Integer currentUserId, String userType) {
134 if (!UserType.THERAPIST.equals(userType)) {
135 throw ICareException.forbidden("Only therapists can delete consultation records");
136 }
137
138 Consultation consultation = consultationRepository.findById(consultationId)
139 .orElseThrow(() -> ICareException.notFound("Consultation not found"));
140
141 if (!consultation.getTherapist().getIdUser().equals(currentUserId)) {
142 throw ICareException.forbidden("You can only delete your own consultation records");
143 }
144
145 consultationRepository.delete(consultation);
146 }
147}
Note: See TracBrowser for help on using the repository browser.