DONE !!
This commit is contained in:
parent
e4d8bffe71
commit
51cb7d9a33
14
api/http/test.http
Normal file
14
api/http/test.http
Normal 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}}
|
||||||
@ -2,6 +2,7 @@ package be.vandewalleh.controllers
|
|||||||
|
|
||||||
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.principal
|
import io.ktor.auth.principal
|
||||||
import io.ktor.routing.Route
|
import io.ktor.routing.Route
|
||||||
import io.ktor.routing.Routing
|
import io.ktor.routing.Routing
|
||||||
@ -20,8 +21,10 @@ abstract class AuthCrudController(
|
|||||||
this.principal<UserIdPrincipal>()!!.name
|
this.principal<UserIdPrincipal>()!!.name
|
||||||
|
|
||||||
override fun Routing.registerRoutes() {
|
override fun Routing.registerRoutes() {
|
||||||
route(path) {
|
authenticate {
|
||||||
route()
|
route(path) {
|
||||||
|
route()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +1,59 @@
|
|||||||
package be.vandewalleh.controllers
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import be.vandewalleh.tables.Notes
|
||||||
|
import be.vandewalleh.tables.Tags
|
||||||
import be.vandewalleh.tables.Users
|
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.database.Database
|
||||||
import me.liuwj.ktorm.dsl.eq
|
import me.liuwj.ktorm.dsl.*
|
||||||
import me.liuwj.ktorm.entity.find
|
|
||||||
import me.liuwj.ktorm.entity.sequenceOf
|
|
||||||
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 db by kodein.instance<Database>()
|
||||||
|
|
||||||
|
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 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,5 +9,5 @@ interface Note : Entity<Note> {
|
|||||||
val id: Int
|
val id: Int
|
||||||
var title: String
|
var title: String
|
||||||
var user: User
|
var user: User
|
||||||
var updatedAt: LocalDateTime?
|
var updatedAt: LocalDateTime
|
||||||
}
|
}
|
||||||
@ -12,4 +12,5 @@ object Chapters : Table<Chapter>("Chapters") {
|
|||||||
val number by int("number").bindTo { it.number }
|
val number by int("number").bindTo { it.number }
|
||||||
val content by text("content").bindTo { it.content }
|
val content by text("content").bindTo { it.content }
|
||||||
val noteId by int("note_id").references(Notes) { it.note }
|
val noteId by int("note_id").references(Notes) { it.note }
|
||||||
|
val note get() = noteId.referenceTable as Notes
|
||||||
}
|
}
|
||||||
@ -6,6 +6,7 @@ import me.liuwj.ktorm.schema.*
|
|||||||
object Notes : Table<Note>("Notes") {
|
object Notes : Table<Note>("Notes") {
|
||||||
val id by int("id").primaryKey().bindTo { it.id }
|
val id by int("id").primaryKey().bindTo { it.id }
|
||||||
val title by varchar("title").bindTo { it.title }
|
val title by varchar("title").bindTo { it.title }
|
||||||
val user by int("user_id").references(Users) { it.user }
|
val userId by int("user_id").references(Users) { it.user }
|
||||||
val updatedAt by datetime("last_viewed").bindTo { it.updatedAt }
|
val updatedAt by datetime("updated_at").bindTo { it.updatedAt }
|
||||||
|
val user get() = userId.referenceTable as Users
|
||||||
}
|
}
|
||||||
@ -9,4 +9,5 @@ object Tags : Table<Tag>("Tags") {
|
|||||||
val id by int("id").primaryKey().bindTo { it.id }
|
val id by int("id").primaryKey().bindTo { it.id }
|
||||||
val name by varchar("name").bindTo { it.name }
|
val name by varchar("name").bindTo { it.name }
|
||||||
val noteId by int("note_id").references(Notes) { it.note }
|
val noteId by int("note_id").references(Notes) { it.note }
|
||||||
|
val note get() = noteId.referenceTable as Notes
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user