1 | package finki.it.terapijamkbackend.spring.services;
|
---|
2 |
|
---|
3 | import finki.it.terapijamkbackend.spring.entities.APPOINTMENT_STATUS;
|
---|
4 | import finki.it.terapijamkbackend.spring.entities.Appointment;
|
---|
5 | import finki.it.terapijamkbackend.spring.entities.Request;
|
---|
6 | import finki.it.terapijamkbackend.spring.entities.User;
|
---|
7 | import finki.it.terapijamkbackend.spring.repositories.AppointmentRepository;
|
---|
8 | import finki.it.terapijamkbackend.spring.repositories.RequestRepository;
|
---|
9 | import finki.it.terapijamkbackend.spring.repositories.UserRepository;
|
---|
10 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | import java.time.LocalDateTime;
|
---|
14 | import java.time.format.DateTimeFormatter;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | @Service
|
---|
18 | public class RequestService {
|
---|
19 | @Autowired
|
---|
20 | private AppointmentRepository appointmentRepository;
|
---|
21 | @Autowired
|
---|
22 | private UserRepository userRepository;
|
---|
23 | @Autowired
|
---|
24 | private RequestRepository requestRepository;
|
---|
25 | public void bookAppointment(String datetime,String couponCode,String medicalCondition,String username) {
|
---|
26 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
---|
27 | LocalDateTime dateTime = LocalDateTime.parse(datetime, formatter);
|
---|
28 | Appointment appointment = appointmentRepository.findByTerm(dateTime);
|
---|
29 | if (appointment != null && appointment.getStatus().equals(APPOINTMENT_STATUS.FREE)) {
|
---|
30 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
31 | if (user == null) {
|
---|
32 | throw new RuntimeException("User not found.");
|
---|
33 | }
|
---|
34 |
|
---|
35 | Request request=new Request(user,dateTime,couponCode,medicalCondition);
|
---|
36 | requestRepository.save(request);
|
---|
37 | } else {
|
---|
38 | throw new RuntimeException("Appointment not found or already booked.");
|
---|
39 | }
|
---|
40 | }
|
---|
41 | public List<Request> findRequestsByUsername(String username) {
|
---|
42 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
43 | return requestRepository.findRequestsForFreeAppointments(user.getId());
|
---|
44 | }
|
---|
45 | public List<Request> findRequestsByTerm(String term) {
|
---|
46 | DateTimeFormatter inputFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
---|
47 | LocalDateTime dateTime = LocalDateTime.parse(term, inputFormatter);
|
---|
48 |
|
---|
49 | return requestRepository.findByTerm(dateTime);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public String cancelReservationById(String username,String term) {
|
---|
53 | try {
|
---|
54 | LocalDateTime ldt=LocalDateTime.parse(term);
|
---|
55 | User user=userRepository.findByUsername(username).orElse(null);
|
---|
56 | Request temp=requestRepository.findByUserIdAndTerm(user.getId(), ldt);
|
---|
57 | String info=temp.getAdditionalInfo();
|
---|
58 | requestRepository.deleteByIdAndTerm(user.getId(),ldt);
|
---|
59 | return info+"&"+user.getId();
|
---|
60 | } catch (Exception e) {
|
---|
61 | return "False";
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean isEmpty(String term) {
|
---|
66 | LocalDateTime temp=LocalDateTime.parse(term);
|
---|
67 | return requestRepository.isRequested(temp) <= 0;
|
---|
68 | }
|
---|
69 | public String findAdditionalInfoBasedOnUsernameAndTerm(String userId,LocalDateTime term)
|
---|
70 | {
|
---|
71 | Long idto=Long.parseLong(userId);
|
---|
72 | Request temp=requestRepository.findByUserIdAndTerm(idto,term);
|
---|
73 | String additionalInfo=temp.getAdditionalInfo();
|
---|
74 | requestRepository.delete(temp);
|
---|
75 | return additionalInfo;
|
---|
76 | }
|
---|
77 | public List<Long> getIdsByTermExcept(String term, String excludedUsername) {
|
---|
78 | LocalDateTime localDateTime=LocalDateTime.parse(term);
|
---|
79 | User excludedUser=userRepository.findByUsername(excludedUsername).orElse(null);
|
---|
80 | return requestRepository.findIdsByTermExcept(localDateTime, excludedUser.getId());
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|