This commit is contained in:
Hubert Van De Walle 2020-04-19 22:58:50 +02:00
parent e4d8bffe71
commit 51cb7d9a33
7 changed files with 63 additions and 9 deletions

14
api/http/test.http Normal file
View File

@ -0,0 +1,14 @@
# Register a new user
POST http://localhost:8081/login
Content-Type: application/json
{
"username": "hubert",
"password": "test"
}
> {% client.global.set("token", response.body.token); %}
### Get notes
GET http://localhost:8081/notes
Authorization: Bearer {{token}}

View File

@ -2,6 +2,7 @@ package be.vandewalleh.controllers
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
@ -20,8 +21,10 @@ abstract class AuthCrudController(
this.principal<UserIdPrincipal>()!!.name
override fun Routing.registerRoutes() {
route(path) {
route()
authenticate {
route(path) {
route()
}
}
}
}

View File

@ -1,25 +1,59 @@
package be.vandewalleh.controllers
import be.vandewalleh.tables.Notes
import be.vandewalleh.tables.Tags
import be.vandewalleh.tables.Users
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.eq
import me.liuwj.ktorm.entity.find
import me.liuwj.ktorm.entity.sequenceOf
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)
override val route: Route.() -> Unit = {
get {
val email = call.userEmail()
val user = db.sequenceOf(Users).find { it.email eq email }!! // TODO
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)
}
}
}

View File

@ -9,5 +9,5 @@ interface Note : Entity<Note> {
val id: Int
var title: String
var user: User
var updatedAt: LocalDateTime?
var updatedAt: LocalDateTime
}

View File

@ -12,4 +12,5 @@ object Chapters : Table<Chapter>("Chapters") {
val number by int("number").bindTo { it.number }
val content by text("content").bindTo { it.content }
val noteId by int("note_id").references(Notes) { it.note }
val note get() = noteId.referenceTable as Notes
}

View File

@ -6,6 +6,7 @@ import me.liuwj.ktorm.schema.*
object Notes : Table<Note>("Notes") {
val id by int("id").primaryKey().bindTo { it.id }
val title by varchar("title").bindTo { it.title }
val user by int("user_id").references(Users) { it.user }
val updatedAt by datetime("last_viewed").bindTo { it.updatedAt }
val userId by int("user_id").references(Users) { it.user }
val updatedAt by datetime("updated_at").bindTo { it.updatedAt }
val user get() = userId.referenceTable as Users
}

View File

@ -9,4 +9,5 @@ object Tags : Table<Tag>("Tags") {
val id by int("id").primaryKey().bindTo { it.id }
val name by varchar("name").bindTo { it.name }
val noteId by int("note_id").references(Notes) { it.note }
val note get() = noteId.referenceTable as Notes
}