source: src/main/java/finki/it/terapijamkbackend/spring/controllers/AppointmentController.java

Last change on this file was 43c9090, checked in by macagaso <gasoskamarija@…>, 5 weeks ago

Updated version

  • Property mode set to 100644
File size: 7.1 KB
Line 
1package finki.it.terapijamkbackend.spring.controllers;
2import finki.it.terapijamkbackend.spring.dto.ApiResponse;
3import finki.it.terapijamkbackend.spring.dto.AppointmentRequest;
4import finki.it.terapijamkbackend.spring.dto.AppointmentResponse;
5import finki.it.terapijamkbackend.spring.dto.TermsResponse;
6import finki.it.terapijamkbackend.spring.entities.APPOINTMENT_STATUS;
7import finki.it.terapijamkbackend.spring.entities.Appointment;
8import finki.it.terapijamkbackend.spring.services.AppointmentService;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.format.annotation.DateTimeFormat;
11import org.springframework.http.HttpStatus;
12import org.springframework.http.ResponseEntity;
13import org.springframework.web.bind.annotation.*;
14import java.time.LocalDate;
15import java.time.LocalDateTime;
16import java.util.List;
17import java.util.stream.Collectors;
18
19@RestController
20@RequestMapping("/api/appointments")
21public class AppointmentController {
22 @Autowired
23 private AppointmentService appointmentService;
24
25 @GetMapping("/free")
26 public List<Appointment> getFreeAppointments(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
27 return appointmentService.findFreeAppointmentsByDate(date);
28 }
29 @GetMapping("/listAll")
30 public List<TermsResponse> getAppointmentsByUser(@RequestParam("username") String username,@RequestParam(required = true) String status) {
31 APPOINTMENT_STATUS appointmentStatus = APPOINTMENT_STATUS.valueOf(status);
32 List<Appointment>terms=appointmentService.findRequestsByUsernameAndStatus(username,appointmentStatus);
33 return terms.stream().map(term->new TermsResponse(
34 term.getRequest().getTerm(),
35 term.getRequest().getUser().getName(),
36 term.getRequest().getUser().getId(),
37 term.getRequest().getUser().getSurname(),
38 term.getRequest().getCouponCode(),
39 term.getRequest().getAdditionalInfo(),
40 term.getRequest().getUser().getUsername()
41 )).collect(Collectors.toList());
42 }
43
44 @GetMapping("/getAllAppointments")
45 public List<Appointment> getAllAppointments() {
46 return appointmentService.getAllAppointments();
47 }
48
49 @PostMapping("/add")
50 public ResponseEntity<ApiResponse> addAppointment(@RequestBody AppointmentRequest appointmentRequest) {
51 try {
52
53 String dateTimeStr = appointmentRequest.getDate() + " " + appointmentRequest.getTime();
54 appointmentService.createAppointment(dateTimeStr);
55 return ResponseEntity.ok(new ApiResponse("Appointment added successfully."));
56 } catch (Exception e) {
57 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
58 .body(new ApiResponse("Failed to add appointment."));}
59 }
60
61 @GetMapping("/listAppointments")
62 public List<AppointmentResponse>getAppointmentsByDate(@RequestParam("date") String date){
63 LocalDate temp=LocalDate.parse(date);
64 List<Appointment>appointments=appointmentService.findAppointmentsByDate(temp);
65 return appointments.stream().map(appointment -> {
66 LocalDateTime termDate = appointment.getTerm();
67 return new AppointmentResponse(termDate);
68 }).collect(Collectors.toList());
69 }
70 @GetMapping("/addAppointments")
71 public void addAppointmentStatusAndUser(@RequestParam("username") String username,@RequestParam("dateTime") String dateTime){
72 appointmentService.addAppointment(username,dateTime);
73 }
74 @GetMapping("/isReserved")
75 public ResponseEntity<Boolean> isAppointmentReserved(@RequestParam("term") String term) {
76 boolean isReserved = appointmentService.isAppointmentReserved(term);
77 return ResponseEntity.ok(isReserved);
78 }
79 @GetMapping("/listApprovedRequest")
80 public TermsResponse getAppointmentByTerm(@RequestParam("term") String term) {
81 Appointment appointment=appointmentService.findAppointmentByTerm(term);
82 TermsResponse temp=new TermsResponse(appointment.getTerm(),
83 appointment.getRequest().getUser().getName(),
84 appointment.getRequest().getUser().getId(),
85 appointment.getRequest().getUser().getSurname(),
86 appointment.getRequest().getCouponCode(),
87 appointment.getRequest().getAdditionalInfo(),
88 appointment.getRequest().getUser().getUsername());
89 return temp;
90 }
91
92 @PutMapping("/removeUserFromAppointment")
93 public ResponseEntity<String> removeUserFromAppointment(@RequestParam("term") String term) {
94 try {
95 Long userId = appointmentService.updateAppointment(term);
96 return ResponseEntity.ok(userId.toString());
97 } catch (Exception e) {
98 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error getting the username");
99 }
100 }
101
102 @GetMapping("/cancelAppointment")
103 public ResponseEntity<String>cancelAppointment(@RequestParam("username") String username,@RequestParam("term") String term){
104 try{
105 String success=appointmentService.cancelAppointmentById(username,term);
106 if(!success.equals("False")){
107 return ResponseEntity.ok("{\"success\": true, \"data\": \"" + success + "\"}");
108 }
109 else{
110 return ResponseEntity.status(400).body("{\"success\": false, \"message\": \"Failed to cancel the reservation.\"}");
111 }
112 }
113 catch(Exception e){
114 return ResponseEntity.status(500).body("{\"success\": false, \"message\": \"An error occurred: " + e.getMessage() + "\"}");
115 }
116 }
117 @DeleteMapping("/deleteAppointment")
118 public ResponseEntity<Void> deleteAppointment(@RequestParam String term) {
119 try {
120 appointmentService.deleteAppointmentByTerm(term);
121 return ResponseEntity.noContent().build();
122 } catch (IllegalArgumentException e) {
123 return ResponseEntity.notFound().build();
124 }
125 }
126 @PostMapping("/create")
127 public ResponseEntity<String> createAppointments(@RequestBody List<AppointmentRequest> appointmentRequests) {
128 try {
129 appointmentService.createAppointments(appointmentRequests);
130 return ResponseEntity.ok("Appointments created successfully");
131 } catch (Exception e) {
132 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to create appointments");
133 }
134 }
135 @DeleteMapping("/deleteFree")
136 public ResponseEntity<Void> deleteFreeAppointmentsInRange(
137 @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
138 @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
139
140 appointmentService.deleteFreeAppointmentsInRange(startDate, endDate);
141 return ResponseEntity.ok().build();
142 }
143 @GetMapping("/getNumberOfAppointments")
144 public ResponseEntity<ApiResponse>getNumberAppointments(@RequestParam("userId")String userId){
145 int total=appointmentService.getNumberOfAppointmentsByUserId(userId);
146 String temp=total+"";
147 return ResponseEntity.ok(new ApiResponse(temp));
148 }
149
150}
Note: See TracBrowser for help on using the repository browser.