Compare commits
No commits in common. 'a7fc4876bf64e538d52d0462439de86f146146d1' and '5aacd66fda7e55985f0d4caab7919b958cd6b304' have entirely different histories.
a7fc4876bf
...
5aacd66fda
@ -1,82 +0,0 @@
|
||||
package space.kklochko.spring_rest_example.controllers;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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 ResponseEntity<?> get(@PathVariable UUID 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
|
||||
@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");
|
||||
}
|
||||
}
|
||||
@ -1,82 +0,0 @@
|
||||
package space.kklochko.spring_rest_example.controllers;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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 ResponseEntity<?> get(@PathVariable UUID 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
|
||||
@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");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue