-
Notifications
You must be signed in to change notification settings - Fork 241
11. Build Update Employee REST API
Ramesh Fadatare edited this page Sep 11, 2022
·
1 revision
EmployeeDto updateEmployee(EmployeeDto employeeDto);
@Override
public EmployeeDto updateEmployee(EmployeeDto employeeDto) {
// we need to check whether employee with given id is exist in DB or not
Employee existingEmployee = employeeRepository.findById(employeeDto.getId())
.orElseThrow(() -> new IllegalArgumentException(
"Employee not exists with a given id : " + employeeDto.getId())
);
// convert EmployeeDto to Employee JPA entity
Employee employee = EmployeeConverter.mapToEmployee(employeeDto);
return EmployeeConverter.mapToEmployeeDto(employeeRepository.save(employee));
}
// build update employee REST API
// http://localhost:8080/api/employees/1
@PutMapping("{id}")
public ResponseEntity<EmployeeDto> updateEmployee(@PathVariable("id") long id
,@RequestBody EmployeeDto employeeDto){
employeeDto.setId(id);
EmployeeDto updatedEmployee = employeeService.updateEmployee(employeeDto);
return new ResponseEntity<EmployeeDto>(updatedEmployee, HttpStatus.OK);
}