diff --git a/src/main/kotlin/su/coolpeople/model/Profile.kt b/src/main/kotlin/su/coolpeople/model/Profile.kt new file mode 100644 index 0000000..3f53f2a --- /dev/null +++ b/src/main/kotlin/su/coolpeople/model/Profile.kt @@ -0,0 +1,17 @@ +package su.coolpeople.model + +import kotlinx.serialization.Serializable + +// https://wiki.dc09.ru/doku.php?id=wiki:coolpeople:dev:backend#анкета +@Serializable +data class Profile( + val name: String, + val age: UInt, + val lat: Float, + val lon: Float, + val city: String, + val approx_loc: Boolean, + val desc: String, + val tags: Array, // TODO: maybe MutableList + val contacts: Array, // TODO: same +) diff --git a/src/main/kotlin/su/coolpeople/model/ProfileRepository.kt b/src/main/kotlin/su/coolpeople/model/ProfileRepository.kt new file mode 100644 index 0000000..e97e96c --- /dev/null +++ b/src/main/kotlin/su/coolpeople/model/ProfileRepository.kt @@ -0,0 +1,22 @@ +package su.coolpeople.model + +object ProfileRepository { + // TODO: Meilisearch + private val profiles = hashMapOf( + 0u to Profile( + "name", 20u, + 60f, 49f, "city", true, + "description", + arrayOf("programming", "music"), + arrayOf("t.me/example"), + ), + ) + + fun getById(id: UInt): Profile? { + return profiles.get(id) + } + + fun deleteById(id: UInt): Boolean { + return profiles.remove(id) != null + } +} diff --git a/src/main/kotlin/su/coolpeople/plugins/Routing.kt b/src/main/kotlin/su/coolpeople/plugins/Routing.kt index e415375..8441088 100644 --- a/src/main/kotlin/su/coolpeople/plugins/Routing.kt +++ b/src/main/kotlin/su/coolpeople/plugins/Routing.kt @@ -3,11 +3,48 @@ package su.coolpeople.plugins import io.ktor.server.application.* import io.ktor.server.response.* import io.ktor.server.routing.* +import io.ktor.http.HttpStatusCode +import su.coolpeople.model.ProfileRepository +import kotlin.text.toUInt fun Application.configureRouting() { routing { - get("/") { - call.respondText("Hello World!") + route("/api/profile") { + put { + TODO() + } + + get("/{id}") { + val id = try { + call.parameters["id"]!!.toUInt() + } catch (ex: kotlin.NumberFormatException) { + call.respond(HttpStatusCode.BadRequest) + return@get + } + + val profile = ProfileRepository.getById(id) + if (profile == null) { + call.respond(HttpStatusCode.NotFound) + return@get + } + + call.respond(profile) + } + + delete("/{id}") { + val id = try { + call.parameters["id"]!!.toUInt() + } catch (ex: kotlin.NumberFormatException) { + call.respond(HttpStatusCode.BadRequest) + return@delete + } + + if (!ProfileRepository.deleteById(id)) { + call.respond(HttpStatusCode.NotFound) + } else { + call.respond(HttpStatusCode.OK) + } + } } } }