Move and rename more things

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

View File

@ -18,7 +18,7 @@ class ChaptersController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle
return this.parameters["chapterNumber"]?.toIntOrNull() return this.parameters["chapterNumber"]?.toIntOrNull()
} }
override fun Routing.registerAuthRoutes() { override fun Routing.routes() {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
} }

View File

@ -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 fun Routing.registerAuthRoutes() { override fun Routing.routes() {
get { get {
val userId = call.userId() val userId = call.userId()
val notes = notesService.getNotes(userId) val notes = notesService.getNotes(userId)

View File

@ -4,8 +4,7 @@ import be.vandewalleh.controllers.base.KodeinController
import io.ktor.routing.Routing import io.ktor.routing.Routing
import org.kodein.di.Kodein import org.kodein.di.Kodein
class RegisterController(kodein: Kodein): KodeinController(kodein){ class RegisterController(kodein: Kodein) : KodeinController("", kodein) {
override fun Routing.registerRoutes() { override fun Routing.routes() {
} }
} }

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 fun Routing.registerAuthRoutes() { override fun Routing.routes() {
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

@ -3,36 +3,25 @@ package be.vandewalleh.controllers.base
import be.vandewalleh.services.UserService import be.vandewalleh.services.UserService
import io.ktor.application.ApplicationCall import io.ktor.application.ApplicationCall
import io.ktor.auth.UserIdPrincipal import io.ktor.auth.UserIdPrincipal
import io.ktor.auth.authenticate
import io.ktor.auth.principal import io.ktor.auth.principal
import io.ktor.routing.Route
import io.ktor.routing.Routing
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
abstract class AuthCrudController( abstract class AuthCrudController(
private val path: String, path: String,
override val kodein: Kodein override val kodein: Kodein
) : ) :
KodeinController(kodein) { KodeinController(path, kodein, auth = true) {
private val userService by instance<UserService>() private val userService by instance<UserService>()
/**
* retrieves the user email from the JWT token
*/
fun ApplicationCall.userEmail(): String = fun ApplicationCall.userEmail(): String =
this.principal<UserIdPrincipal>()!!.name this.principal<UserIdPrincipal>()!!.name
fun ApplicationCall.userId(): Int = fun ApplicationCall.userId(): Int =
userService.getUserId(userEmail())!! userService.getUserId(userEmail())!!
abstract fun Routing.registerAuthRoutes()
override fun Routing.registerRoutes() {
authenticate {
route(path) {
this@registerRoutes.registerAuthRoutes()
} }
}
}
}

View File

@ -1,19 +1,40 @@
package be.vandewalleh.controllers.base package be.vandewalleh.controllers.base
import io.ktor.application.ApplicationCall import io.ktor.application.ApplicationCall
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.Routing import io.ktor.routing.Routing
import io.ktor.routing.route
import org.kodein.di.Kodein import org.kodein.di.Kodein
import org.kodein.di.KodeinAware import org.kodein.di.KodeinAware
abstract class KodeinController(override val kodein: Kodein) : KodeinAware { abstract class KodeinController(
private val path: String,
override val kodein: Kodein,
private val auth: Boolean = false
) : KodeinAware {
/** /**
* Method that subtypes must override to register the handled [Routing] routes. * Method that subtypes must override to declare their [Routing] routes.
*/ */
abstract fun Routing.registerRoutes() abstract fun Routing.routes()
fun Routing.registerRoutes() {
if (auth) {
authenticate {
route(path) {
this@registerRoutes.routes()
}
}
}
else {
route(path) {
this@registerRoutes.routes()
}
}
}
suspend fun ApplicationCall.respondStatus(status: HttpStatusCode) { suspend fun ApplicationCall.respondStatus(status: HttpStatusCode) {
this.respond(status, mapOf("message" to status.description)) this.respond(status, mapOf("message" to status.description))