Using fold method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PayrollService(private val employeeService: EmployeeService) { | |
private val logger = LoggerFactory.getLogger(PayrollService::class.java) | |
fun generatePayrollFor(employeeName: String): Either<Problem, Payroll> { | |
return employeeService.findEmployeeByName(employeeName) | |
.fold( | |
{ httpFailureProblem -> | |
logger.info("Could not fetch employee: ${httpFailureProblem.message()}") | |
httpFailureProblem.left() | |
}, | |
{ employee -> | |
buildPayrollForEmployee(employee) | |
} | |
) | |
} | |
data class Payroll(private val id: String, | |
private val employeeId: String, | |
private val salary: Float, | |
private val date: Date) | |
private fun buildPayrollForEmployee(employee: Employee): Either<Problem, Payroll> { | |
return Payroll(UUID.randomUUID().toString(), employee.id, employee.salary, Date()).right() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment