Skip to content

Instantly share code, notes, and snippets.

@theboreddev
Created October 27, 2021 06:21
Embed
What would you like to do?
Using fold method
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