Lots of things

This commit is contained in:
2020-06-22 17:34:45 +02:00
parent f134a83604
commit 3306b16f91
21 changed files with 482 additions and 344 deletions
+6 -1
View File
@@ -1,5 +1,10 @@
package be.vandewalleh.routing
import be.vandewalleh.routing.notes.notes
import be.vandewalleh.routing.notes.tags
import be.vandewalleh.routing.notes.title
import be.vandewalleh.routing.user.auth
import be.vandewalleh.routing.user.user
import io.ktor.routing.*
import org.kodein.di.Kodein
@@ -9,4 +14,4 @@ fun Routing.registerRoutes(kodein: Kodein) {
notes(kodein)
title(kodein)
tags(kodein)
}
}
-55
View File
@@ -1,55 +0,0 @@
package be.vandewalleh.routing
import be.vandewalleh.extensions.noteUuid
import be.vandewalleh.extensions.receiveNotePatch
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId
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.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
fun Routing.title(kodein: Kodein) {
val notesService by kodein.instance<NotesService>()
authenticate {
route("/notes/{noteUuid}") {
get {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
val response =
notesService.getNote(userId, noteUuid) ?: return@get call.respondStatus(HttpStatusCode.NotFound)
call.respond(response)
}
patch {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
val exists = notesService.noteExists(userId, noteUuid)
if (!exists) return@patch call.respondStatus(HttpStatusCode.NotFound)
val notePatch = call.receiveNotePatch().copy(uuid = noteUuid)
notesService.updateNote(notePatch)
call.respondStatus(HttpStatusCode.OK)
}
delete {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
val exists = notesService.noteExists(userId, noteUuid)
if (!exists) return@delete call.respondStatus(HttpStatusCode.NotFound)
notesService.deleteNote(noteUuid)
call.respondStatus(HttpStatusCode.OK)
}
}
}
}
+53
View File
@@ -0,0 +1,53 @@
package be.vandewalleh.routing.notes
import be.vandewalleh.entities.Note
import be.vandewalleh.extensions.noteUuid
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.NoteService
import io.ktor.application.call
import io.ktor.auth.authenticate
import io.ktor.http.HttpStatusCode
import io.ktor.request.*
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 noteService by kodein.instance<NoteService>()
authenticate {
route("/notes/{noteUuid}") {
get {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
val response = noteService.find(userId, noteUuid)
?: return@get call.respondStatus(HttpStatusCode.NotFound)
call.respond(response)
}
put {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
val note = call.receive<Note>()
note.uuid = noteUuid
if (noteService.updateNote(userId, note))
call.respondStatus(HttpStatusCode.OK)
else call.respondStatus(HttpStatusCode.NotFound)
}
delete {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
if (noteService.delete(userId, noteUuid))
call.respondStatus(HttpStatusCode.OK)
else
call.respondStatus(HttpStatusCode.NotFound)
}
}
}
}
@@ -1,31 +1,32 @@
package be.vandewalleh.routing
package be.vandewalleh.routing.notes
import be.vandewalleh.extensions.receiveNoteCreate
import be.vandewalleh.entities.Note
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.NotesService
import be.vandewalleh.services.NoteService
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
fun Routing.notes(kodein: Kodein) {
val notesService by kodein.instance<NotesService>()
val noteService by kodein.instance<NoteService>()
authenticate {
get("/notes") {
val userId = call.userId()
val notes = notesService.getNotes(userId)
val notes = noteService.findAll(userId)
call.respond(notes)
}
post("/notes") {
val userId = call.userId()
val note = call.receiveNoteCreate()
val uuid = notesService.createNote(userId, note)
call.respond(HttpStatusCode.Created, mapOf("uuid" to uuid))
val note = call.receive<Note>()
val createdNote = noteService.create(userId, note)
call.respond(HttpStatusCode.Created, createdNote)
}
}
}
@@ -1,7 +1,7 @@
package be.vandewalleh.routing
package be.vandewalleh.routing.notes
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.NotesService
import be.vandewalleh.services.NoteService
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.response.*
@@ -10,11 +10,11 @@ import org.kodein.di.Kodein
import org.kodein.di.generic.instance
fun Routing.tags(kodein: Kodein) {
val notesService by kodein.instance<NotesService>()
val noteService by kodein.instance<NoteService>()
authenticate {
get("/tags") {
call.respond(notesService.getTags(call.userId()))
call.respond(noteService.getTags(call.userId()))
}
}
}
}
@@ -1,4 +1,4 @@
package be.vandewalleh.routing
package be.vandewalleh.routing.user
import be.vandewalleh.auth.SimpleJWT
import be.vandewalleh.auth.UserDbIdPrincipal
@@ -1,4 +1,4 @@
package be.vandewalleh.routing
package be.vandewalleh.routing.user
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId