Add the REST API controllers for Department and Patient.
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
package space.kklochko.spring_rest_example.controllers;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/departments")
|
||||
public class DepartmentController {
|
||||
@Autowired
|
||||
EntityManager manager;
|
||||
|
||||
@Autowired
|
||||
DepartmentRepository departmentRepository;
|
||||
|
||||
@PostMapping
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> create(@RequestBody Department department) {
|
||||
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!!!");
|
||||
|
||||
boolean successful = departmentRepository.create(department);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to create the department");
|
||||
|
||||
return ResponseEntity.status(201).body("The department created successfully");
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ResponseBody
|
||||
public Department get(@PathVariable UUID id) {
|
||||
return departmentRepository.read(id);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public List<Department> getAll() {
|
||||
return departmentRepository.readAll();
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> update(@RequestBody Department department) {
|
||||
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!!!");
|
||||
|
||||
boolean successful = departmentRepository.update(department);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to update the department.");
|
||||
|
||||
return ResponseEntity.status(200).body("The department updated successfully");
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> delete(@PathVariable UUID id) {
|
||||
boolean successful = departmentRepository.delete(id);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to delete the department, because the department does not exist!!!");
|
||||
|
||||
return ResponseEntity.status(201).body("The department deleted successfully");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package space.kklochko.spring_rest_example.controllers;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/patients")
|
||||
public class PatientController {
|
||||
@Autowired
|
||||
EntityManager manager;
|
||||
|
||||
@Autowired
|
||||
PatientRepository patientRepository;
|
||||
|
||||
@PostMapping
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> create(@RequestBody Patient patient) {
|
||||
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!!!");
|
||||
|
||||
boolean successful = patientRepository.create(patient);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to create the patient");
|
||||
|
||||
return ResponseEntity.status(201).body("The patient created successfully");
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ResponseBody
|
||||
public Patient get(@PathVariable UUID id) {
|
||||
return patientRepository.read(id);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ResponseBody
|
||||
public List<Patient> getAll() {
|
||||
return patientRepository.readAll();
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> update(@RequestBody Patient patient) {
|
||||
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!!!");
|
||||
|
||||
boolean successful = patientRepository.update(patient);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to update the patient.");
|
||||
|
||||
return ResponseEntity.status(200).body("The patient updated successfully");
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> delete(@PathVariable UUID id) {
|
||||
boolean successful = patientRepository.delete(id);
|
||||
|
||||
if(!successful)
|
||||
return ResponseEntity.status(500).body("Failed to delete the patient, because the patient does not exist!!!");
|
||||
|
||||
return ResponseEntity.status(201).body("The patient deleted successfully");
|
||||
}
|
||||
}
|
||||
+2
@@ -3,6 +3,7 @@ package space.kklochko.spring_rest_example.db.repositories;
|
||||
import jakarta.persistence.EntityGraph;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import org.springframework.stereotype.Component;
|
||||
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalInsert;
|
||||
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalRemove;
|
||||
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalUpdate;
|
||||
@@ -12,6 +13,7 @@ import space.kklochko.spring_rest_example.models.Patient;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class DepartmentRepository extends AbstractRepository<Department> {
|
||||
public DepartmentRepository(EntityManager manager){
|
||||
super(manager);
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package space.kklochko.spring_rest_example.db.repositories;
|
||||
import jakarta.persistence.EntityGraph;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import org.springframework.stereotype.Component;
|
||||
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalInsert;
|
||||
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalRemove;
|
||||
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalUpdate;
|
||||
@@ -11,6 +12,7 @@ import space.kklochko.spring_rest_example.models.Patient;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class PatientRepository extends AbstractRepository<Patient> {
|
||||
public PatientRepository(EntityManager manager) {
|
||||
super(manager);
|
||||
|
||||
Reference in New Issue
Block a user