source: src/main/java/finki/it/terapijamkbackend/spring/services/AppointmentService.java

Last change on this file was 743de55, checked in by macagaso <gasoskamarija@…>, 6 weeks ago

Initial commit

  • Property mode set to 100644
File size: 5.7 KB
Line 
1package finki.it.terapijamkbackend.spring.services;
2
3import finki.it.terapijamkbackend.spring.dto.AppointmentRequest;
4import finki.it.terapijamkbackend.spring.entities.APPOINTMENT_STATUS;
5import finki.it.terapijamkbackend.spring.entities.Appointment;
6import finki.it.terapijamkbackend.spring.entities.Request;
7import finki.it.terapijamkbackend.spring.entities.User;
8import finki.it.terapijamkbackend.spring.repositories.AppointmentRepository;
9import finki.it.terapijamkbackend.spring.repositories.RequestRepository;
10import finki.it.terapijamkbackend.spring.repositories.UserRepository;
11import jakarta.transaction.Transactional;
12import org.springframework.beans.factory.annotation.Autowired;
13import org.springframework.stereotype.Service;
14import java.time.LocalDate;
15import java.time.LocalDateTime;
16import java.time.LocalTime;
17import java.time.format.DateTimeFormatter;
18import java.util.List;
19import java.util.Optional;
20
21@Service
22public 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}
Note: See TracBrowser for help on using the repository browser.