source: src/main/java/finki/it/terapijamkbackend/spring/controllers/RequestController.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: 4.7 KB
Line 
1package finki.it.terapijamkbackend.spring.controllers;
2
3import finki.it.terapijamkbackend.spring.dto.IdsResponse;
4import finki.it.terapijamkbackend.spring.dto.TermsResponse;
5import finki.it.terapijamkbackend.spring.entities.Request;
6import finki.it.terapijamkbackend.spring.services.RequestService;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.http.HttpStatus;
9import org.springframework.http.ResponseEntity;
10import org.springframework.web.bind.annotation.*;
11import java.time.LocalDateTime;
12import java.time.format.DateTimeFormatter;
13import java.util.List;
14import java.util.Map;
15import java.util.stream.Collectors;
16
17@RestController
18@RequestMapping("/api/requests")
19public class RequestController {
20 @Autowired
21 private RequestService requestService;
22
23 @PostMapping("/book")
24 public ResponseEntity<String> bookAppointment(@RequestBody Map<String, String> request, @CookieValue(value = "username", defaultValue = "") String username) {
25 String term = request.get("term");
26 String couponCode = request.get("couponCode");
27 String medicalCondition = request.get("medicalCondition");
28
29 try {
30 requestService.bookAppointment(term, couponCode, medicalCondition,username);
31 return ResponseEntity.ok("Appointment booked successfully.");
32 } catch (RuntimeException e) {
33 return ResponseEntity.badRequest().body(e.getMessage());
34 }
35 }
36
37 @GetMapping("/listAll")
38 public List<TermsResponse> getRequestsByUser(@RequestParam("username") String username) {
39 List<Request>terms=requestService.findRequestsByUsername(username);
40 return terms.stream()
41 .map(term -> new TermsResponse(
42 term.getTerm(),
43 term.getUser().getName(),
44 term.getUser().getId(),
45 term.getUser().getSurname(),
46 term.getCouponCode(),
47 term.getAdditionalInfo(),
48 term.getUser().getUsername()
49 ))
50 .collect(Collectors.toList());
51 }
52 @GetMapping("/listRequests")
53 public List<TermsResponse> getRequestsByTerm(@RequestParam("term") String term) {
54 List<Request>terms=requestService.findRequestsByTerm(term);
55 return terms.stream()
56 .map(item -> new TermsResponse(
57 item.getTerm(),
58 item.getUser().getName(),
59 item.getUser().getId(),
60 item.getUser().getSurname(),
61 item.getCouponCode(),
62 item.getAdditionalInfo(),
63 item.getUser().getUsername()
64 ))
65 .collect(Collectors.toList());
66 }
67
68
69 @GetMapping("/cancelReservation")
70 public ResponseEntity<String>cancelReservation(@RequestParam("username") String username,@RequestParam("term") String term){
71 try{
72 String success=requestService.cancelReservationById(username,term);
73 if(!success.equals("False")){
74 return ResponseEntity.ok("{\"success\": true, \"data\": \"" + success + "\"}");
75 }
76 else{
77 return ResponseEntity.status(400).body("{\"success\": false, \"message\": \"Failed to cancel the reservation.\"}");
78 }
79 }
80 catch(Exception e){
81 return ResponseEntity.status(500).body("{\"success\": false, \"message\": \"An error occurred: " + e.getMessage() + "\"}");
82 }
83 }
84
85 @GetMapping("/isEmpty")
86 public ResponseEntity<Boolean> isAppointmentEmpty(@RequestParam("term") String term) {
87 boolean isEmpty = requestService.isEmpty(term);
88 return ResponseEntity.ok(isEmpty);
89 }
90 @GetMapping("/removeRequest")
91 public ResponseEntity<String> cleanRequest(@RequestParam("term") String term,@RequestParam("userId") String userId) {
92 try {
93 DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
94 LocalDateTime parsedTerm = LocalDateTime.parse(term, formatter);
95 String additionalInfo = requestService.findAdditionalInfoBasedOnUsernameAndTerm(userId, parsedTerm);
96 return ResponseEntity.ok(additionalInfo);
97 } catch (Exception e) {
98 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing request: " + e.getMessage());
99 }
100 }
101
102 @GetMapping("/users-by-term")
103 public IdsResponse getIdsByTermExcept(@RequestParam("term") String term, @RequestParam("excludedUsername") String excludedUsername) {
104 List<Long> ids = requestService.getIdsByTermExcept(term, excludedUsername);
105 return new IdsResponse(ids);
106 }
107
108}
Note: See TracBrowser for help on using the repository browser.