source: backend/src/main/java/com/finki/icare/service/ConsultationSlotService.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: 3.9 KB
Line 
1package com.finki.icare.service;
2
3import com.finki.icare.dto.AddSlotRequest;
4import com.finki.icare.dto.ConsultationSlotsDTO;
5import com.finki.icare.enums.UserType;
6import com.finki.icare.exceptions.ICareException;
7import com.finki.icare.model.Therapist;
8import com.finki.icare.repository.TherapistRepository;
9import lombok.RequiredArgsConstructor;
10import org.springframework.stereotype.Service;
11
12import java.time.LocalDate;
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.List;
16import java.util.stream.Collectors;
17
18@Service
19@RequiredArgsConstructor
20public class ConsultationSlotService {
21
22 private final TherapistRepository therapistRepository;
23
24 public ConsultationSlotsDTO getTherapistSlots(Integer therapistId, String userType) {
25 if (!UserType.THERAPIST.equals(userType)) {
26 throw ICareException.forbidden("Only therapists can manage consultation slots");
27 }
28
29 Therapist therapist = therapistRepository
30 .findById(therapistId)
31 .orElseThrow(() -> ICareException.notFound("Therapist not found"));
32
33 List<LocalDate> slots = therapist.getConsultationSlots() != null
34 ? Arrays.asList(therapist.getConsultationSlots())
35 : new ArrayList<>();
36
37 return new ConsultationSlotsDTO(slots);
38 }
39
40 public ConsultationSlotsDTO addSlot(Integer therapistId, AddSlotRequest request, Integer currentUserId, String userType) {
41 if (!UserType.THERAPIST.equals(userType)) {
42 throw ICareException.forbidden("Only therapists can manage consultation slots");
43 }
44
45 if (!therapistId.equals(currentUserId)) {
46 throw ICareException.forbidden("You can only manage your own consultation slots");
47 }
48
49 Therapist therapist = therapistRepository
50 .findById(therapistId)
51 .orElseThrow(() -> ICareException.notFound("Therapist not found"));
52
53 if (request.getDate().isBefore(LocalDate.now())) {
54 throw ICareException.badRequest("Cannot add slots for past dates");
55 }
56
57 List<LocalDate> slots = therapist.getConsultationSlots() != null
58 ? new ArrayList<>(Arrays.asList(therapist.getConsultationSlots()))
59 : new ArrayList<>();
60
61 if (slots.contains(request.getDate())) {
62 throw ICareException.conflict("This date is already in your consultation slots");
63 }
64
65 slots.add(request.getDate());
66 slots.sort(LocalDate::compareTo);
67
68 therapist.setConsultationSlots(slots.toArray(new LocalDate[0]));
69 therapistRepository.save(therapist);
70
71 return new ConsultationSlotsDTO(slots);
72 }
73
74 public ConsultationSlotsDTO removeSlot(Integer therapistId, LocalDate date, Integer currentUserId, String userType) {
75 if (!UserType.THERAPIST.equals(userType)) {
76 throw ICareException.forbidden("Only therapists can manage consultation slots");
77 }
78
79 if (!therapistId.equals(currentUserId)) {
80 throw ICareException.forbidden("You can only manage your own consultation slots");
81 }
82
83 Therapist therapist = therapistRepository
84 .findById(therapistId)
85 .orElseThrow(() -> ICareException.notFound("Therapist not found"));
86
87 if (therapist.getConsultationSlots() == null) {
88 throw ICareException.notFound("No consultation slots found");
89 }
90
91 if (date.isBefore(LocalDate.now())) {
92 throw ICareException.badRequest("Cannot add slots for past dates");
93 }
94
95 List<LocalDate> slots = new ArrayList<>(Arrays.asList(therapist.getConsultationSlots()));
96
97 if (!slots.remove(date)) {
98 throw ICareException.notFound("This date is not in your consultation slots");
99 }
100
101 therapist.setConsultationSlots(slots.toArray(new LocalDate[0]));
102 therapistRepository.save(therapist);
103
104 return new ConsultationSlotsDTO(slots);
105 }
106}
Note: See TracBrowser for help on using the repository browser.