Move and rename more things

This commit is contained in:
Hubert Van De Walle 2020-04-20 16:09:35 +02:00
parent 2bb3329742
commit 5a9bdaca73
5 changed files with 20 additions and 73 deletions

View File

@ -1,20 +1,9 @@
package be.vandewalleh.controllers package be.vandewalleh.controllers
import be.vandewalleh.controllers.base.AuthCrudController import be.vandewalleh.controllers.base.AuthCrudController
import be.vandewalleh.entities.User
import be.vandewalleh.tables.Chapters
import be.vandewalleh.tables.Notes
import be.vandewalleh.tables.Users
import io.ktor.application.ApplicationCall import io.ktor.application.ApplicationCall
import io.ktor.application.call import io.ktor.routing.Routing
import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.routing.Route
import io.ktor.routing.post
import me.liuwj.ktorm.database.Database import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
import me.liuwj.ktorm.entity.find
import me.liuwj.ktorm.entity.sequenceOf
import org.kodein.di.Kodein import org.kodein.di.Kodein
import org.kodein.di.generic.instance import org.kodein.di.generic.instance
@ -29,60 +18,7 @@ class ChaptersController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle
return this.parameters["chapterNumber"]?.toIntOrNull() return this.parameters["chapterNumber"]?.toIntOrNull()
} }
private fun ApplicationCall.chapterExists(): Boolean { override fun Routing.registerAuthRoutes() {
val user = user() TODO("Not yet implemented")
val title = noteTitle() ?: error("title missing")
val noteId = requestedNoteId() ?: error("")
return db.from(Chapters)
.select(Chapters.id)
.where { Notes.userId eq user.id and (Notes.id eq noteId) }
.limit(0, 1)
.map { it[Notes.id]!! }
.firstOrNull() != null
}
private fun ApplicationCall.user(): User {
return db.sequenceOf(Users)
.find { it.email eq this.userEmail() }
?: 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 data class PostRequestBody(val title: String, val content: String)
override val route: Route.() -> Unit = {
post {
val noteId = call.requestedNoteId()
?: return@post call.respondStatus(HttpStatusCode.NotFound)
val chapterNumber = call.chapterNumber()
?:return@post call.respondStatus(HttpStatusCode.BadRequest)
val exists = false
val (title, content) = call.receive<PostRequestBody>()
}
} }
} }

View File

@ -4,7 +4,7 @@ import be.vandewalleh.controllers.base.AuthCrudController
import be.vandewalleh.services.NotesService import be.vandewalleh.services.NotesService
import io.ktor.application.call import io.ktor.application.call
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Routing
import io.ktor.routing.get import io.ktor.routing.get
import org.kodein.di.Kodein import org.kodein.di.Kodein
import org.kodein.di.generic.instance import org.kodein.di.generic.instance
@ -12,7 +12,7 @@ import org.kodein.di.generic.instance
class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) { class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) {
private val notesService by kodein.instance<NotesService>() private val notesService by kodein.instance<NotesService>()
override val route: Route.() -> Unit = { override fun Routing.registerAuthRoutes() {
get { get {
val userId = call.userId() val userId = call.userId()
val notes = notesService.getNotes(userId) val notes = notesService.getNotes(userId)

View File

@ -0,0 +1,11 @@
package be.vandewalleh.controllers
import be.vandewalleh.controllers.base.KodeinController
import io.ktor.routing.Routing
import org.kodein.di.Kodein
class RegisterController(kodein: Kodein): KodeinController(kodein){
override fun Routing.registerRoutes() {
}
}

View File

@ -59,7 +59,7 @@ class TitleController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle}",
private class PatchRequestBody(val title: String? = null, val tags: List<String>? = null) private class PatchRequestBody(val title: String? = null, val tags: List<String>? = null)
override val route: Route.() -> Unit = { override fun Routing.registerAuthRoutes() {
post { post {
val title = call.noteTitle() ?: error("") val title = call.noteTitle() ?: error("")
val tags = call.receive<PostRequestBody>().tags val tags = call.receive<PostRequestBody>().tags

View File

@ -17,8 +17,6 @@ abstract class AuthCrudController(
) : ) :
KodeinController(kodein) { KodeinController(kodein) {
abstract val route: Route.() -> Unit
private val userService by instance<UserService>() private val userService by instance<UserService>()
fun ApplicationCall.userEmail(): String = fun ApplicationCall.userEmail(): String =
@ -27,10 +25,12 @@ abstract class AuthCrudController(
fun ApplicationCall.userId(): Int = fun ApplicationCall.userId(): Int =
userService.getUserId(userEmail())!! userService.getUserId(userEmail())!!
abstract fun Routing.registerAuthRoutes()
override fun Routing.registerRoutes() { override fun Routing.registerRoutes() {
authenticate { authenticate {
route(path) { route(path) {
route() this@registerRoutes.registerAuthRoutes()
} }
} }
} }