feat: basic api for get & post, no db yet

This commit is contained in:
DarkCat09 2024-06-21 16:47:46 +04:00
parent 352124a6e8
commit 440008ec64
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
3 changed files with 78 additions and 2 deletions

View file

@ -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<String>, // TODO: maybe MutableList<String>
val contacts: Array<String>, // TODO: same
)

View file

@ -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
}
}

View file

@ -3,11 +3,48 @@ 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.http.HttpStatusCode
import su.coolpeople.model.ProfileRepository
import kotlin.text.toUInt
fun Application.configureRouting() { fun Application.configureRouting() {
routing { routing {
get("/") { route("/api/profile") {
call.respondText("Hello World!") 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)
}
}
} }
} }
} }