Change NotesController to use services

This commit is contained in:
Hubert Van De Walle 2020-04-20 15:44:44 +02:00
parent 8dd4fc9f4d
commit d6f2489d50
2 changed files with 12 additions and 43 deletions

View File

@ -1,5 +1,6 @@
package be.vandewalleh.controllers package be.vandewalleh.controllers
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.authenticate
@ -8,6 +9,7 @@ import io.ktor.routing.Route
import io.ktor.routing.Routing import io.ktor.routing.Routing
import io.ktor.routing.route import io.ktor.routing.route
import org.kodein.di.Kodein import org.kodein.di.Kodein
import org.kodein.di.generic.instance
abstract class AuthCrudController( abstract class AuthCrudController(
private val path: String, private val path: String,
@ -17,9 +19,14 @@ abstract class AuthCrudController(
abstract val route: Route.() -> Unit abstract val route: Route.() -> Unit
private val userService by instance<UserService>()
fun ApplicationCall.userEmail(): String = fun ApplicationCall.userEmail(): String =
this.principal<UserIdPrincipal>()!!.name this.principal<UserIdPrincipal>()!!.name
fun ApplicationCall.userId(): Int =
userService.getUserId(userEmail())!!
override fun Routing.registerRoutes() { override fun Routing.registerRoutes() {
authenticate { authenticate {
route(path) { route(path) {

View File

@ -1,59 +1,21 @@
package be.vandewalleh.controllers package be.vandewalleh.controllers
import be.vandewalleh.tables.Notes import be.vandewalleh.services.NotesService
import be.vandewalleh.tables.Tags
import be.vandewalleh.tables.Users
import io.ktor.application.call import io.ktor.application.call
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.routing.get 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.Kodein
import org.kodein.di.generic.instance import org.kodein.di.generic.instance
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) { class NotesController(kodein: Kodein) : AuthCrudController("/notes", kodein) {
private val db by kodein.instance<Database>() private val notesService by kodein.instance<NotesService>()
private class ResponseItem(val title: String, val tags: List<String>, val updatedAt: String)
override val route: Route.() -> Unit = { override val route: Route.() -> Unit = {
get { get {
val email = call.userEmail() val userId = call.userId()
val notes = notesService.getNotes(userId)
val list = db.from(Notes) call.respond(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)
} }
} }
} }