Merge branch 'feature/user-operations'

This commit is contained in:
Hubert Van De Walle 2020-04-21 20:34:38 +02:00
commit 9bf1c32e84
8 changed files with 87 additions and 45 deletions

View File

@ -14,7 +14,7 @@
# Group Accounts # Group Accounts
## Create an account [/register] ## Create an account [/user]
### Register a new user [POST] ### Register a new user [POST]
@ -33,7 +33,7 @@
+ message: User already exists (string) + message: User already exists (string)
## Authenticate user [/login] ## Authenticate user [/user/login]
Authenticate one user to access protected routing. Authenticate one user to access protected routing.
### Authenticate a user [POST] ### Authenticate a user [POST]

View File

@ -18,7 +18,7 @@ fun Routing.login(kodein: Kodein) {
data class TokenResponse(val token: String) data class TokenResponse(val token: String)
route("/login"){ route("/user/login"){
post { post {
val credential = call.receive<UsernamePasswordCredential>() val credential = call.receive<UsernamePasswordCredential>()

View File

@ -1,32 +0,0 @@
package be.vandewalleh.routing
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.services.UserRegistrationDto
import be.vandewalleh.services.UserService
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
fun Routing.register(kodein: Kodein) {
val userService by kodein.instance<UserService>()
post("/register") {
val user = call.receive<UserRegistrationDto>()
if (userService.userExists(user.username, user.email))
return@post call.respond(HttpStatusCode.Conflict)
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
userService.createUser(
UserRegistrationDto(user.username, user.email, hashedPassword)
)
return@post call.respondStatus(HttpStatusCode.Created)
}
}

View File

@ -0,0 +1,62 @@
package be.vandewalleh.routing
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.UserDto
import be.vandewalleh.services.UserService
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
fun Routing.user(kodein: Kodein) {
val userService by kodein.instance<UserService>()
route("/user") {
post {
val user = call.receive<UserDto>()
if (userService.userExists(user.username, user.email))
return@post call.respond(HttpStatusCode.Conflict)
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
userService.createUser(
UserDto(user.username, user.email, hashedPassword)
)
call.respondStatus(HttpStatusCode.Created)
}
authenticate {
put {
val user = call.receive<UserDto>()
if (userService.userExists(user.username, user.email))
return@put call.respond(HttpStatusCode.Conflict)
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
userService.updateUser(
call.userId(),
UserDto(user.username, user.email, hashedPassword)
)
call.respondStatus(HttpStatusCode.OK)
}
delete {
userService.deleteUser(call.userId())
call.respondStatus(HttpStatusCode.OK)
}
}
}
}

View File

@ -52,7 +52,7 @@ class UserService(override val kodein: Kodein) : KodeinAware {
* create a new user * create a new user
* password should already be hashed * password should already be hashed
*/ */
fun createUser(user: UserRegistrationDto) { fun createUser(user: UserDto) {
db.useTransaction { db.useTransaction {
val newUser = User { val newUser = User {
this.username = user.username this.username = user.username
@ -65,6 +65,19 @@ class UserService(override val kodein: Kodein) : KodeinAware {
} }
} }
fun updateUser(userId: Int, user: UserDto) {
db.useTransaction {
db.update(Users) {
it.username to user.username
it.email to user.email
it.password to user.password
where {
it.id eq userId
}
}
}
}
fun deleteUser(userId: Int) { fun deleteUser(userId: Int) {
db.useTransaction { db.useTransaction {
db.delete(Users) { it.id eq userId } db.delete(Users) { it.id eq userId }
@ -72,4 +85,4 @@ class UserService(override val kodein: Kodein) : KodeinAware {
} }
} }
data class UserRegistrationDto(val username: String, val email: String, val password: String) data class UserDto(val username: String, val email: String, val password: String)

View File

@ -3,7 +3,7 @@ import apiClient from '@/api'
export default { export default {
async login({username, password}) { async login({username, password}) {
try { try {
const {data} = await apiClient.post('/signin', { const {data} = await apiClient.post('/user/signin', {
username, username,
password password
}) })

View File

@ -3,7 +3,7 @@ import apiClient from '@/api'
export default { export default {
async register({username, email, password}) { async register({username, email, password}) {
try { try {
await apiClient.post('/signup', { await apiClient.post('/user', {
username, username,
email, email,
password password

File diff suppressed because one or more lines are too long