✨ add User routes and serialization
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
6cd21f30e8
commit
f8b4a1cb01
@ -1,15 +1,26 @@
|
|||||||
package allin
|
package allin
|
||||||
|
|
||||||
import allin.plugins.*
|
import allin.routing.BasicRouting
|
||||||
|
import allin.routing.UserRouter
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.engine.*
|
import io.ktor.server.engine.*
|
||||||
import io.ktor.server.netty.*
|
import io.ktor.server.netty.*
|
||||||
|
import io.ktor.server.plugins.contentnegotiation.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
|
embeddedServer(Netty,port=8080,host="0.0.0.0"){
|
||||||
.start(wait = true)
|
extracted()
|
||||||
|
}.start(wait = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Application.module() {
|
private fun Application.extracted() {
|
||||||
configureRouting()
|
install(ContentNegotiation) {
|
||||||
|
json()
|
||||||
|
}
|
||||||
|
BasicRouting()
|
||||||
|
UserRouter()
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package allin.entities
|
||||||
|
|
||||||
|
import org.ktorm.schema.Table
|
||||||
|
import org.ktorm.schema.int
|
||||||
|
import org.ktorm.schema.varchar
|
||||||
|
|
||||||
|
object UserEntity : Table<Nothing>("user") {
|
||||||
|
val id = int("id").primaryKey()
|
||||||
|
val username = varchar("username")
|
||||||
|
val password = varchar("password")
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package allin.model
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class User(val username: String, val email: String, val password: String, var nbCoins: Int)
|
@ -1,13 +1,71 @@
|
|||||||
package allin.plugins
|
/*package allin.plugins
|
||||||
|
|
||||||
|
import allin.model.User
|
||||||
|
import io.ktor.http.*
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.html.*
|
||||||
|
import io.ktor.server.request.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
import kotlinx.html.body
|
||||||
|
import kotlinx.html.h1
|
||||||
|
|
||||||
|
val users = mutableListOf<User>()
|
||||||
fun Application.configureRouting() {
|
fun Application.configureRouting() {
|
||||||
routing {
|
routing {
|
||||||
get("/") {
|
get("/") {
|
||||||
call.respondText("Hello World!")
|
call.respondHtml(HttpStatusCode.OK) {
|
||||||
|
body {
|
||||||
|
h1 {
|
||||||
|
+"Bienvenue dans l'API de l'application ALLin!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
route("/users") {
|
||||||
|
get {
|
||||||
|
call.respondText(users.joinToString("\n"), ContentType.Text.Plain)
|
||||||
|
}
|
||||||
|
post {
|
||||||
|
val newUser = call.receive<User>()
|
||||||
|
users.add(newUser)
|
||||||
|
call.respond(HttpStatusCode.Created, newUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
route("/users/{username}") {
|
||||||
|
get {
|
||||||
|
val username = call.parameters["username"]
|
||||||
|
val user = users.find { it.username == username }
|
||||||
|
if (user != null) {
|
||||||
|
call.respond(user)
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
put {
|
||||||
|
val username = call.parameters["username"]
|
||||||
|
val userIndex = users.indexOfFirst { it.username == username }
|
||||||
|
if (userIndex != -1) {
|
||||||
|
val updatedUser = call.receive<User>()
|
||||||
|
users[userIndex] = updatedUser
|
||||||
|
call.respond(updatedUser)
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete {
|
||||||
|
val username = call.parameters["username"]
|
||||||
|
val user = users.find { it.username == username }
|
||||||
|
if (user != null) {
|
||||||
|
users.remove(user)
|
||||||
|
call.respond(HttpStatusCode.NoContent)
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
@ -0,0 +1,13 @@
|
|||||||
|
package allin.routing
|
||||||
|
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
|
||||||
|
fun Application.BasicRouting(){
|
||||||
|
routing {
|
||||||
|
get("/") {
|
||||||
|
call.respond("Bienvenue sur l'API de AlLin!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package allin.routing
|
||||||
|
|
||||||
|
import allin.model.User
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.request.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
|
||||||
|
val users = mutableListOf(User("user1", "user1@example.com", "mdp",1000))
|
||||||
|
fun Application.UserRouter() {
|
||||||
|
routing {
|
||||||
|
route("/users") {
|
||||||
|
get {
|
||||||
|
call.respond(users)
|
||||||
|
}
|
||||||
|
post {
|
||||||
|
val newUser = call.receive<User>()
|
||||||
|
users.add(newUser)
|
||||||
|
call.respond(HttpStatusCode.Created, newUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
route("/users/{username}") {
|
||||||
|
get {
|
||||||
|
val username = call.parameters["username"]
|
||||||
|
val user = users.find { it.username == username }
|
||||||
|
if (user != null) {
|
||||||
|
call.respond(user)
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
put {
|
||||||
|
val username = call.parameters["username"]
|
||||||
|
val userIndex = users.indexOfFirst { it.username == username }
|
||||||
|
if (userIndex != -1) {
|
||||||
|
val updatedUser = call.receive<User>()
|
||||||
|
users[userIndex] = updatedUser
|
||||||
|
call.respond(updatedUser)
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete {
|
||||||
|
val username = call.parameters["username"]
|
||||||
|
val user = users.find { it.username == username }
|
||||||
|
if (user != null) {
|
||||||
|
users.remove(user)
|
||||||
|
call.respond(HttpStatusCode.NoContent)
|
||||||
|
} else {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue