| 1 | package com.finki.icare.controller;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.PatientDTO;
|
|---|
| 4 | import com.finki.icare.service.PatientService;
|
|---|
| 5 | import com.finki.icare.utils.AuthUtils;
|
|---|
| 6 | import org.springframework.http.ResponseEntity;
|
|---|
| 7 | import org.springframework.security.core.Authentication;
|
|---|
| 8 | import org.springframework.web.bind.annotation.*;
|
|---|
| 9 |
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 |
|
|---|
| 12 | @RestController
|
|---|
| 13 | @RequestMapping("/api/patients")
|
|---|
| 14 | @CrossOrigin(origins = "http://localhost:3000")
|
|---|
| 15 | public class PatientController {
|
|---|
| 16 |
|
|---|
| 17 | private final PatientService patientService;
|
|---|
| 18 |
|
|---|
| 19 | public PatientController(PatientService patientService) {
|
|---|
| 20 | this.patientService = patientService;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | @PutMapping("/therapist/{therapistId}")
|
|---|
| 24 | public ResponseEntity<Void> setTherapist(
|
|---|
| 25 | @PathVariable Integer therapistId,
|
|---|
| 26 | Authentication authentication) {
|
|---|
| 27 | Integer patientId = AuthUtils.getUserId(authentication);
|
|---|
| 28 | patientService.setTherapist(patientId, therapistId);
|
|---|
| 29 | return ResponseEntity.noContent().build();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | @DeleteMapping("/therapist")
|
|---|
| 33 | public ResponseEntity<Void> removeTherapist(Authentication authentication) {
|
|---|
| 34 | Integer patientId = AuthUtils.getUserId(authentication);
|
|---|
| 35 | patientService.removeTherapist(patientId);
|
|---|
| 36 | return ResponseEntity.noContent().build();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | @GetMapping("/therapist")
|
|---|
| 40 | public ResponseEntity<Integer> getCurrentTherapist(Authentication authentication) {
|
|---|
| 41 | Integer patientId = AuthUtils.getUserId(authentication);
|
|---|
| 42 | Integer therapistId = patientService.getCurrentTherapistId(patientId);
|
|---|
| 43 | if (therapistId == null) {
|
|---|
| 44 | return ResponseEntity.noContent().build();
|
|---|
| 45 | }
|
|---|
| 46 | return ResponseEntity.ok(therapistId);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | @GetMapping
|
|---|
| 50 | public ResponseEntity<List<PatientDTO>> getAllPatients() {
|
|---|
| 51 | List<PatientDTO> patients = patientService.getAllPatients();
|
|---|
| 52 | return ResponseEntity.ok(patients);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|