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");
}
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;
}
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);
}
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);
}
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();
}
Aggregations