use of rest.spring.entity.Employee in project spring-boot-rest-api by shaikhhafiz.
the class EmployeeController method updateEmployee.
/**
* Update an employee by id
* @param employeeId
* @param employeeDetails
* @return
*/
@PutMapping("employees/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable("managerId") Long managerId, @PathVariable(value = "id") Long employeeId, @Valid @RequestBody Employee employeeDetails) {
Manager manager = null;
manager = managerService.findById(managerId);
if (manager == null) {
return new ResponseEntity("Manager with manager id " + managerId + "is not exist", HttpStatus.NO_CONTENT);
}
Employee employee = null;
employee = employeeService.findById(employeeId);
if (employee == null) {
return new ResponseEntity("Employee with employee id " + employeeId + "is not exist", HttpStatus.NO_CONTENT);
}
employee.setName(employeeDetails.getName());
employee.setEmail(employeeDetails.getEmail());
employee.setDesignation(employeeDetails.getDesignation());
employee.setSalary(employeeDetails.getSalary());
employee.setManager(manager);
Employee updatedEmployee = employeeService.update(employee);
return new ResponseEntity(updatedEmployee, HttpStatus.OK);
}
use of rest.spring.entity.Employee in project spring-boot-rest-api by shaikhhafiz.
the class EmployeeController method employeeList.
/**
* Get list of all employee
* @return
*/
@GetMapping("/employees")
public ResponseEntity<List<Employee>> employeeList(@PathVariable("managerId") Long managerId) {
Manager manager = null;
List<Employee> employees = null;
try {
manager = managerService.findById(managerId);
if (manager == null) {
return new ResponseEntity("Manager with manager id " + managerId + " does not exist", HttpStatus.NO_CONTENT);
} else if (manager != null) {
employees = employeeService.findAllByManager(manager);
if (employees.isEmpty()) {
return new ResponseEntity("There have no employees with manager id " + managerId, HttpStatus.NO_CONTENT);
}
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
return new ResponseEntity<List<Employee>>(employees, HttpStatus.OK);
}
Aggregations