|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|