| 1 | package com.finki.icare.controller;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.ConsultationDTO;
|
|---|
| 4 | import com.finki.icare.dto.CreateConsultationRequest;
|
|---|
| 5 | import com.finki.icare.dto.UpdateConsultationRequest;
|
|---|
| 6 | import com.finki.icare.service.ConsultationService;
|
|---|
| 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/consultations")
|
|---|
| 18 | @CrossOrigin(origins = "http://localhost:3000")
|
|---|
| 19 | @RequiredArgsConstructor
|
|---|
| 20 | public class ConsultationController {
|
|---|
| 21 |
|
|---|
| 22 | private final ConsultationService consultationService;
|
|---|
| 23 |
|
|---|
| 24 | @GetMapping("/therapist/{therapistId}")
|
|---|
| 25 | public ResponseEntity<List<ConsultationDTO>> getTherapistConsultations(
|
|---|
| 26 | @PathVariable Integer therapistId,
|
|---|
| 27 | Authentication authentication
|
|---|
| 28 | ) {
|
|---|
| 29 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 30 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 31 |
|
|---|
| 32 | List<ConsultationDTO> consultations = consultationService.getTherapistConsultations(
|
|---|
| 33 | therapistId, currentUserId, userType
|
|---|
| 34 | );
|
|---|
| 35 | return ResponseEntity.ok(consultations);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | @GetMapping("/patient/{patientId}")
|
|---|
| 39 | public ResponseEntity<List<ConsultationDTO>> getPatientConsultations(
|
|---|
| 40 | @PathVariable Integer patientId,
|
|---|
| 41 | Authentication authentication
|
|---|
| 42 | ) {
|
|---|
| 43 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 44 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 45 |
|
|---|
| 46 | List<ConsultationDTO> consultations = consultationService.getPatientConsultations(
|
|---|
| 47 | patientId, currentUserId, userType
|
|---|
| 48 | );
|
|---|
| 49 | return ResponseEntity.ok(consultations);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @PostMapping
|
|---|
| 53 | public ResponseEntity<ConsultationDTO> createConsultation(
|
|---|
| 54 | @RequestBody CreateConsultationRequest request,
|
|---|
| 55 | Authentication authentication
|
|---|
| 56 | ) {
|
|---|
| 57 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 58 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 59 |
|
|---|
| 60 | ConsultationDTO consultation = consultationService.createConsultation(
|
|---|
| 61 | request, currentUserId, userType
|
|---|
| 62 | );
|
|---|
| 63 | return ResponseEntity.status(HttpStatus.CREATED).body(consultation);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @PutMapping("/{consultationId}")
|
|---|
| 67 | public ResponseEntity<ConsultationDTO> updateConsultation(
|
|---|
| 68 | @PathVariable Integer consultationId,
|
|---|
| 69 | @RequestBody UpdateConsultationRequest request,
|
|---|
| 70 | Authentication authentication
|
|---|
| 71 | ) {
|
|---|
| 72 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 73 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 74 |
|
|---|
| 75 | ConsultationDTO consultation = consultationService.updateConsultation(
|
|---|
| 76 | consultationId, request, currentUserId, userType
|
|---|
| 77 | );
|
|---|
| 78 | return ResponseEntity.ok(consultation);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | @DeleteMapping("/{consultationId}")
|
|---|
| 82 | public ResponseEntity<Void> deleteConsultation(
|
|---|
| 83 | @PathVariable Integer consultationId,
|
|---|
| 84 | Authentication authentication
|
|---|
| 85 | ) {
|
|---|
| 86 | Integer currentUserId = AuthUtils.getUserId(authentication);
|
|---|
| 87 | String userType = AuthUtils.getUserType(authentication);
|
|---|
| 88 |
|
|---|
| 89 | consultationService.deleteConsultation(consultationId, currentUserId, userType);
|
|---|
| 90 | return ResponseEntity.noContent().build();
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|