Search in sources :

Example 6 with Manager

use of rest.spring.entity.Manager in project spring-boot-rest-api by shaikhhafiz.

the class ManagerController method updateManager.

/**
 * Update an manager by id
 * @param managerId
 * @param managerDetails
 * @return
 */
@PutMapping("/manager/{id}")
public ResponseEntity<Manager> updateManager(@PathVariable(value = "id") Long managerId, @Valid @RequestBody Manager managerDetails) {
    Manager manager = managerService.findById(managerId);
    if (manager == null) {
        return ResponseEntity.notFound().build();
    }
    managerDetails.setId(managerId);
    Manager updatedManager = managerService.update(managerDetails);
    return ResponseEntity.ok(updatedManager);
}
Also used : Manager(rest.spring.entity.Manager) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 7 with Manager

use of rest.spring.entity.Manager 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 8 with Manager

use of rest.spring.entity.Manager 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)

Example 9 with Manager

use of rest.spring.entity.Manager in project spring-boot-rest-api by shaikhhafiz.

the class ManagerController method deleteManager.

/**
 * Delete a manager by id
 * @param managerId
 * @return
 */
@DeleteMapping("/manager/{id}")
public ResponseEntity<Manager> deleteManager(@PathVariable(value = "id") Long managerId) {
    Manager manager = managerService.findById(managerId);
    if (manager == null) {
        return ResponseEntity.notFound().build();
    }
    managerService.delete(manager);
    return ResponseEntity.ok().build();
}
Also used : Manager(rest.spring.entity.Manager) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Aggregations

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