49 lines
1.5 KiB
Kotlin
49 lines
1.5 KiB
Kotlin
package com.restapi.controllers
|
|
|
|
import com.restapi.domain.DocType
|
|
import com.restapi.domain.Document
|
|
import com.restapi.domain.Session
|
|
import io.javalin.http.Context
|
|
import io.javalin.http.HttpStatus
|
|
import io.javalin.http.NotFoundResponse
|
|
import io.javalin.http.bodyAsClass
|
|
|
|
object DocumentCtrl {
|
|
fun get(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val doc = Session.database.find(Document::class.java, id) ?: throw NotFoundResponse("no doc found with id $id")
|
|
ctx.status(HttpStatus.OK)
|
|
ctx.json(doc)
|
|
}
|
|
|
|
fun create(ctx: Context) {
|
|
val doc = ctx.bodyAsClass<Document>()
|
|
Session.database.save(doc)
|
|
ctx.status(HttpStatus.CREATED)
|
|
ctx.json(doc)
|
|
}
|
|
|
|
fun print(ctx: Context) {
|
|
//would be handled in the frontend ??
|
|
}
|
|
|
|
fun delete(ctx: Context) {
|
|
val id = ctx.pathParam("id")
|
|
val doc = Session.database.find(Document::class.java, id) ?: throw NotFoundResponse("no document found with id $id")
|
|
Session.database.delete(doc)
|
|
ctx.status(HttpStatus.OK)
|
|
}
|
|
|
|
fun getWithRefId(ctx: Context) {
|
|
//fetches a particular doc (po, quote) with ref id
|
|
val refId = ctx.pathParam("refId")
|
|
val doc = Session.database.find(Document::class.java)
|
|
.where()
|
|
.eq("typeOfDoc", DocType.valueOf(ctx.pathParam("type")))
|
|
.eq("refIdOfDoc", refId)
|
|
?: throw NotFoundResponse("no doc found for refId $refId")
|
|
ctx.status(HttpStatus.OK)
|
|
ctx.json(doc)
|
|
}
|
|
|
|
} |