-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppointmentService.java
More file actions
172 lines (145 loc) · 7.75 KB
/
Copy pathAppointmentService.java
File metadata and controls
172 lines (145 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.cts.healthcare.service;
import com.cts.healthcare.entity.Appointment;
import com.cts.healthcare.entity.DoctorAvailability;
import com.cts.healthcare.entity.Status;
import com.cts.healthcare.entity.TimeSlot;
import com.cts.healthcare.entity.User;
import com.cts.healthcare.repository.AppointmentRepository;
import com.cts.healthcare.repository.DoctorAvailabilityRepository;
import com.cts.healthcare.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.*;
@Service
public class AppointmentService {
private final AppointmentRepository appointmentRepository;
private final DoctorAvailabilityRepository availabilityRepository;
private final UserRepository userRepository;
private final DoctorAvailabilityService doctorAvailabilityService;
public AppointmentService(AppointmentRepository appointmentRepository, DoctorAvailabilityRepository availabilityRepository, UserRepository userRepository, DoctorAvailabilityService doctorAvailabilityService) {
this.appointmentRepository = appointmentRepository;
this.availabilityRepository = availabilityRepository;
this.userRepository = userRepository;
this.doctorAvailabilityService = doctorAvailabilityService;
}
public String bookAppointment(Appointment appointment) {
// Validate if the doctor exists in the Users table
User doctor = userRepository.findById(appointment.getDoctor().getId())
.orElseThrow(() -> new IllegalArgumentException("Doctor not found with ID: " + appointment.getDoctor().getId()));
// Validate if the patient exists in the Users table
User patient = userRepository.findById(appointment.getPatient().getId())
.orElseThrow(() -> new IllegalArgumentException("Patient not found with ID: " + appointment.getPatient().getId()));
// Check if the time slot is blocked by the doctor
if (doctorAvailabilityService.isTimeSlotBlocked(doctor, appointment.getDate(), appointment.getTimeSlot())) {
return "The selected time slot is blocked by the doctor.";
}
// Check if the time slot exists in the availability table
Optional<DoctorAvailability> availability = availabilityRepository.findByDoctorIdAndDateAndTimeSlot(
doctor.getId(), appointment.getDate(), appointment.getTimeSlot()
).stream().findFirst();
if (availability.isPresent()) {
// If the time slot exists, it is unavailable
return "The selected time slot is unavailable.";
} else {
// If the time slot does not exist, book the appointment and mark it as unavailable
DoctorAvailability newAvailability = new DoctorAvailability();
newAvailability.setDoctor(doctor);
newAvailability.setDate(appointment.getDate());
newAvailability.setTimeSlot(appointment.getTimeSlot());
availabilityRepository.save(newAvailability);
// Save the appointment
appointment.setDoctor(doctor);
appointment.setPatient(patient);
appointment.setStatus(Status.BOOKED);
appointmentRepository.save(appointment);
return "Appointment booked successfully!";
}
}
@Transactional
public String cancelAppointment(Long appointmentId) {
Optional<Appointment> appointment = appointmentRepository.findByAppointmentId(appointmentId);
if (appointment.isPresent()) {
// Update appointment status
appointment.get().setStatus(Status.CANCELLED);
appointmentRepository.save(appointment.get());
// Delete availability
availabilityRepository.deleteByDoctorIdAndDateAndTimeSlot(
appointment.get().getDoctor().getId(),
appointment.get().getDate(),
appointment.get().getTimeSlot()
);
return "Appointment cancelled successfully!";
} else {
return "Appointment not found.";
}
}
@Transactional
public String modifyAppointment(Long appointmentId, LocalDate newDate, TimeSlot newTimeSlot) {
Optional<Appointment> appointment = appointmentRepository.findById(appointmentId);
if (appointment.isPresent()) {
Appointment existingAppointment = appointment.get();
// Delete the old availability
availabilityRepository.deleteByDoctorIdAndDateAndTimeSlot(
existingAppointment.getDoctor().getId(),
existingAppointment.getDate(),
existingAppointment.getTimeSlot()
);
// Check if the new time slot is blocked by the doctor
if (doctorAvailabilityService.isTimeSlotBlocked(existingAppointment.getDoctor(), newDate, newTimeSlot)) {
return "The selected new time slot is blocked by the doctor.";
}
// Check if the new time slot is available
Optional<DoctorAvailability> newAvailability = availabilityRepository.findByDoctorIdAndDateAndTimeSlot(
existingAppointment.getDoctor().getId(), newDate, newTimeSlot
).stream().findFirst();
if (newAvailability.isPresent()) {
// If the new time slot exists, it is unavailable
return "The selected new time slot is unavailable.";
} else {
// If the new time slot does not exist, mark it as unavailable
DoctorAvailability updatedAvailability = new DoctorAvailability();
updatedAvailability.setDoctor(existingAppointment.getDoctor());
updatedAvailability.setDate(newDate);
updatedAvailability.setTimeSlot(newTimeSlot);
availabilityRepository.save(updatedAvailability);
// Save the updated appointment
existingAppointment.setDate(newDate);
existingAppointment.setTimeSlot(newTimeSlot);
appointmentRepository.save(existingAppointment);
return "Appointment modified successfully!";
}
} else {
return "Appointment not found.";
}
}
@Transactional
public String completeAppointment(Long appointmentId) {
Optional<Appointment> appointment = appointmentRepository.findByAppointmentId(appointmentId);
if (appointment.isPresent()) {
// Update appointment status to COMPLETED
appointment.get().setStatus(Status.COMPLETED);
appointmentRepository.save(appointment.get());
return "Appointment marked as completed!";
} else {
return "Appointment not found.";
}
}
public List<Appointment> getUpcomingAppointments(Long patientId) {
return appointmentRepository.findByPatientIdAndStatus(patientId, Status.BOOKED);
}
public List<Appointment> getUpcomingAppointmentsForDoctor(Long doctorId) {
return appointmentRepository.findByDoctorIdAndStatus(doctorId, Status.BOOKED);
}
public List<Appointment> getCompletedAppointmentsForPatients(Long patientId) {
System.out.println(appointmentRepository.findByDoctorIdAndStatus(patientId, Status.COMPLETED));
return appointmentRepository.findByPatientIdAndStatus(patientId, Status.COMPLETED);
}
public List<Appointment> getCompletedAppointmentsForDoctors(Long patientId) {
System.out.println(appointmentRepository.findByDoctorIdAndStatus(patientId, Status.COMPLETED));
return appointmentRepository.findByDoctorIdAndStatus(patientId, Status.COMPLETED);
}
public Optional<Appointment> giveByAppointmentId(Long id) {
return appointmentRepository.findByAppointmentId(id);
}
}