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 jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import space.kklochko.spring_rest_example.db.repositories.DepartmentRepository; import space.kklochko.spring_rest_example.db.repositories.DepartmentRepository;
@ -37,8 +38,13 @@ public class DepartmentController {
@GetMapping("/{id}") @GetMapping("/{id}")
@ResponseBody @ResponseBody
public Department get(@PathVariable UUID id) { public ResponseEntity<?> get(@PathVariable UUID id) {
return departmentRepository.read(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 @GetMapping

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

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

Loading…
Cancel
Save