This is ugly..
This commit is contained in:
parent
1f9923dc82
commit
69c35d0732
@ -7,11 +7,14 @@ import be.vandewalleh.migrations.Migration
|
|||||||
import be.vandewalleh.routing.registerRoutes
|
import be.vandewalleh.routing.registerRoutes
|
||||||
import be.vandewalleh.services.serviceModule
|
import be.vandewalleh.services.serviceModule
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
|
import io.ktor.application.call
|
||||||
import io.ktor.application.feature
|
import io.ktor.application.feature
|
||||||
import io.ktor.application.log
|
import io.ktor.application.log
|
||||||
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.Route
|
import io.ktor.routing.Route
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
import io.ktor.routing.RoutingPath.Companion.root
|
import io.ktor.routing.RoutingPath.Companion.root
|
||||||
|
import io.ktor.routing.get
|
||||||
import io.ktor.routing.routing
|
import io.ktor.routing.routing
|
||||||
import me.liuwj.ktorm.database.Database
|
import me.liuwj.ktorm.database.Database
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import org.kodein.di.generic.instance
|
|||||||
fun Application.authenticationModule() {
|
fun Application.authenticationModule() {
|
||||||
install(Authentication) {
|
install(Authentication) {
|
||||||
jwt {
|
jwt {
|
||||||
val simpleJwt: SimpleJWT by kodein.instance()
|
val simpleJwt by kodein.instance<SimpleJWT>()
|
||||||
verifier(simpleJwt.verifier)
|
verifier(simpleJwt.verifier)
|
||||||
validate {
|
validate {
|
||||||
UserIdPrincipal(it.payload.getClaim("name").asString())
|
UserIdPrincipal(it.payload.getClaim("name").asString())
|
||||||
|
|||||||
@ -1,8 +1,14 @@
|
|||||||
package be.vandewalleh.routing
|
package be.vandewalleh.routing
|
||||||
|
|
||||||
|
import io.ktor.auth.authenticate
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
|
import io.ktor.routing.route
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
|
|
||||||
fun Routing.chapters(kodein: Kodein) {
|
fun Routing.chapters(kodein: Kodein) {
|
||||||
|
authenticate {
|
||||||
|
route("/notes/{noteTitle}/chapters/{chapterNumber}") {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import io.ktor.request.receive
|
|||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
import io.ktor.routing.post
|
import io.ktor.routing.post
|
||||||
|
import io.ktor.routing.route
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
import org.kodein.di.generic.instance
|
import org.kodein.di.generic.instance
|
||||||
import org.mindrot.jbcrypt.BCrypt
|
import org.mindrot.jbcrypt.BCrypt
|
||||||
@ -19,17 +20,20 @@ fun Routing.login(kodein: Kodein) {
|
|||||||
|
|
||||||
data class TokenResponse(val token: String)
|
data class TokenResponse(val token: String)
|
||||||
|
|
||||||
post {
|
route("/login"){
|
||||||
val credential = call.receive<UsernamePasswordCredential>()
|
post {
|
||||||
|
val credential = call.receive<UsernamePasswordCredential>()
|
||||||
|
|
||||||
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
|
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
|
||||||
?: return@post call.respond(HttpStatusCode.Unauthorized)
|
?: return@post call.respond(HttpStatusCode.Unauthorized)
|
||||||
|
|
||||||
if (!BCrypt.checkpw(credential.password, password)) {
|
if (!BCrypt.checkpw(credential.password, password)) {
|
||||||
return@post call.respond(HttpStatusCode.Unauthorized)
|
return@post call.respond(HttpStatusCode.Unauthorized)
|
||||||
|
}
|
||||||
|
|
||||||
|
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return@post call.respond(TokenResponse(simpleJwt.sign(email)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ package be.vandewalleh.routing
|
|||||||
import be.vandewalleh.extensions.userId
|
import be.vandewalleh.extensions.userId
|
||||||
import be.vandewalleh.services.NotesService
|
import be.vandewalleh.services.NotesService
|
||||||
import io.ktor.application.call
|
import io.ktor.application.call
|
||||||
|
import io.ktor.auth.authenticate
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
import io.ktor.routing.get
|
import io.ktor.routing.get
|
||||||
@ -12,9 +13,11 @@ import org.kodein.di.generic.instance
|
|||||||
fun Routing.notes(kodein: Kodein) {
|
fun Routing.notes(kodein: Kodein) {
|
||||||
val notesService by kodein.instance<NotesService>()
|
val notesService by kodein.instance<NotesService>()
|
||||||
|
|
||||||
get {
|
authenticate {
|
||||||
val userId = call.userId()
|
get("/notes") {
|
||||||
val notes = notesService.getNotes(userId)
|
val userId = call.userId()
|
||||||
call.respond(notes)
|
val notes = notesService.getNotes(userId)
|
||||||
|
call.respond(notes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,7 @@ import org.mindrot.jbcrypt.BCrypt
|
|||||||
fun Routing.register(kodein: Kodein) {
|
fun Routing.register(kodein: Kodein) {
|
||||||
val userService by kodein.instance<UserService>()
|
val userService by kodein.instance<UserService>()
|
||||||
|
|
||||||
post {
|
post("/register") {
|
||||||
val user = call.receive<UserRegistrationDto>()
|
val user = call.receive<UserRegistrationDto>()
|
||||||
|
|
||||||
if (userService.userExists(user.username, user.email))
|
if (userService.userExists(user.username, user.email))
|
||||||
|
|||||||
@ -1,30 +1,12 @@
|
|||||||
package be.vandewalleh.routing
|
package be.vandewalleh.routing
|
||||||
|
|
||||||
import io.ktor.auth.authenticate
|
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
import io.ktor.routing.route
|
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
|
|
||||||
fun Routing.registerRoutes(kodein: Kodein) {
|
fun Routing.registerRoutes(kodein: Kodein) {
|
||||||
route("/login") {
|
login(kodein)
|
||||||
this@registerRoutes.login(kodein)
|
register(kodein)
|
||||||
}
|
notes(kodein)
|
||||||
|
title(kodein)
|
||||||
route("/register") {
|
chapters(kodein)
|
||||||
this@registerRoutes.register(kodein)
|
|
||||||
}
|
|
||||||
|
|
||||||
authenticate {
|
|
||||||
route("/notes") {
|
|
||||||
this@registerRoutes.notes(kodein)
|
|
||||||
|
|
||||||
route("/{noteTitle}") {
|
|
||||||
this@registerRoutes.title(kodein)
|
|
||||||
|
|
||||||
route("/chapters/{chapterNumber}") {
|
|
||||||
this@registerRoutes.chapters(kodein)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ package be.vandewalleh.routing
|
|||||||
import be.vandewalleh.extensions.*
|
import be.vandewalleh.extensions.*
|
||||||
import be.vandewalleh.services.NotesService
|
import be.vandewalleh.services.NotesService
|
||||||
import io.ktor.application.call
|
import io.ktor.application.call
|
||||||
|
import io.ktor.auth.authenticate
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.*
|
||||||
@ -12,48 +13,52 @@ import org.kodein.di.generic.instance
|
|||||||
fun Routing.title(kodein: Kodein) {
|
fun Routing.title(kodein: Kodein) {
|
||||||
val notesService by kodein.instance<NotesService>()
|
val notesService by kodein.instance<NotesService>()
|
||||||
|
|
||||||
post {
|
authenticate {
|
||||||
val userId = call.userId()
|
route("/notes/{noteTitle}") {
|
||||||
val title = call.parameters.noteTitle()
|
post {
|
||||||
val tags = call.receiveTags()
|
val userId = call.userId()
|
||||||
val noteId = call.parameters.noteId(userId)
|
val title = call.parameters.noteTitle()
|
||||||
|
val tags = call.receiveTags()
|
||||||
|
val noteId = call.parameters.noteId(userId)
|
||||||
|
|
||||||
if (noteId != null) {
|
if (noteId != null) {
|
||||||
return@post call.respondStatus(HttpStatusCode.Conflict)
|
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user