Start controller
This commit is contained in:
parent
1711dd835e
commit
68d8363ebf
@ -9,6 +9,7 @@ import be.vandewalleh.features.features
|
|||||||
import be.vandewalleh.migrations.Migration
|
import be.vandewalleh.migrations.Migration
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
import io.ktor.application.log
|
import io.ktor.application.log
|
||||||
|
import io.ktor.auth.authenticate
|
||||||
import io.ktor.routing.routing
|
import io.ktor.routing.routing
|
||||||
import me.liuwj.ktorm.database.Database
|
import me.liuwj.ktorm.database.Database
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
|
|||||||
28
api/src/controllers/AuthCrudController.kt
Normal file
28
api/src/controllers/AuthCrudController.kt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import io.ktor.application.ApplicationCall
|
||||||
|
import io.ktor.auth.UserIdPrincipal
|
||||||
|
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
|
||||||
|
|
||||||
|
abstract class AuthCrudController(
|
||||||
|
private val path: String,
|
||||||
|
override val kodein: Kodein
|
||||||
|
) :
|
||||||
|
KodeinController(kodein) {
|
||||||
|
|
||||||
|
abstract val route: Route.() -> Unit
|
||||||
|
|
||||||
|
fun ApplicationCall.userEmail(): String =
|
||||||
|
this.principal<UserIdPrincipal>()!!.name
|
||||||
|
|
||||||
|
override fun Routing.registerRoutes() {
|
||||||
|
route(path) {
|
||||||
|
route()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -14,5 +14,5 @@ val controllerModule = Kodein.Module(name = "Controller") {
|
|||||||
|
|
||||||
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
|
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
|
||||||
bind<KodeinController>().inSet() with singleton { HealthCheckController(this.kodein) }
|
bind<KodeinController>().inSet() with singleton { HealthCheckController(this.kodein) }
|
||||||
bind<KodeinController>().inSet() with singleton { NoteController(this.kodein) }
|
bind<KodeinController>().inSet() with singleton { NotesController(this.kodein) }
|
||||||
}
|
}
|
||||||
@ -1,27 +1,14 @@
|
|||||||
package be.vandewalleh.controllers
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
import io.ktor.application.Application
|
|
||||||
import io.ktor.locations.locations
|
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
import org.kodein.di.KodeinAware
|
import org.kodein.di.KodeinAware
|
||||||
import org.kodein.di.generic.instance
|
|
||||||
|
|
||||||
abstract class KodeinController(override val kodein: Kodein) : KodeinAware {
|
abstract class KodeinController(override val kodein: Kodein) : KodeinAware {
|
||||||
/**
|
|
||||||
* Injected dependency with the current [Application].
|
|
||||||
*/
|
|
||||||
val application: Application by instance()
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shortcut to get the url of a [TypedRoute].
|
|
||||||
*/
|
|
||||||
val TypedRoute.href get() = application.locations.href(this)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method that subtypes must override to register the handled [Routing] routes.
|
* Method that subtypes must override to register the handled [Routing] routes.
|
||||||
*/
|
*/
|
||||||
abstract fun Routing.registerRoutes()
|
abstract fun Routing.registerRoutes()
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TypedRoute
|
|
||||||
@ -1,73 +0,0 @@
|
|||||||
package be.vandewalleh.controllers
|
|
||||||
|
|
||||||
import be.vandewalleh.entities.Note
|
|
||||||
import be.vandewalleh.entities.Tag
|
|
||||||
import be.vandewalleh.errors.ApiError
|
|
||||||
import be.vandewalleh.tables.Notes
|
|
||||||
import be.vandewalleh.tables.Tags
|
|
||||||
import be.vandewalleh.tables.Users
|
|
||||||
import io.ktor.application.call
|
|
||||||
import io.ktor.auth.UserIdPrincipal
|
|
||||||
import io.ktor.auth.authenticate
|
|
||||||
import io.ktor.auth.principal
|
|
||||||
import io.ktor.http.HttpStatusCode
|
|
||||||
import io.ktor.locations.Location
|
|
||||||
import io.ktor.locations.post
|
|
||||||
import io.ktor.request.receive
|
|
||||||
import io.ktor.response.respond
|
|
||||||
import io.ktor.routing.Routing
|
|
||||||
import me.liuwj.ktorm.database.Database
|
|
||||||
import me.liuwj.ktorm.dsl.eq
|
|
||||||
import me.liuwj.ktorm.entity.add
|
|
||||||
import me.liuwj.ktorm.entity.find
|
|
||||||
import me.liuwj.ktorm.entity.sequenceOf
|
|
||||||
import org.kodein.di.Kodein
|
|
||||||
import org.kodein.di.generic.instance
|
|
||||||
|
|
||||||
class NoteController(kodein: Kodein) : KodeinController(kodein) {
|
|
||||||
private val db by instance<Database>()
|
|
||||||
|
|
||||||
override fun Routing.registerRoutes() {
|
|
||||||
authenticate {
|
|
||||||
post<Routes.Notes> {
|
|
||||||
data class Response(val message: String)
|
|
||||||
|
|
||||||
val (email) = call.principal<UserIdPrincipal>()!!
|
|
||||||
val body = call.receive<PostNotesBody>()
|
|
||||||
|
|
||||||
val user = db.sequenceOf(Users)
|
|
||||||
.find { Users.email eq email }
|
|
||||||
?: return@post call.respond(HttpStatusCode.Forbidden, ApiError.DeletedUserError)
|
|
||||||
|
|
||||||
val transaction = db.useTransaction {
|
|
||||||
|
|
||||||
val note = Note {
|
|
||||||
this.title = body.title
|
|
||||||
this.user = user
|
|
||||||
}
|
|
||||||
|
|
||||||
db.sequenceOf(Notes).add(note)
|
|
||||||
|
|
||||||
body.tags.forEach { tagName ->
|
|
||||||
val tag = Tag {
|
|
||||||
this.name = tagName
|
|
||||||
this.note = note
|
|
||||||
}
|
|
||||||
db.sequenceOf(Tags).add(tag)
|
|
||||||
}
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
return@post call.respond(Response("created"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
object Routes {
|
|
||||||
@Location("/notes")
|
|
||||||
class Notes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class PostNotesBody(val title: String, val content: String, val tags: List<String>)
|
|
||||||
25
api/src/controllers/NotesController.kt
Normal file
25
api/src/controllers/NotesController.kt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import be.vandewalleh.tables.Users
|
||||||
|
import io.ktor.application.call
|
||||||
|
import io.ktor.response.respond
|
||||||
|
import io.ktor.routing.Route
|
||||||
|
import io.ktor.routing.get
|
||||||
|
import me.liuwj.ktorm.database.Database
|
||||||
|
import me.liuwj.ktorm.dsl.eq
|
||||||
|
import me.liuwj.ktorm.entity.find
|
||||||
|
import me.liuwj.ktorm.entity.sequenceOf
|
||||||
|
import org.kodein.di.Kodein
|
||||||
|
import org.kodein.di.generic.instance
|
||||||
|
|
||||||
|
class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) {
|
||||||
|
private val db by kodein.instance<Database>()
|
||||||
|
|
||||||
|
override val route: Route.() -> Unit = {
|
||||||
|
get {
|
||||||
|
val email = call.userEmail()
|
||||||
|
val user = db.sequenceOf(Users).find { it.email eq email }!! // TODO
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -26,6 +26,8 @@ class UserController(kodein: Kodein) : KodeinController(kodein) {
|
|||||||
private val db by instance<Database>()
|
private val db by instance<Database>()
|
||||||
|
|
||||||
override fun Routing.registerRoutes() {
|
override fun Routing.registerRoutes() {
|
||||||
|
|
||||||
|
|
||||||
post<Routes.Login> {
|
post<Routes.Login> {
|
||||||
data class Response(val token: String)
|
data class Response(val token: String)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user