Search in sources :

Example 1 with Employee

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

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

the class Bootstrap method buildEmployee.

/**
 * Creating a employee object setting it's value thorough setter method and return the object
 * @param name
 * @param email
 * @param designation
 * @param salary
 * @return
 */
private Employee buildEmployee(String name, String email, String designation, String salary, Manager manager) {
    Employee employee = new Employee();
    employee.setName(name);
    employee.setEmail(email);
    employee.setDesignation(designation);
    employee.setSalary(salary);
    employee.setManager(manager);
    return employee;
}
Also used : Employee(rest.spring.entity.Employee)

Example 3 with Employee

use of rest.spring.entity.Employee 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 4 with Employee

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

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

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