I am using the java stream API to handle incoming requests my employee service.
A few questions I had are:
Does using the streams API in the way I'm using it below affect the performance of the application?
I have not seen any tutorials using the streams API in the way I am using it. So is it bad practice to use streams this way?
Does using streams this way make the code less readable?
public Response<EmployeeDto> saveEmployee(NewEmployeeRequest request) {
boolean isEmployeeEmailExisting = employeeRepository.existsByEmail(request.email());
if (isEmployeeEmailExisting) {
log.error("Employee with email {} already exists", request.email());
throw new ApplicationException(HttpStatus.CONFLICT, "Employee Already Exists", null);
}
EmployeeDto employeeDto = Stream.of(request)
.map(employeeMapper::mapToEmployee)
.map(employeeRepository::save)
.map(employeeMapper::mapToDto)
.findFirst()
.get();
return Response.successfulResponse(HttpStatus.CREATED.value(), "Successful", employeeDto);
}
Employee employee = employeeMapper.mapToEmployee(request); Employee savedEmployee = employeeRepository.save(employee); EmployeeDto dto = employeeMapper.mapToDto(savedEmployee);
\$\endgroup\$