Update the controllers to return messages or errors as JSONs.
This commit is contained in:
@@ -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<String> authenticateUser(@RequestBody User user){
|
||||
public ResponseEntity<Object> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-9
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-9
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-9
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user