Compare commits

..

No commits in common. "591cd28eb4fddfdb3950fc96c169c8a093e198dd" and "440008ec6438d8a3abddf38acf34a13039a12acf" have entirely different histories.

2 changed files with 16 additions and 7 deletions

View file

@ -12,11 +12,11 @@ object ProfileRepository {
), ),
) )
fun get(id: UInt): Profile? { fun getById(id: UInt): Profile? {
return profiles.get(id) return profiles.get(id)
} }
fun delete(id: UInt): Boolean { fun deleteById(id: UInt): Boolean {
return profiles.remove(id) != null return profiles.remove(id) != null
} }
} }

View file

@ -3,7 +3,6 @@ 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
@ -16,9 +15,14 @@ fun Application.configureRouting() {
} }
get("/{id}") { get("/{id}") {
val id = call.parameters["id"]?.toUIntOrNull() ?: throw BadRequestException("Invalid ID") val id = try {
call.parameters["id"]!!.toUInt()
} catch (ex: kotlin.NumberFormatException) {
call.respond(HttpStatusCode.BadRequest)
return@get
}
val profile = ProfileRepository.get(id) val profile = ProfileRepository.getById(id)
if (profile == null) { if (profile == null) {
call.respond(HttpStatusCode.NotFound) call.respond(HttpStatusCode.NotFound)
return@get return@get
@ -28,9 +32,14 @@ fun Application.configureRouting() {
} }
delete("/{id}") { delete("/{id}") {
val id = call.parameters["id"]?.toUIntOrNull() ?: throw BadRequestException("Invalid ID") val id = try {
call.parameters["id"]!!.toUInt()
} catch (ex: kotlin.NumberFormatException) {
call.respond(HttpStatusCode.BadRequest)
return@delete
}
if (!ProfileRepository.delete(id)) { if (!ProfileRepository.deleteById(id)) {
call.respond(HttpStatusCode.NotFound) call.respond(HttpStatusCode.NotFound)
} else { } else {
call.respond(HttpStatusCode.OK) call.respond(HttpStatusCode.OK)