- Timestamp:
- 05/23/26 19:27:12 (4 months ago)
- Branches:
- master
- Children:
- ccb5a6b
- Parents:
- 43e476a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/src/main/java/medora/controller/PatientController.java
r43e476a r946877f 1 1 package medora.controller; 2 2 3 import medora.dto.PatientDTO; 3 4 import medora.dto.CreatePatientRequest; 4 import medora.dto.PatientDTO;5 5 import medora.models.domain.Patient; 6 6 import medora.service.PatientService; 7 import medora.util.SecurityUtil; 7 8 import org.slf4j.Logger; 8 9 import org.slf4j.LoggerFactory; … … 10 11 import org.springframework.http.ResponseEntity; 11 12 import org.springframework.web.bind.annotation.*; 13 import jakarta.servlet.http.HttpServletRequest; 12 14 13 15 import java.util.List; … … 22 24 23 25 private final PatientService patientService; 24 25 public PatientController(PatientService patientService) { 26 private final SecurityUtil securityUtil; 27 28 public PatientController(PatientService patientService, SecurityUtil securityUtil) { 26 29 this.patientService = patientService; 30 this.securityUtil = securityUtil; 27 31 } 28 32 29 33 @PostMapping 30 public ResponseEntity<?> createPatient(@RequestBody CreatePatientRequest request) { 31 try { 34 public ResponseEntity<?> createPatient(@RequestBody CreatePatientRequest request, HttpServletRequest httpRequest) { 35 try { 36 String role = securityUtil.getRoleFromRequest(httpRequest); 37 if (role == null) { 38 return ResponseEntity.status(HttpStatus.UNAUTHORIZED) 39 .body(Map.of("error", "Unauthorized")); 40 } 41 42 // Only ADMIN can create patients 43 if (!role.equals("ADMIN")) { 44 return ResponseEntity.status(HttpStatus.FORBIDDEN) 45 .body(Map.of("error", "Only administrators can create patients")); 46 } 32 47 if (request.getFirstName() == null || request.getFirstName().isBlank()) { 33 48 return ResponseEntity.badRequest() … … 144 159 @PutMapping("/{patientId}") 145 160 public ResponseEntity<?> updatePatient(@PathVariable Long patientId, 146 @RequestBody CreatePatientRequest request) { 147 try { 161 @RequestBody CreatePatientRequest request, 162 HttpServletRequest httpRequest) { 163 try { 164 String role = securityUtil.getRoleFromRequest(httpRequest); 165 if (role == null) { 166 return ResponseEntity.status(HttpStatus.UNAUTHORIZED) 167 .body(Map.of("error", "Unauthorized")); 168 } 169 170 // Only ADMIN can update patients 171 if (!role.equals("ADMIN")) { 172 return ResponseEntity.status(HttpStatus.FORBIDDEN) 173 .body(Map.of("error", "Only administrators can update patients")); 174 } 175 148 176 logger.info("Updating patient with ID: {}", patientId); 149 177
Note:
See TracChangeset
for help on using the changeset viewer.
