Move and rename more things
This commit is contained in:
parent
5a9bdaca73
commit
c387a2c4cf
@ -18,7 +18,7 @@ class ChaptersController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle
|
||||
return this.parameters["chapterNumber"]?.toIntOrNull()
|
||||
}
|
||||
|
||||
override fun Routing.registerAuthRoutes() {
|
||||
override fun Routing.routes() {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ import org.kodein.di.generic.instance
|
||||
class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) {
|
||||
private val notesService by kodein.instance<NotesService>()
|
||||
|
||||
override fun Routing.registerAuthRoutes() {
|
||||
override fun Routing.routes() {
|
||||
get {
|
||||
val userId = call.userId()
|
||||
val notes = notesService.getNotes(userId)
|
||||
|
||||
@ -4,8 +4,7 @@ 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() {
|
||||
|
||||
class RegisterController(kodein: Kodein) : KodeinController("", kodein) {
|
||||
override fun Routing.routes() {
|
||||
}
|
||||
}
|
||||
@ -59,7 +59,7 @@ class TitleController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle}",
|
||||
|
||||
private class PatchRequestBody(val title: String? = null, val tags: List<String>? = null)
|
||||
|
||||
override fun Routing.registerAuthRoutes() {
|
||||
override fun Routing.routes() {
|
||||
post {
|
||||
val title = call.noteTitle() ?: error("")
|
||||
val tags = call.receive<PostRequestBody>().tags
|
||||
|
||||
@ -3,36 +3,25 @@ package be.vandewalleh.controllers.base
|
||||
import be.vandewalleh.services.UserService
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.auth.UserIdPrincipal
|
||||
import io.ktor.auth.authenticate
|
||||
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.generic.instance
|
||||
|
||||
abstract class AuthCrudController(
|
||||
private val path: String,
|
||||
path: String,
|
||||
override val kodein: Kodein
|
||||
) :
|
||||
KodeinController(kodein) {
|
||||
KodeinController(path, kodein, auth = true) {
|
||||
|
||||
private val userService by instance<UserService>()
|
||||
|
||||
/**
|
||||
* retrieves the user email from the JWT token
|
||||
*/
|
||||
fun ApplicationCall.userEmail(): String =
|
||||
this.principal<UserIdPrincipal>()!!.name
|
||||
|
||||
fun ApplicationCall.userId(): Int =
|
||||
userService.getUserId(userEmail())!!
|
||||
|
||||
abstract fun Routing.registerAuthRoutes()
|
||||
|
||||
override fun Routing.registerRoutes() {
|
||||
authenticate {
|
||||
route(path) {
|
||||
this@registerRoutes.registerAuthRoutes()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,19 +1,40 @@
|
||||
package be.vandewalleh.controllers.base
|
||||
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.auth.authenticate
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Routing
|
||||
import io.ktor.routing.route
|
||||
import org.kodein.di.Kodein
|
||||
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) {
|
||||
this.respond(status, mapOf("message" to status.description))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user