WIP
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import io.ktor.routing.Routing
|
||||
import org.kodein.di.Kodein
|
||||
|
||||
fun Routing.chapters(kodein: Kodein) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import be.vandewalleh.auth.SimpleJWT
|
||||
import be.vandewalleh.auth.UsernamePasswordCredential
|
||||
import be.vandewalleh.services.UserService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.post
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.instance
|
||||
import org.mindrot.jbcrypt.BCrypt
|
||||
|
||||
fun Routing.login(kodein: Kodein) {
|
||||
val simpleJwt by kodein.instance<SimpleJWT>()
|
||||
val userService by kodein.instance<UserService>()
|
||||
|
||||
data class TokenResponse(val token: String)
|
||||
|
||||
post {
|
||||
val credential = call.receive<UsernamePasswordCredential>()
|
||||
|
||||
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
|
||||
?: return@post call.respond(HttpStatusCode.Unauthorized)
|
||||
|
||||
if (!BCrypt.checkpw(credential.password, password)) {
|
||||
return@post call.respond(HttpStatusCode.Unauthorized)
|
||||
}
|
||||
|
||||
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import be.vandewalleh.extensions.userId
|
||||
import be.vandewalleh.services.NotesService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.get
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.instance
|
||||
|
||||
fun Routing.notes(kodein: Kodein) {
|
||||
val notesService by kodein.instance<NotesService>()
|
||||
|
||||
get {
|
||||
val userId = call.userId()
|
||||
val notes = notesService.getNotes(userId)
|
||||
call.respond(notes)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import be.vandewalleh.extensions.respondStatus
|
||||
import be.vandewalleh.services.UserRegistrationDto
|
||||
import be.vandewalleh.services.UserService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.post
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import io.ktor.auth.authenticate
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.route
|
||||
import org.kodein.di.Kodein
|
||||
|
||||
fun Routing.registerRoutes(kodein: Kodein) {
|
||||
route("/login") {
|
||||
this@registerRoutes.login(kodein)
|
||||
}
|
||||
|
||||
route("/register") {
|
||||
this@registerRoutes.register(kodein)
|
||||
}
|
||||
|
||||
authenticate {
|
||||
route("/notes") {
|
||||
this@registerRoutes.notes(kodein)
|
||||
|
||||
route("/{noteTitle}") {
|
||||
this@registerRoutes.title(kodein)
|
||||
|
||||
route("/chapters/{chapterNumber}") {
|
||||
this@registerRoutes.chapters(kodein)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package be.vandewalleh.routing
|
||||
|
||||
import be.vandewalleh.extensions.*
|
||||
import be.vandewalleh.services.NotesService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.instance
|
||||
|
||||
fun Routing.title(kodein: Kodein) {
|
||||
val notesService by kodein.instance<NotesService>()
|
||||
|
||||
post {
|
||||
val userId = call.userId()
|
||||
val title = call.parameters.noteTitle()
|
||||
val tags = call.receiveTags()
|
||||
val noteId = call.parameters.noteId(userId)
|
||||
|
||||
if (noteId != null) {
|
||||
return@post call.respondStatus(HttpStatusCode.Conflict)
|
||||
}
|
||||
|
||||
notesService.createNote(userId, title, tags)
|
||||
call.respondStatus(HttpStatusCode.Created)
|
||||
}
|
||||
|
||||
get {
|
||||
val userId = call.userId()
|
||||
val noteId = call.parameters.noteId(userId)
|
||||
?: return@get call.respondStatus(HttpStatusCode.NotFound)
|
||||
|
||||
val response = notesService.getTagsAndChapters(noteId)
|
||||
call.respond(response)
|
||||
}
|
||||
|
||||
patch {
|
||||
val notePatch = call.receiveNotePatch()
|
||||
if (notePatch.tags == null && notePatch.title == null)
|
||||
return@patch call.respondStatus(HttpStatusCode.BadRequest)
|
||||
|
||||
val userId = call.userId()
|
||||
val noteId = call.parameters.noteId(userId)
|
||||
?: return@patch call.respondStatus(HttpStatusCode.NotFound)
|
||||
|
||||
notesService.updateNote(noteId, notePatch.tags, notePatch.title)
|
||||
call.respondStatus(HttpStatusCode.OK)
|
||||
}
|
||||
|
||||
delete {
|
||||
val userId = call.userId()
|
||||
val noteId = call.parameters.noteId(userId)
|
||||
?: return@delete call.respondStatus(HttpStatusCode.NotFound)
|
||||
|
||||
notesService.deleteNote(noteId)
|
||||
call.respondStatus(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user