1 | package finki.it.terapijamkbackend.spring.services;
|
---|
2 |
|
---|
3 | import finki.it.terapijamkbackend.spring.dto.AppointmentRequest;
|
---|
4 | import finki.it.terapijamkbackend.spring.entities.APPOINTMENT_STATUS;
|
---|
5 | import finki.it.terapijamkbackend.spring.entities.Appointment;
|
---|
6 | import finki.it.terapijamkbackend.spring.entities.Request;
|
---|
7 | import finki.it.terapijamkbackend.spring.entities.User;
|
---|
8 | import finki.it.terapijamkbackend.spring.repositories.AppointmentRepository;
|
---|
9 | import finki.it.terapijamkbackend.spring.repositories.RequestRepository;
|
---|
10 | import finki.it.terapijamkbackend.spring.repositories.UserRepository;
|
---|
11 | import jakarta.transaction.Transactional;
|
---|
12 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
13 | import org.springframework.stereotype.Service;
|
---|
14 | import java.time.LocalDate;
|
---|
15 | import java.time.LocalDateTime;
|
---|
16 | import java.time.LocalTime;
|
---|
17 | import java.time.format.DateTimeFormatter;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Optional;
|
---|
20 |
|
---|
21 | @Service
|
---|
22 | public class AppointmentService {
|
---|
23 | @Autowired
|
---|
24 | private AppointmentRepository appointmentRepository;
|
---|
25 | @Autowired
|
---|
26 | private UserRepository userRepository;
|
---|
27 | @Autowired
|
---|
28 | private RequestRepository requestRepository;
|
---|
29 | public List<Appointment> findFreeAppointmentsByDate(LocalDate date){
|
---|
30 | return appointmentRepository.findAppointmentsByDateAndStatus(date);
|
---|
31 | }
|
---|
32 | public List<Appointment> findRequestsByUsernameAndStatus(String username,APPOINTMENT_STATUS status) {
|
---|
33 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
34 | return appointmentRepository.findByUserIdAndStatus(user.getId(),status);
|
---|
35 | }
|
---|
36 | public void createAppointment(String dateTimeStr) {
|
---|
37 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
---|
38 | LocalDateTime appointmentDateTime = LocalDateTime.parse(dateTimeStr, formatter);
|
---|
39 | Appointment appointment=new Appointment(appointmentDateTime);
|
---|
40 | appointmentRepository.save(appointment);
|
---|
41 | }
|
---|
42 | public List<Appointment> findAppointmentsByDate(LocalDate date){
|
---|
43 | return appointmentRepository.findAppointmentsByDate(date);
|
---|
44 | }
|
---|
45 | public void addAppointment(String username,String dateTime){
|
---|
46 | LocalDateTime temp=LocalDateTime.parse(dateTime);
|
---|
47 | Optional<Appointment> appointmentOpt = appointmentRepository.findByDateTime(temp);
|
---|
48 | if(appointmentOpt.isPresent()){
|
---|
49 | Appointment appointment = appointmentOpt.get();
|
---|
50 | appointment.setStatus(APPOINTMENT_STATUS.RESERVED);
|
---|
51 | User user = userRepository.findByUsername(username).orElse(null);
|
---|
52 | long userId=user.getId();
|
---|
53 |
|
---|
54 | Request request=requestRepository.findByUserIdAndTerm(userId,temp);
|
---|
55 | appointment.setRequest(request);
|
---|
56 | appointmentRepository.save(appointment);
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | throw new IllegalArgumentException("Appointment not found for the provided username and dateTime");
|
---|
60 | }
|
---|
61 | }
|
---|
62 | public boolean isAppointmentReserved(String term) {
|
---|
63 | LocalDateTime temp=LocalDateTime.parse(term);
|
---|
64 | return appointmentRepository.isAppointmentReserved(temp);
|
---|
65 | }
|
---|
66 | public Appointment findAppointmentByTerm(String term){
|
---|
67 | DateTimeFormatter inputFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
---|
68 | LocalDateTime dateTime = LocalDateTime.parse(term, inputFormatter);
|
---|
69 | return appointmentRepository.findByTerm(dateTime);
|
---|
70 | }
|
---|
71 | public long updateAppointment(String term)
|
---|
72 | {
|
---|
73 | LocalDateTime localDateTime = LocalDateTime.parse(term);
|
---|
74 | Appointment temp = appointmentRepository.findByTerm(localDateTime);
|
---|
75 | Long userId=temp.getRequest().getUser().getId();
|
---|
76 | temp.setRequest(null);
|
---|
77 | temp.setStatus(APPOINTMENT_STATUS.FREE);
|
---|
78 | appointmentRepository.save(temp);
|
---|
79 | return userId;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public String cancelAppointmentById(String username,String term) {
|
---|
83 | try {
|
---|
84 | LocalDateTime ldt=LocalDateTime.parse(term);
|
---|
85 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
86 | Long appointmentId = appointmentRepository.findIdByUserIdAndTerm(user.getId(), ldt);
|
---|
87 | appointmentRepository.updateStatusToFree(appointmentId);
|
---|
88 | Request request = requestRepository.findByUserIdAndTerm(user.getId(), ldt);
|
---|
89 | requestRepository.delete(request);
|
---|
90 | return request.getAdditionalInfo()+"&"+user.getId();
|
---|
91 | } catch (Exception e) {
|
---|
92 | return "False";
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Transactional
|
---|
97 | public void deleteAppointmentByTerm(String term) {
|
---|
98 | LocalDateTime temp=LocalDateTime.parse(term);
|
---|
99 | if (appointmentRepository.existsByTerm(temp)) {
|
---|
100 | appointmentRepository.deleteByTerm(temp);
|
---|
101 | } else {
|
---|
102 | throw new IllegalArgumentException("Appointment with term " + term + " does not exist");
|
---|
103 | }
|
---|
104 | }
|
---|
105 | public List<Appointment> getAllAppointments() {
|
---|
106 | return appointmentRepository.findAllSortedByDateTime();
|
---|
107 | }
|
---|
108 | public void createAppointments(List<AppointmentRequest> requests) {
|
---|
109 | for (AppointmentRequest request : requests) {
|
---|
110 | LocalDateTime appointmentDateTime = LocalDateTime.of(
|
---|
111 | LocalDate.parse(request.getDate()),
|
---|
112 | LocalTime.parse(request.getTime())
|
---|
113 | );
|
---|
114 | Appointment appointment = new Appointment();
|
---|
115 | appointment.setTerm(appointmentDateTime);
|
---|
116 | appointment.setStatus(APPOINTMENT_STATUS.FREE);
|
---|
117 | appointmentRepository.save(appointment);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | @Transactional
|
---|
121 | public void deleteFreeAppointmentsInRange(LocalDate startDate, LocalDate endDate) {
|
---|
122 | appointmentRepository.deleteByDateRangeAndStatus(startDate, endDate);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public int getNumberOfAppointmentsByUserId(String userId){
|
---|
126 | return appointmentRepository.countAppointmentsByUserId(Long.parseLong(userId));
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | }
|
---|