-
-
Save m-debugger/fd653f13801f8489de3499cb3879e7a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
when (info) { | |
null -> { | |
return unauthorized() | |
} | |
else -> { | |
val validInfo = validator.validateInfo(info) | |
when (validInfo) { | |
is Either.Right -> { | |
if (!validInfo.b.authorised) { | |
// authorised is false so return unauthorised | |
return unauthorized() | |
} | |
// authorised is true, so use the authorised token to create a certificate | |
val authorisedInfo = validInfo.b | |
val productService = productServiceBuilder.buildProductService(authorisedInfo) | |
// TODO add when (productService) here | |
// | |
return when (productService) { | |
is Either.Right -> { | |
val product = productService.b.getProduct( | |
authorisedInfo.userId!!) | |
APIGatewayProxyResponseEvent() | |
.withBody(jacksonObjectMapper().writeValueAsString(product)) | |
.withStatusCode(200) | |
} | |
is Either.Left -> { | |
systemError() | |
} | |
} | |
} | |
is Either.Left -> { | |
return systemError() | |
} | |
} | |
} | |
} |
pakoito
commented
Oct 31, 2018
•
Either.monadError().bindingCatch {
val one = info.toOption().toEither { unauthorized() }.bind()
val two = validator.validateInfo(one).bind()
val result = if (!two.authorised) {
unauthorized().left()
} else {
try {
val three = productServiceBuilder.buildProductService(two).bind()
val four = getProduct(authorisedInfo.userId!!).bind()
APIGatewayProxyResponseEvent()
.withBody(jacksonObjectMapper().writeValueAsString(product))
.withStatusCode(200).right()
} catch (t: Throwable) {
systemError().left()
}
}
result.bind()
}
what about:
(info ?: return unauthorized())
.let(validator::validateInfo)
.flatMap { validatedInfo ->
val authorisedInfo = validatedInfo.takeIf { it.authorised } ?: return unauthorized()
productServiceBuilder.buildProductService(authorisedInfo)
.map { productService ->
val product = productService.getProduct(authorisedInfo.userId!!)
APIGatewayProxyResponseEvent()
.withBody(jacksonObjectMapper().writeValueAsString(product))
.withStatusCode(200)
}
}
.getOrElse(::systemError)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment