Simplify endpoints in example app

This commit is contained in:
Michael Bull 2018-01-11 19:27:07 +00:00
parent 76870ef78a
commit d2723009df
1 changed files with 36 additions and 37 deletions

View File

@ -1,13 +1,11 @@
package com.github.michaelbull.result.example package com.github.michaelbull.result.example
import com.github.michaelbull.result.andThen import com.github.michaelbull.result.*
import com.github.michaelbull.result.example.model.domain.Customer import com.github.michaelbull.result.example.model.domain.Customer
import com.github.michaelbull.result.example.model.domain.CustomerId import com.github.michaelbull.result.example.model.domain.CustomerId
import com.github.michaelbull.result.example.model.domain.DomainMessage import com.github.michaelbull.result.example.model.domain.DomainMessage
import com.github.michaelbull.result.example.model.dto.CustomerDto import com.github.michaelbull.result.example.model.dto.CustomerDto
import com.github.michaelbull.result.example.service.CustomerService import com.github.michaelbull.result.example.service.CustomerService
import com.github.michaelbull.result.mapBoth
import com.github.michaelbull.result.mapError
import io.ktor.application.Application import io.ktor.application.Application
import io.ktor.application.call import io.ktor.application.call
import io.ktor.application.install import io.ktor.application.install
@ -22,6 +20,7 @@ import io.ktor.response.respond
import io.ktor.routing.get import io.ktor.routing.get
import io.ktor.routing.post import io.ktor.routing.post
import io.ktor.routing.routing import io.ktor.routing.routing
import io.ktor.util.ValuesMap
fun Application.main() { fun Application.main() {
install(DefaultHeaders) install(DefaultHeaders)
@ -35,45 +34,45 @@ fun Application.main() {
routing { routing {
get("/customers/{id}") { get("/customers/{id}") {
val id = call.parameters["id"]?.toLongOrNull() readId(call.parameters)
if (id == null) { .andThen(CustomerId.Companion::create)
call.respond(HttpStatusCode.BadRequest)
} else {
CustomerId.create(id)
.andThen(CustomerService::getById) .andThen(CustomerService::getById)
.mapError(::messageToResponse) .mapError(::messageToResponse)
.mapBoth( .mapBoth(
success = { call.respond(HttpStatusCode.OK, CustomerDto.from(it)) }, { call.respond(HttpStatusCode.OK, CustomerDto.from(it)) },
failure = { call.respond(it.first, it.second) } { call.respond(it.first, it.second) }
) )
} }
}
post("/customers/{id}") { post("/customers/{id}") {
val id = call.parameters["id"]?.toLongOrNull() readId(call.parameters)
if (id == null) { .andThen {
call.respond(HttpStatusCode.BadRequest)
} else {
val dto = call.receive<CustomerDto>() val dto = call.receive<CustomerDto>()
dto.id = id dto.id = it
Ok(dto)
Customer.from(dto) }
.andThen(Customer.Companion::from)
.andThen(CustomerService::upsert) .andThen(CustomerService::upsert)
.mapError(::messageToResponse) .mapError(::messageToResponse)
.mapBoth( .mapBoth(
success = { { event ->
if (it == null) { if (event == null) {
call.respond(HttpStatusCode.NotModified) call.respond(HttpStatusCode.NotModified)
} else { } else {
val (status, message) = messageToResponse(it) val (status, message) = messageToResponse(event)
call.respond(status, message) call.respond(status, message)
} }
}, },
failure = { call.respond(it.first, it.second) } { call.respond(it.first, it.second) }
) )
} }
} }
}
}
private fun readId(values: ValuesMap): Result<Long, DomainMessage> {
val id = values["id"]?.toLongOrNull()
return if (id != null) Ok(id) else Err(DomainMessage.CustomerRequired)
} }
private fun messageToResponse(message: DomainMessage) = when (message) { private fun messageToResponse(message: DomainMessage) = when (message) {