Search in sources :

Example 6 with Employee

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Employee(rest.spring.entity.Employee) Manager(rest.spring.entity.Manager) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 7 with Employee

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Employee(rest.spring.entity.Employee) Manager(rest.spring.entity.Manager) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Employee (rest.spring.entity.Employee)7 Manager (rest.spring.entity.Manager)6 ResponseEntity (org.springframework.http.ResponseEntity)4 ArrayList (java.util.ArrayList)3 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1