move around classes

This commit is contained in:
gowthaman
2024-05-23 12:53:52 +05:30
parent ed1e7546a7
commit ce622a118f
10 changed files with 787 additions and 712 deletions

View File

@@ -0,0 +1,49 @@
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)
}
}