This commit is contained in:
Hubert Van De Walle 2020-04-20 00:19:26 +02:00
parent 35ebaaa79f
commit a1cd1d7403

View File

@ -13,6 +13,7 @@ import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.routing.Routing
import io.ktor.routing.get
import io.ktor.routing.post
import me.liuwj.ktorm.database.Database
@ -35,6 +36,22 @@ class NotesTitleController(kodein: Kodein) : AuthCrudController("/notes/{noteTit
?: error("")
}
/**
* Method that returns a [Notes] ID from it's title and the currently logged in user.
* returns null if none found
*/
private fun ApplicationCall.requestedNoteId(): Int? {
val user = user()
val title = noteTitle() ?: error("title missing")
return db.from(Notes)
.select(Notes.id)
.where { Notes.userId eq user.id and (Notes.title eq title) }
.limit(0, 1)
.map { it[Notes.id]!! }
.firstOrNull()
}
private class PostRequestBody(val tags: List<String>)
private class ChapterDto(val title: String, val content: String)
@ -47,10 +64,7 @@ class NotesTitleController(kodein: Kodein) : AuthCrudController("/notes/{noteTit
val user = call.user()
val exists = db.sequenceOf(Notes)
.filter { it.userId eq user.id }
.filter { it.title eq title }
.firstOrNull() != null
val exists = call.requestedNoteId() != null
if (exists) {
return@post call.respondStatus(HttpStatusCode.Conflict)
@ -79,15 +93,7 @@ class NotesTitleController(kodein: Kodein) : AuthCrudController("/notes/{noteTit
}
get {
val user = call.user()
val title = call.noteTitle() ?: error("title missing")
val noteId = db.from(Notes)
.select(Notes.id)
.where { Notes.userId eq user.id and (Notes.title eq title) }
.limit(0, 1)
.map { it[Notes.id]!! }
.firstOrNull()
val noteId = call.requestedNoteId()
?: return@get call.respondStatus(HttpStatusCode.NotFound)
val tags = db.from(Tags)