Simplify endpoints in example app
This commit is contained in:
parent
76870ef78a
commit
d2723009df
@ -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)
|
.andThen(CustomerService::getById)
|
||||||
} else {
|
.mapError(::messageToResponse)
|
||||||
CustomerId.create(id)
|
.mapBoth(
|
||||||
.andThen(CustomerService::getById)
|
{ call.respond(HttpStatusCode.OK, CustomerDto.from(it)) },
|
||||||
.mapError(::messageToResponse)
|
{ call.respond(it.first, it.second) }
|
||||||
.mapBoth(
|
)
|
||||||
success = { call.respond(HttpStatusCode.OK, CustomerDto.from(it)) },
|
|
||||||
failure = { 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)
|
val dto = call.receive<CustomerDto>()
|
||||||
} else {
|
dto.id = it
|
||||||
val dto = call.receive<CustomerDto>()
|
Ok(dto)
|
||||||
dto.id = id
|
}
|
||||||
|
.andThen(Customer.Companion::from)
|
||||||
Customer.from(dto)
|
.andThen(CustomerService::upsert)
|
||||||
.andThen(CustomerService::upsert)
|
.mapError(::messageToResponse)
|
||||||
.mapError(::messageToResponse)
|
.mapBoth(
|
||||||
.mapBoth(
|
{ event ->
|
||||||
success = {
|
if (event == null) {
|
||||||
if (it == null) {
|
call.respond(HttpStatusCode.NotModified)
|
||||||
call.respond(HttpStatusCode.NotModified)
|
} else {
|
||||||
} else {
|
val (status, message) = messageToResponse(event)
|
||||||
val (status, message) = messageToResponse(it)
|
call.respond(status, message)
|
||||||
call.respond(status, message)
|
}
|
||||||
}
|
},
|
||||||
},
|
{ call.respond(it.first, it.second) }
|
||||||
failure = { 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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user