Compare commits
2 commits
ff4a2746f2
...
440008ec64
Author | SHA1 | Date | |
---|---|---|---|
440008ec64 | |||
352124a6e8 |
4 changed files with 81 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,6 +4,8 @@ build/
|
|||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
.kotlin/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
|
|
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.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue