refactor: cleaner str-to-int with 400 response on err
This commit is contained in:
parent
ae2d98fa1f
commit
591cd28eb4
1 changed files with 3 additions and 12 deletions
|
@ -3,6 +3,7 @@ package su.coolpeople.plugins
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
import io.ktor.server.plugins.BadRequestException
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import su.coolpeople.model.ProfileRepository
|
import su.coolpeople.model.ProfileRepository
|
||||||
import kotlin.text.toUInt
|
import kotlin.text.toUInt
|
||||||
|
@ -15,12 +16,7 @@ fun Application.configureRouting() {
|
||||||
}
|
}
|
||||||
|
|
||||||
get("/{id}") {
|
get("/{id}") {
|
||||||
val id = try {
|
val id = call.parameters["id"]?.toUIntOrNull() ?: throw BadRequestException("Invalid ID")
|
||||||
call.parameters["id"]!!.toUInt()
|
|
||||||
} catch (ex: kotlin.NumberFormatException) {
|
|
||||||
call.respond(HttpStatusCode.BadRequest)
|
|
||||||
return@get
|
|
||||||
}
|
|
||||||
|
|
||||||
val profile = ProfileRepository.get(id)
|
val profile = ProfileRepository.get(id)
|
||||||
if (profile == null) {
|
if (profile == null) {
|
||||||
|
@ -32,12 +28,7 @@ fun Application.configureRouting() {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete("/{id}") {
|
delete("/{id}") {
|
||||||
val id = try {
|
val id = call.parameters["id"]?.toUIntOrNull() ?: throw BadRequestException("Invalid ID")
|
||||||
call.parameters["id"]!!.toUInt()
|
|
||||||
} catch (ex: kotlin.NumberFormatException) {
|
|
||||||
call.respond(HttpStatusCode.BadRequest)
|
|
||||||
return@delete
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ProfileRepository.delete(id)) {
|
if (!ProfileRepository.delete(id)) {
|
||||||
call.respond(HttpStatusCode.NotFound)
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
|
Loading…
Add table
Reference in a new issue