This is ugly..

This commit is contained in:
2020-04-21 16:06:53 +02:00
parent 1f9923dc82
commit 69c35d0732
8 changed files with 81 additions and 78 deletions
+6
View File
@@ -1,8 +1,14 @@
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.chapters(kodein: Kodein) {
authenticate {
route("/notes/{noteTitle}/chapters/{chapterNumber}") {
}
}
}
+12 -8
View File
@@ -9,6 +9,7 @@ import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Routing
import io.ktor.routing.post
import io.ktor.routing.route
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import org.mindrot.jbcrypt.BCrypt
@@ -19,17 +20,20 @@ fun Routing.login(kodein: Kodein) {
data class TokenResponse(val token: String)
post {
val credential = call.receive<UsernamePasswordCredential>()
route("/login"){
post {
val credential = call.receive<UsernamePasswordCredential>()
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
?: return@post call.respond(HttpStatusCode.Unauthorized)
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)
if (!BCrypt.checkpw(credential.password, password)) {
return@post call.respond(HttpStatusCode.Unauthorized)
}
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
}
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
}
}
+7 -4
View File
@@ -3,6 +3,7 @@ package be.vandewalleh.routing
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.NotesService
import io.ktor.application.call
import io.ktor.auth.authenticate
import io.ktor.response.respond
import io.ktor.routing.Routing
import io.ktor.routing.get
@@ -12,9 +13,11 @@ 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)
authenticate {
get("/notes") {
val userId = call.userId()
val notes = notesService.getNotes(userId)
call.respond(notes)
}
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ import org.mindrot.jbcrypt.BCrypt
fun Routing.register(kodein: Kodein) {
val userService by kodein.instance<UserService>()
post {
post("/register") {
val user = call.receive<UserRegistrationDto>()
if (userService.userExists(user.username, user.email))
+5 -23
View File
@@ -1,30 +1,12 @@
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)
}
}
}
}
login(kodein)
register(kodein)
notes(kodein)
title(kodein)
chapters(kodein)
}
+46 -41
View File
@@ -3,6 +3,7 @@ package be.vandewalleh.routing
import be.vandewalleh.extensions.*
import be.vandewalleh.services.NotesService
import io.ktor.application.call
import io.ktor.auth.authenticate
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.routing.*
@@ -12,48 +13,52 @@ 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)
authenticate {
route("/notes/{noteTitle}") {
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)
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)
}
}
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)
}
}