Change NotesController to use services
This commit is contained in:
parent
8dd4fc9f4d
commit
d6f2489d50
@ -1,5 +1,6 @@
|
||||
package be.vandewalleh.controllers
|
||||
|
||||
import be.vandewalleh.services.UserService
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.auth.UserIdPrincipal
|
||||
import io.ktor.auth.authenticate
|
||||
@ -8,6 +9,7 @@ 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,
|
||||
@ -17,9 +19,14 @@ abstract class AuthCrudController(
|
||||
|
||||
abstract val route: Route.() -> Unit
|
||||
|
||||
private val userService by instance<UserService>()
|
||||
|
||||
fun ApplicationCall.userEmail(): String =
|
||||
this.principal<UserIdPrincipal>()!!.name
|
||||
|
||||
fun ApplicationCall.userId(): Int =
|
||||
userService.getUserId(userEmail())!!
|
||||
|
||||
override fun Routing.registerRoutes() {
|
||||
authenticate {
|
||||
route(path) {
|
||||
|
||||
@ -1,59 +1,21 @@
|
||||
package be.vandewalleh.controllers
|
||||
|
||||
import be.vandewalleh.tables.Notes
|
||||
import be.vandewalleh.tables.Tags
|
||||
import be.vandewalleh.tables.Users
|
||||
import be.vandewalleh.services.NotesService
|
||||
import io.ktor.application.call
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.response.respondText
|
||||
import io.ktor.routing.Route
|
||||
import io.ktor.routing.get
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.instance
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) {
|
||||
private val db by kodein.instance<Database>()
|
||||
|
||||
private class ResponseItem(val title: String, val tags: List<String>, val updatedAt: String)
|
||||
private val notesService by kodein.instance<NotesService>()
|
||||
|
||||
override val route: Route.() -> Unit = {
|
||||
get {
|
||||
val email = call.userEmail()
|
||||
|
||||
val list = db.from(Notes)
|
||||
.leftJoin(Users, on = Users.id eq Notes.userId)
|
||||
.select(Notes.id, Notes.title, Notes.updatedAt)
|
||||
.where { Users.email eq email }
|
||||
.orderBy(Notes.updatedAt.desc())
|
||||
.map { row ->
|
||||
Notes.createEntity(row)
|
||||
}
|
||||
.toList()
|
||||
|
||||
val response = mutableListOf<ResponseItem>()
|
||||
|
||||
list.forEach { note ->
|
||||
val tags = db.from(Tags)
|
||||
.select(Tags.name)
|
||||
.where { Tags.noteId eq note.id }
|
||||
.map { it[Tags.name]!! }
|
||||
.toList()
|
||||
|
||||
val updatedAt = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(note.updatedAt)
|
||||
|
||||
val item = ResponseItem(
|
||||
title = note.title,
|
||||
tags = tags,
|
||||
updatedAt = updatedAt
|
||||
)
|
||||
response += item
|
||||
}
|
||||
|
||||
call.respond(response)
|
||||
val userId = call.userId()
|
||||
val notes = notesService.getNotes(userId)
|
||||
call.respond(notes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user