Skip to content

Instantly share code, notes, and snippets.

@theboreddev
Created October 27, 2021 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theboreddev/7030d4bb2092fe6e5ea9c3ff18be65e7 to your computer and use it in GitHub Desktop.
Save theboreddev/7030d4bb2092fe6e5ea9c3ff18be65e7 to your computer and use it in GitHub Desktop.
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