More tests !

This commit is contained in:
2020-06-14 23:06:28 +02:00
parent 913e3dfc93
commit 07ec732c55
10 changed files with 281 additions and 16 deletions
+15 -7
View File
@@ -1,5 +1,6 @@
package be.vandewalleh.routing
import be.vandewalleh.entities.User
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.UserService
@@ -12,16 +13,22 @@ import io.ktor.routing.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
import java.time.LocalDateTime
fun Routing.user(kodein: Kodein) {
val userService by kodein.instance<UserService>()
post("/user/test") {
val user = call.receive<User>()
call.respond(user)
}
route("/user") {
post {
val user = call.receive<UserDto>()
val user = call.receive<User>()
if (userService.userExists(user.username, user.email))
return@post call.respond(HttpStatusCode.Conflict)
return@post call.respondStatus(HttpStatusCode.Conflict)
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
@@ -32,7 +39,7 @@ fun Routing.user(kodein: Kodein) {
authenticate {
put {
val user = call.receive<UserDto>()
val user = call.receive<User>()
if (userService.userExists(user.username, user.email))
return@put call.respond(HttpStatusCode.Conflict)
@@ -45,12 +52,13 @@ fun Routing.user(kodein: Kodein) {
}
delete {
userService.deleteUser(call.userId())
call.respondStatus(HttpStatusCode.OK)
val status = if (userService.deleteUser(call.userId()))
HttpStatusCode.OK
else
HttpStatusCode.NotFound
call.respondStatus(status)
}
}
}
}
private data class UserDto(val username: String, val email: String, val password: String)