Update the route to return 404 if an model does not exist.

main
KKlochko 1 year ago
parent 22207235d9
commit a7fc4876bf

@ -2,6 +2,7 @@ package space.kklochko.spring_rest_example.controllers;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.DepartmentRepository;
@ -37,8 +38,13 @@ public class DepartmentController {
@GetMapping("/{id}")
@ResponseBody
public Department get(@PathVariable UUID id) {
return departmentRepository.read(id);
public ResponseEntity<?> get(@PathVariable UUID id) {
Department department = departmentRepository.read(id);
if(department == null)
return new ResponseEntity<>("Department not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(department, HttpStatus.OK);
}
@GetMapping

@ -3,6 +3,7 @@ package space.kklochko.spring_rest_example.controllers;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.IndicatorRepository;
@ -40,8 +41,13 @@ public class IndicatorController {
@GetMapping("/{id}")
@ResponseBody
public Indicator get(@PathVariable UUID id) {
return indicatorRepository.read(id);
public ResponseEntity<?> get(@PathVariable UUID id) {
Indicator indicator = indicatorRepository.read(id);
if(indicator == null)
return new ResponseEntity<>("Indicator not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(indicator, HttpStatus.OK);
}
@GetMapping

@ -2,6 +2,7 @@ package space.kklochko.spring_rest_example.controllers;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.PatientRepository;
@ -37,8 +38,13 @@ public class PatientController {
@GetMapping("/{id}")
@ResponseBody
public Patient get(@PathVariable UUID id) {
return patientRepository.read(id);
public ResponseEntity<?> get(@PathVariable UUID id) {
Patient patient = patientRepository.read(id);
if(patient == null)
return new ResponseEntity<>("Patient not found", HttpStatus.NOT_FOUND);
return new ResponseEntity<>(patient, HttpStatus.OK);
}
@GetMapping

Loading…
Cancel
Save