From 474bb9daaf1ea54b2b6212db86f18ac1b6395065 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 3 Dec 2023 11:53:00 +0200 Subject: [PATCH] Update the controllers to return messages or errors as JSONs. --- .../controllers/AuthController.java | 14 +++++++------ .../controllers/DepartmentController.java | 21 +++++++++++-------- .../controllers/IndicatorController.java | 21 +++++++++++-------- .../controllers/PatientController.java | 21 +++++++++++-------- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/main/java/space/kklochko/spring_rest_example/controllers/AuthController.java b/src/main/java/space/kklochko/spring_rest_example/controllers/AuthController.java index d92ffda..64a1beb 100644 --- a/src/main/java/space/kklochko/spring_rest_example/controllers/AuthController.java +++ b/src/main/java/space/kklochko/spring_rest_example/controllers/AuthController.java @@ -12,6 +12,8 @@ import space.kklochko.spring_rest_example.db.repositories.UserRepository; import space.kklochko.spring_rest_example.models.Token; import space.kklochko.spring_rest_example.models.User; import space.kklochko.spring_rest_example.models.factories.TokenFactory; +import space.kklochko.spring_rest_example.models.rest.RestError; +import space.kklochko.spring_rest_example.models.rest.RestMessage; import space.kklochko.spring_rest_example.security.hashes.Sha256Hash; @RestController @@ -27,28 +29,28 @@ public class AuthController { private TokenFactory tokenFactory; @PostMapping("/login") - public ResponseEntity authenticateUser(@RequestBody User user){ + public ResponseEntity authenticateUser(@RequestBody User user){ if(user.getUsername() == null || user.getUsername().isEmpty()) - return ResponseEntity.status(500).body("Failed to login, because no username!!!"); + return new ResponseEntity<>(new RestError("Failed to login, because no username!!!"), HttpStatus.BAD_REQUEST); if(user.getPassword() == null || user.getPassword().isEmpty()) - return ResponseEntity.status(500).body("Failed to login, because no password!!!"); + return new ResponseEntity<>(new RestError("Failed to login, because no password!!!"), HttpStatus.BAD_REQUEST); User expected = userRepository.read(user.getUsername()); if(expected == null) - return ResponseEntity.status(500).body("Failed to login, because the user does not exist!!! Please, check your username!!!"); + return new ResponseEntity<>(new RestError("Failed to login, because the user does not exist!!! Please, check your username!!!"), HttpStatus.NOT_FOUND); String hashedPassword = (new Sha256Hash()).hashOf(user.getPassword()); if(!expected.getPassword().equals(hashedPassword)) - return ResponseEntity.status(500).body("Failed to login, because incorrect password!!! Please, check your password!!!"); + return new ResponseEntity<>(new RestError("Failed to login, because incorrect password!!! Please, check your password!!!"), HttpStatus.UNAUTHORIZED); Token token = tokenFactory.create(expected.getUsername(), expected.getRole()); tokenRepository.create(token); - return new ResponseEntity<>(String.format("token: %s", token.getToken()), HttpStatus.OK); + return new ResponseEntity<>(new RestMessage(String.format("token: %s", token.getToken())), HttpStatus.OK); } } diff --git a/src/main/java/space/kklochko/spring_rest_example/controllers/DepartmentController.java b/src/main/java/space/kklochko/spring_rest_example/controllers/DepartmentController.java index b4c2b70..ba3b13a 100644 --- a/src/main/java/space/kklochko/spring_rest_example/controllers/DepartmentController.java +++ b/src/main/java/space/kklochko/spring_rest_example/controllers/DepartmentController.java @@ -7,6 +7,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import space.kklochko.spring_rest_example.db.repositories.DepartmentRepository; import space.kklochko.spring_rest_example.models.Department; +import space.kklochko.spring_rest_example.models.rest.RestError; +import space.kklochko.spring_rest_example.models.rest.RestMessage; import java.util.List; import java.util.UUID; @@ -26,14 +28,14 @@ public class DepartmentController { Department isExist = departmentRepository.read(department.getId()); if(isExist != null) - return ResponseEntity.status(500).body("Failed to create the department, because the id is already used!!!"); + return new ResponseEntity<>(new RestError("Failed to create the department, because the id is already used!!!"), HttpStatus.CONFLICT); boolean successful = departmentRepository.create(department); if(!successful) - return ResponseEntity.status(500).body("Failed to create the department"); + return new ResponseEntity<>(new RestError("Failed to create the department."), HttpStatus.INTERNAL_SERVER_ERROR); - return ResponseEntity.status(201).body("The department created successfully"); + return new ResponseEntity<>(new RestMessage("The department created successfully."), HttpStatus.CREATED); } @GetMapping("/{id}") @@ -42,7 +44,7 @@ public class DepartmentController { Department department = departmentRepository.read(id); if(department == null) - return new ResponseEntity<>("Department not found", HttpStatus.NOT_FOUND); + return new ResponseEntity<>(new RestError("The department not found."), HttpStatus.NOT_FOUND); return new ResponseEntity<>(department, HttpStatus.OK); } @@ -59,14 +61,14 @@ public class DepartmentController { Department isExist = departmentRepository.read(department.getId()); if(isExist == null) - return ResponseEntity.status(500).body("Failed to update the department, because the department does not exist!!!"); + return new ResponseEntity<>(new RestError("Failed to update the department, because the department does not exist!!!"), HttpStatus.NOT_FOUND); boolean successful = departmentRepository.update(department); if(!successful) - return ResponseEntity.status(500).body("Failed to update the department."); + return new ResponseEntity<>(new RestError("Failed to update the department."), HttpStatus.INTERNAL_SERVER_ERROR); - return ResponseEntity.status(200).body("The department updated successfully"); + return new ResponseEntity<>(new RestMessage("The department updated successfully."), HttpStatus.OK); } @DeleteMapping("/{id}") @@ -75,8 +77,9 @@ public class DepartmentController { boolean successful = departmentRepository.delete(id); if(!successful) - return ResponseEntity.status(500).body("Failed to delete the department, because the department does not exist!!!"); + return new ResponseEntity<>(new RestError("Failed to delete the department, because the department does not exist!!!"), HttpStatus.NOT_FOUND); - return ResponseEntity.status(201).body("The department deleted successfully"); + return new ResponseEntity<>(new RestMessage("The department deleted successfully."), HttpStatus.OK); } } + diff --git a/src/main/java/space/kklochko/spring_rest_example/controllers/IndicatorController.java b/src/main/java/space/kklochko/spring_rest_example/controllers/IndicatorController.java index 447ccae..a64a946 100644 --- a/src/main/java/space/kklochko/spring_rest_example/controllers/IndicatorController.java +++ b/src/main/java/space/kklochko/spring_rest_example/controllers/IndicatorController.java @@ -7,6 +7,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import space.kklochko.spring_rest_example.db.repositories.IndicatorRepository; import space.kklochko.spring_rest_example.models.Indicator; +import space.kklochko.spring_rest_example.models.rest.RestError; +import space.kklochko.spring_rest_example.models.rest.RestMessage; import java.util.List; import java.util.UUID; @@ -26,14 +28,14 @@ public class IndicatorController { Indicator isExist = indicatorRepository.read(indicator.getId()); if(isExist != null) - return ResponseEntity.status(500).body("Failed to create the indicator, because the id is already used!!!"); + return new ResponseEntity<>(new RestError("Failed to create the indicator, because the id is already used!!!"), HttpStatus.CONFLICT); boolean successful = indicatorRepository.create(indicator); if(!successful) - return ResponseEntity.status(500).body("Failed to create the indicator"); + return new ResponseEntity<>(new RestError("Failed to create the indicator."), HttpStatus.INTERNAL_SERVER_ERROR); - return ResponseEntity.status(201).body("The indicator created successfully"); + return new ResponseEntity<>(new RestMessage("The indicator created successfully."), HttpStatus.CREATED); } @GetMapping("/{id}") @@ -42,7 +44,7 @@ public class IndicatorController { Indicator indicator = indicatorRepository.read(id); if(indicator == null) - return new ResponseEntity<>("Indicator not found", HttpStatus.NOT_FOUND); + return new ResponseEntity<>(new RestError("The indicator not found."), HttpStatus.NOT_FOUND); return new ResponseEntity<>(indicator, HttpStatus.OK); } @@ -59,14 +61,14 @@ public class IndicatorController { Indicator isExist = indicatorRepository.read(indicator.getId()); if(isExist == null) - return ResponseEntity.status(500).body("Failed to update the indicator, because the indicator does not exist!!!"); + return new ResponseEntity<>(new RestError("Failed to update the indicator, because the indicator does not exist!!!"), HttpStatus.NOT_FOUND); boolean successful = indicatorRepository.update(indicator); if(!successful) - return ResponseEntity.status(500).body("Failed to update the indicator."); + return new ResponseEntity<>(new RestError("Failed to update the indicator."), HttpStatus.INTERNAL_SERVER_ERROR); - return ResponseEntity.status(200).body("The indicator updated successfully"); + return new ResponseEntity<>(new RestMessage("The indicator updated successfully."), HttpStatus.OK); } @DeleteMapping("/{id}") @@ -75,8 +77,9 @@ public class IndicatorController { boolean successful = indicatorRepository.delete(id); if(!successful) - return ResponseEntity.status(500).body("Failed to delete the indicator, because the indicator does not exist!!!"); + return new ResponseEntity<>(new RestError("Failed to delete the indicator, because the indicator does not exist!!!"), HttpStatus.NOT_FOUND); - return ResponseEntity.status(201).body("The indicator deleted successfully"); + return new ResponseEntity<>(new RestMessage("The indicator deleted successfully."), HttpStatus.OK); } } + diff --git a/src/main/java/space/kklochko/spring_rest_example/controllers/PatientController.java b/src/main/java/space/kklochko/spring_rest_example/controllers/PatientController.java index 6bdfd8c..d9356a1 100644 --- a/src/main/java/space/kklochko/spring_rest_example/controllers/PatientController.java +++ b/src/main/java/space/kklochko/spring_rest_example/controllers/PatientController.java @@ -7,6 +7,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import space.kklochko.spring_rest_example.db.repositories.PatientRepository; import space.kklochko.spring_rest_example.models.Patient; +import space.kklochko.spring_rest_example.models.rest.RestError; +import space.kklochko.spring_rest_example.models.rest.RestMessage; import java.util.List; import java.util.UUID; @@ -26,14 +28,14 @@ public class PatientController { Patient isExist = patientRepository.read(patient.getId()); if(isExist != null) - return ResponseEntity.status(500).body("Failed to create the patient, because the id is already used!!!"); + return new ResponseEntity<>(new RestError("Failed to create the patient, because the id is already used!!!"), HttpStatus.CONFLICT); boolean successful = patientRepository.create(patient); if(!successful) - return ResponseEntity.status(500).body("Failed to create the patient"); + return new ResponseEntity<>(new RestError("Failed to create the patient."), HttpStatus.INTERNAL_SERVER_ERROR); - return ResponseEntity.status(201).body("The patient created successfully"); + return new ResponseEntity<>(new RestMessage("The patient created successfully."), HttpStatus.CREATED); } @GetMapping("/{id}") @@ -42,7 +44,7 @@ public class PatientController { Patient patient = patientRepository.read(id); if(patient == null) - return new ResponseEntity<>("Patient not found", HttpStatus.NOT_FOUND); + return new ResponseEntity<>(new RestError("The patient not found."), HttpStatus.NOT_FOUND); return new ResponseEntity<>(patient, HttpStatus.OK); } @@ -59,14 +61,14 @@ public class PatientController { Patient isExist = patientRepository.read(patient.getId()); if(isExist == null) - return ResponseEntity.status(500).body("Failed to update the patient, because the patient does not exist!!!"); + return new ResponseEntity<>(new RestError("Failed to update the patient, because the patient does not exist!!!"), HttpStatus.NOT_FOUND); boolean successful = patientRepository.update(patient); if(!successful) - return ResponseEntity.status(500).body("Failed to update the patient."); + return new ResponseEntity<>(new RestError("Failed to update the patient."), HttpStatus.INTERNAL_SERVER_ERROR); - return ResponseEntity.status(200).body("The patient updated successfully"); + return new ResponseEntity<>(new RestMessage("The patient updated successfully."), HttpStatus.OK); } @DeleteMapping("/{id}") @@ -75,8 +77,9 @@ public class PatientController { boolean successful = patientRepository.delete(id); if(!successful) - return ResponseEntity.status(500).body("Failed to delete the patient, because the patient does not exist!!!"); + return new ResponseEntity<>(new RestError("Failed to delete the patient, because the patient does not exist!!!"), HttpStatus.NOT_FOUND); - return ResponseEntity.status(201).body("The patient deleted successfully"); + return new ResponseEntity<>(new RestMessage("The patient deleted successfully."), HttpStatus.OK); } } +