feat: basic api for get & post, no db yet
This commit is contained in:
parent
352124a6e8
commit
440008ec64
3 changed files with 78 additions and 2 deletions
17
src/main/kotlin/su/coolpeople/model/Profile.kt
Normal file
17
src/main/kotlin/su/coolpeople/model/Profile.kt
Normal 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
|
||||||
|
)
|
22
src/main/kotlin/su/coolpeople/model/ProfileRepository.kt
Normal file
22
src/main/kotlin/su/coolpeople/model/ProfileRepository.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue