Search in sources :

Example 1 with Manager

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

the class Bootstrap method initEmployee.

/**
 * Initializing employee record in database
 */
private void initEmployee() {
    long countEmployee = employeeRepository.count();
    if (countEmployee > 0) {
        log.info(countEmployee + "Employee already exists");
        return;
    }
    List<Employee> employee = new ArrayList<Employee>();
    Manager manager = null;
    employee.add(buildEmployee("nobody", "nobody@gmail.com", "none", "0", manager));
    employeeRepository.save(employee);
    log.info("Some employee has been created");
}
Also used : Employee(rest.spring.entity.Employee) ArrayList(java.util.ArrayList) Manager(rest.spring.entity.Manager)

Example 2 with Manager

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

the class Bootstrap method initManagerEmployee.

/**
 * Create employee with manager
 */
private void initManagerEmployee() {
    // employeeRepository.deleteAllInBatch();
    // managerRepository.deleteAllInBatch();
    long countEmployee = employeeRepository.count();
    long countManager = managerRepository.count();
    if (countManager > 0 || countEmployee > 0) {
        log.info(countManager + " managers and " + countEmployee + " employee already exists");
        return;
    }
    Manager manager = this.buildManager("Umme Habiba", "habiba@gmail.com", "development", "50000");
    Employee employee1 = this.buildEmployee("Hafiz ahmed", "hafiz@gmail.com", "Developer", "10000", manager);
    Employee employee2 = this.buildEmployee("Shaikh hafiz", "shaikh@gmail.com", "Developer", "10000", manager);
    employee1.setManager(manager);
    employee2.setManager(manager);
    List<Employee> employees = new ArrayList<Employee>();
    employees.add(employee1);
    employees.add(employee2);
    manager.setEmployees(employees);
    managerRepository.save(manager);
}
Also used : Employee(rest.spring.entity.Employee) ArrayList(java.util.ArrayList) Manager(rest.spring.entity.Manager)

Example 3 with Manager

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

the class Bootstrap method buildManager.

/**
 * Creating a manager object, setting value through setter methods and return the object
 * @param name
 * @param email
 * @param department
 * @param salary
 * @return
 */
private Manager buildManager(String name, String email, String department, String salary) {
    Manager manager = new Manager();
    manager.setName(name);
    manager.setEmail(email);
    manager.setDepartment(department);
    manager.setSalary(salary);
    return manager;
}
Also used : Manager(rest.spring.entity.Manager)

Example 4 with Manager

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

the class EmployeeController method createEmployee.

/**
 * Save a new employee
 * @param employee
 * @return
 */
@PostMapping("employees/create")
public ResponseEntity<Employee> createEmployee(@PathVariable("managerId") Long managerId, @Valid @RequestBody Employee employee) {
    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.setManager(manager);
    List<Employee> employees = new ArrayList<Employee>();
    employees.add(employee);
    manager.setEmployees(employees);
    /*
		 * To do check manager have at least one employee ohterwise manager.getEmployees().add(employee) will return 
		 * null pointer exception
		 */
    // manager.getEmployees().add(employee);
    Manager managerTemp = null;
    managerTemp = managerService.save(manager);
    if (managerTemp == null) {
        return new ResponseEntity("Employee cant be created", HttpStatus.FAILED_DEPENDENCY);
    }
    return new ResponseEntity(employee, HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Employee(rest.spring.entity.Employee) ArrayList(java.util.ArrayList) Manager(rest.spring.entity.Manager) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with Manager

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

the class EmployeeController method deleteEmployee.

/**
 * Delete an employee by id
 * @param employeeId
 * @return
 */
@DeleteMapping("employees/{id}")
public ResponseEntity<Employee> deleteEmployee(@PathVariable("managerId") Long managerId, @PathVariable(value = "id") Long employeeId) {
    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 = employeeService.findById(employeeId);
    if (employee == null) {
        return ResponseEntity.notFound().build();
    }
    employeeService.delete(employee);
    return ResponseEntity.ok().build();
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Employee(rest.spring.entity.Employee) 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