| 1 | package com.finki.icare.controller;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.CreateTherapyRequest;
|
|---|
| 4 | import com.finki.icare.dto.TherapyDTO;
|
|---|
| 5 | import com.finki.icare.dto.UpdateTherapyRequest;
|
|---|
| 6 | import com.finki.icare.service.TherapyService;
|
|---|
| 7 | import com.finki.icare.utils.AuthUtils;
|
|---|
| 8 | import lombok.RequiredArgsConstructor;
|
|---|
| 9 | import org.springframework.http.HttpStatus;
|
|---|
| 10 | import org.springframework.http.ResponseEntity;
|
|---|
| 11 | import org.springframework.security.core.Authentication;
|
|---|
| 12 | import org.springframework.web.bind.annotation.*;
|
|---|
| 13 |
|
|---|
| 14 | import java.util.List;
|
|---|
| 15 |
|
|---|
| 16 | @RestController
|
|---|
| 17 | @RequestMapping("/api/therapies")
|
|---|
| 18 | @CrossOrigin(origins = "http://localhost:3000")
|
|---|
| 19 | @RequiredArgsConstructor
|
|---|
| 20 | public 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 |
|
|---|