diff --git a/api/src/NotesApplication.kt b/api/src/NotesApplication.kt index 125c687..c1c3cff 100644 --- a/api/src/NotesApplication.kt +++ b/api/src/NotesApplication.kt @@ -7,11 +7,14 @@ import be.vandewalleh.migrations.Migration import be.vandewalleh.routing.registerRoutes import be.vandewalleh.services.serviceModule import io.ktor.application.Application +import io.ktor.application.call import io.ktor.application.feature import io.ktor.application.log +import io.ktor.response.respond import io.ktor.routing.Route import io.ktor.routing.Routing import io.ktor.routing.RoutingPath.Companion.root +import io.ktor.routing.get import io.ktor.routing.routing import me.liuwj.ktorm.database.Database import org.kodein.di.Kodein diff --git a/api/src/auth/AuthenticationModule.kt b/api/src/auth/AuthenticationModule.kt index 1a9de0f..f57ad63 100644 --- a/api/src/auth/AuthenticationModule.kt +++ b/api/src/auth/AuthenticationModule.kt @@ -11,7 +11,7 @@ import org.kodein.di.generic.instance fun Application.authenticationModule() { install(Authentication) { jwt { - val simpleJwt: SimpleJWT by kodein.instance() + val simpleJwt by kodein.instance() verifier(simpleJwt.verifier) validate { UserIdPrincipal(it.payload.getClaim("name").asString()) diff --git a/api/src/routing/ChaptersController.kt b/api/src/routing/ChaptersController.kt index 1fafeda..76850b7 100644 --- a/api/src/routing/ChaptersController.kt +++ b/api/src/routing/ChaptersController.kt @@ -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}") { + } + } } diff --git a/api/src/routing/LoginController.kt b/api/src/routing/LoginController.kt index b51b755..d776de1 100644 --- a/api/src/routing/LoginController.kt +++ b/api/src/routing/LoginController.kt @@ -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() + route("/login"){ + post { + val credential = call.receive() - 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))) } + } \ No newline at end of file diff --git a/api/src/routing/NotesController.kt b/api/src/routing/NotesController.kt index bec6086..f4108ea 100644 --- a/api/src/routing/NotesController.kt +++ b/api/src/routing/NotesController.kt @@ -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() - 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) + } } } diff --git a/api/src/routing/RegisterController.kt b/api/src/routing/RegisterController.kt index f36039f..8f32ae0 100644 --- a/api/src/routing/RegisterController.kt +++ b/api/src/routing/RegisterController.kt @@ -16,7 +16,7 @@ import org.mindrot.jbcrypt.BCrypt fun Routing.register(kodein: Kodein) { val userService by kodein.instance() - post { + post("/register") { val user = call.receive() if (userService.userExists(user.username, user.email)) diff --git a/api/src/routing/Routes.kt b/api/src/routing/Routes.kt index 24341d4..31e6401 100644 --- a/api/src/routing/Routes.kt +++ b/api/src/routing/Routes.kt @@ -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) } \ No newline at end of file diff --git a/api/src/routing/TitleController.kt b/api/src/routing/TitleController.kt index 0e994cb..4fb4e95 100644 --- a/api/src/routing/TitleController.kt +++ b/api/src/routing/TitleController.kt @@ -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() - 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) } }