Compare commits

...

2 commits

Author SHA1 Message Date
440008ec64
feat: basic api for get & post, no db yet 2024-06-21 16:47:46 +04:00
352124a6e8
fix: add .kotlin to ignore 2024-06-21 16:47:27 +04:00
4 changed files with 81 additions and 3 deletions

4
.gitignore vendored
View file

@ -4,6 +4,8 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/
.kotlin/
### STS ###
.apt_generated
.classpath
@ -33,4 +35,4 @@ out/
/.nb-gradle/
### VS Code ###
.vscode/
.vscode/

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.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)
}
}
}
}
}