Add create note

This commit is contained in:
Hubert Van De Walle 2020-04-19 23:27:43 +02:00
parent 2a142504a5
commit 0335c1eb08
4 changed files with 91 additions and 1 deletions

View File

@ -11,4 +11,16 @@ Content-Type: application/json
### Get notes ### Get notes
GET http://localhost:8081/notes GET http://localhost:8081/notes
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
### Create a note
POST http://localhost:8081/notes/babar
Content-Type: application/json
Authorization: Bearer {{token}}
{
"tags": [
"Test",
"Dev"
]
}

View File

@ -15,4 +15,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 { NotesController(this.kodein) } bind<KodeinController>().inSet() with singleton { NotesController(this.kodein) }
bind<KodeinController>().inSet() with singleton { NotesTitleController(this.kodein) }
} }

View File

@ -1,5 +1,8 @@
package be.vandewalleh.controllers package be.vandewalleh.controllers
import io.ktor.application.ApplicationCall
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
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
@ -11,4 +14,8 @@ abstract class KodeinController(override val kodein: Kodein) : KodeinAware {
* 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()
suspend fun ApplicationCall.respondStatus(status: HttpStatusCode) {
this.respond(status, mapOf("message" to status.description))
}
} }

View File

@ -0,0 +1,70 @@
package be.vandewalleh.controllers
import be.vandewalleh.entities.Note
import be.vandewalleh.entities.Tag
import be.vandewalleh.tables.Notes
import be.vandewalleh.tables.Tags
import be.vandewalleh.tables.Users
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.routing.post
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.eq
import me.liuwj.ktorm.entity.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
import java.time.LocalDateTime
class NotesTitleController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle}", kodein) {
private val db by kodein.instance<Database>()
private fun ApplicationCall.noteTitle(): String? {
return this.parameters["noteTitle"]!!
}
private class PostRequestBody(val tags: List<String>)
override val route: Route.() -> Unit = {
post {
val title = call.noteTitle() ?: error("")
val tags = call.receive<PostRequestBody>().tags
val user = db.sequenceOf(Users)
.find { it.email eq call.userEmail() } ?: error("")
val exists = db.sequenceOf(Notes)
.filter { it.userId eq user.id }
.filter { it.title eq title }
.firstOrNull() != null
if (exists) {
return@post call.respondStatus(HttpStatusCode.Conflict)
}
db.useTransaction {
val note = Note {
this.title = title
this.user = user
this.updatedAt = LocalDateTime.now()
}
db.sequenceOf(Notes).add(note)
tags.forEach { tagName ->
val tag = Tag {
this.note = note
this.name = tagName
}
db.sequenceOf(Tags).add(tag)
}
}
call.respondStatus(HttpStatusCode.Created)
}
}
}