source: backend/src/main/java/com/finki/icare/controller/TherapyController.java

main
Last change on this file was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[700e2f9]1package com.finki.icare.controller;
2
3import com.finki.icare.dto.CreateTherapyRequest;
4import com.finki.icare.dto.TherapyDTO;
5import com.finki.icare.dto.UpdateTherapyRequest;
6import com.finki.icare.service.TherapyService;
7import com.finki.icare.utils.AuthUtils;
8import lombok.RequiredArgsConstructor;
9import org.springframework.http.HttpStatus;
10import org.springframework.http.ResponseEntity;
11import org.springframework.security.core.Authentication;
12import org.springframework.web.bind.annotation.*;
13
14import java.util.List;
15
16@RestController
17@RequestMapping("/api/therapies")
18@CrossOrigin(origins = "http://localhost:3000")
19@RequiredArgsConstructor
20public class TherapyController {
21
22 private final TherapyService therapyService;
23
24 @GetMapping("/consultation/{consultationId}")
25 public ResponseEntity<List<TherapyDTO>> getTherapiesByConsultation(
26 @PathVariable Integer consultationId,
27 Authentication authentication
28 ) {
29 Integer currentUserId = AuthUtils.getUserId(authentication);
30 String userType = AuthUtils.getUserType(authentication);
31
32 List<TherapyDTO> therapies = therapyService.getTherapiesByConsultation(
33 consultationId, currentUserId, userType
34 );
35 return ResponseEntity.ok(therapies);
36 }
37
38 @PostMapping("/consultation/{consultationId}")
39 public ResponseEntity<TherapyDTO> createTherapy(
40 @PathVariable Integer consultationId,
41 @RequestBody CreateTherapyRequest request,
42 Authentication authentication
43 ) {
44 Integer currentUserId = AuthUtils.getUserId(authentication);
45 String userType = AuthUtils.getUserType(authentication);
46
47 TherapyDTO therapy = therapyService.createTherapy(
48 consultationId, request, currentUserId, userType
49 );
50 return ResponseEntity.status(HttpStatus.CREATED).body(therapy);
51 }
52
53 @PutMapping("/{therapyId}")
54 public ResponseEntity<TherapyDTO> updateTherapy(
55 @PathVariable Integer therapyId,
56 @RequestBody UpdateTherapyRequest request,
57 Authentication authentication
58 ) {
59 Integer currentUserId = AuthUtils.getUserId(authentication);
60 String userType = AuthUtils.getUserType(authentication);
61
62 TherapyDTO therapy = therapyService.updateTherapy(
63 therapyId, request, currentUserId, userType
64 );
65 return ResponseEntity.ok(therapy);
66 }
67
68 @DeleteMapping("/{therapyId}")
69 public ResponseEntity<Void> deleteTherapy(
70 @PathVariable Integer therapyId,
71 Authentication authentication
72 ) {
73 Integer currentUserId = AuthUtils.getUserId(authentication);
74 String userType = AuthUtils.getUserType(authentication);
75
76 therapyService.deleteTherapy(therapyId, currentUserId, userType);
77 return ResponseEntity.noContent().build();
78 }
79}
80
Note: See TracBrowser for help on using the repository browser.