Commit 7384be84 authored by Kyle Muldoon's avatar Kyle Muldoon

made update route more efficient

parent 1ce94966
......@@ -41,15 +41,17 @@ public class EmployeeController {
}
// Update a particular employee
@PutMapping("/employees/update/{id}")
public ResponseEntity<Employee> updateEmployeeById(@PathVariable(value = "id") Long employeeId, @RequestBody Employee employeeNew) throws ResourceNotFoundException {
Employee employeeOld = employeeService.getEmployeeById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee by Id Not Found" + employeeId));
@PutMapping("/employees/update/")
public ResponseEntity<Employee> updateEmployeeById(@RequestBody Employee employeeNew) throws ResourceNotFoundException {
Employee employee = employeeService.getEmployeeById(employeeNew.getId())
.orElseThrow(() -> new ResourceNotFoundException("Employee by Id Not Found" + employeeNew.getId()));
employee.setLastName(employeeNew.getLastName());
employee.setFirstName(employeeNew.getFirstName());
employeeService.deleteEmployeeById(employeeOld.getId());
employeeService.addEmployee(employeeNew);
employeeService.updateEmployee(employee);
return ResponseEntity.ok().body(employeeNew);
return ResponseEntity.ok().body(employee);
}
......
......@@ -15,7 +15,6 @@ public class EmployeeService {
public List<Employee> findAllEmployees() {
/*some business*/
return employeeRepository.findAll();
}
......@@ -23,17 +22,15 @@ public class EmployeeService {
return employeeRepository.save(employee);
}
// public void updateEmployee(Employee employee) {
// employeeRepository.
//
// }
public Optional<Employee> getEmployeeById(Long employeeId) {
return employeeRepository.findById(employeeId);
}
public void deleteEmployeeById(Long employeeId) {
employeeRepository.deleteById(employeeId);
}
public void updateEmployee(Employee employee) {
employeeRepository.save(employee);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment