Start migration of notesServices + controllers

This commit is contained in:
2020-04-25 17:24:43 +02:00
parent 573963b161
commit 52aae6773f
8 changed files with 130 additions and 106 deletions
-13
View File
@@ -1,13 +0,0 @@
package be.vandewalleh.routing
import io.ktor.auth.*
import io.ktor.routing.*
import org.kodein.di.Kodein
fun Routing.chapters(kodein: Kodein) {
authenticate {
route("/notes/{noteTitle}/chapters/{chapterNumber}") {
}
}
}
+18
View File
@@ -1,9 +1,13 @@
package be.vandewalleh.routing
import be.vandewalleh.extensions.noteTitle
import be.vandewalleh.extensions.receiveNoteCreate
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.NotesService
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import org.kodein.di.Kodein
@@ -18,5 +22,19 @@ fun Routing.notes(kodein: Kodein) {
val notes = notesService.getNotes(userId)
call.respond(notes)
}
post("/notes") {
val userId = call.userId()
val noteUuid = call.parameters.noteTitle()
val note = call.receiveNoteCreate()
val exists = notesService.noteExistsWithTitle(userId, note.title)
if (exists) {
return@post call.respondStatus(HttpStatusCode.Conflict)
}
notesService.createNote(userId, note.title, note.tags)
call.respondStatus(HttpStatusCode.Created)
}
}
}
-1
View File
@@ -8,6 +8,5 @@ fun Routing.registerRoutes(kodein: Kodein) {
login(kodein)
notes(kodein)
title(kodein)
chapters(kodein)
tags(kodein)
}
+22 -29
View File
@@ -1,6 +1,9 @@
package be.vandewalleh.routing
import be.vandewalleh.extensions.*
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.*
import io.ktor.auth.*
@@ -14,49 +17,39 @@ fun Routing.title(kodein: Kodein) {
val notesService by kodein.instance<NotesService>()
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)
}
notesService.createNote(userId, title, tags)
call.respondStatus(HttpStatusCode.Created)
}
route("/notes/{noteUuid}") {
get {
val userId = call.userId()
val noteId = call.parameters.noteId(userId)
?: return@get call.respondStatus(HttpStatusCode.NotFound)
val noteUuid = call.parameters.noteUuid()
val response = notesService.getTagsAndChapters(noteId)
val exists = notesService.noteExists(userId, noteUuid)
if (exists) return@get call.respondStatus(HttpStatusCode.NotFound)
val response = notesService.getNote(noteUuid)
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)
val noteUuid = call.parameters.noteUuid()
notesService.updateNote(noteId, notePatch.tags, notePatch.title)
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 noteId = call.parameters.noteId(userId)
?: return@delete call.respondStatus(HttpStatusCode.NotFound)
val noteUuid = call.parameters.noteUuid()
notesService.deleteNote(noteId)
val exists = notesService.noteExists(userId, noteUuid)
if (exists) return@delete call.respondStatus(HttpStatusCode.NotFound)
notesService.deleteNote(noteUuid)
call.respondStatus(HttpStatusCode.OK)
}
}