add Login and Register routes with serialization
continuous-integration/drone/push Build is passing Details

pull/4/head
Lucas Evard 1 year ago
parent f8b4a1cb01
commit 95897ee891

@ -4,7 +4,9 @@ WORKDIR /home/maven/src
RUN mvn clean package
FROM openjdk:11
EXPOSE 8080:8080
EXPOSE 8080
RUN mkdir /app
COPY --from=build /home/maven/src/target/*-with-dependencies.jar /app/ktor-docker-sample.jar
ENTRYPOINT ["java","-jar","/app/ktor-docker-sample.jar"]
ENTRYPOINT ["java", "-jar", "/app/ktor-docker-sample.jar"]

@ -8,7 +8,7 @@
<name>allin-api</name>
<description>allin-api</description>
<properties>
<kotlin.version>1.8.10</kotlin.version>
<kotlin.version>1.9.10</kotlin.version>
<serialization.version>1.5.0</serialization.version>
<ktor_version>2.3.4</ktor_version>
<kotlin.code.style>official</kotlin.code.style>

@ -4,3 +4,6 @@ import kotlinx.serialization.Serializable
@Serializable
data class User(val username: String, val email: String, val password: String, var nbCoins: Int)
@Serializable
data class CheckUser(val username: String,val password: String)

@ -1,5 +1,6 @@
package allin.routing
import allin.model.CheckUser
import allin.model.User
import io.ktor.http.*
import io.ktor.server.application.*
@ -7,13 +8,17 @@ 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))
val users = mutableListOf<User>()
fun Application.UserRouter() {
routing {
route("/users") {
get {
call.respond(users)
}
}
route("/users/register"){
post {
val newUser = call.receive<User>()
users.add(newUser)
@ -21,6 +26,20 @@ fun Application.UserRouter() {
}
}
route("/users/login"){
post {
val checkUser = call.receive<CheckUser>()
val user = users.find { it.username == checkUser.username }
if (user != null) {
if(user.password==checkUser.password)
call.respond(HttpStatusCode.fromValue(200),user)
else call.respond(HttpStatusCode.NotFound)
} else {
call.respond(HttpStatusCode.NotFound)
}
}
}
route("/users/{username}") {
get {
val username = call.parameters["username"]
@ -55,3 +74,5 @@ fun Application.UserRouter() {
}
}
}
// REGISTER 201 created 400 bad request
// LOGIN 200 OK 404

Loading…
Cancel
Save