From 043cddcaa09d4d689c0e087b8f689b8bce429d51 Mon Sep 17 00:00:00 2001 From: vinay Date: Fri, 19 Jan 2024 10:42:50 +0530 Subject: [PATCH] product --- api.http | 44 ++++++++++++-- src/main/kotlin/com/restapi/Main.kt | 11 ++-- .../com/restapi/controllers/Entities.kt | 58 ++++++++++--------- src/main/kotlin/com/restapi/domain/models.kt | 1 + 4 files changed, 79 insertions(+), 35 deletions(-) diff --git a/api.http b/api.http index 5c75b21..f367f5e 100644 --- a/api.http +++ b/api.http @@ -5,14 +5,14 @@ Authorization: {{auth-token}} { "data": { - "number": "KA01HD6667", + "number": "KA01HD6677", "owner": "gowthaman" }, "uniqueIdentifier": "" } ### create row -POST http://localhost:9001/api/vendor +POST http://localhost:9001/api/vendor/po Content-Type: application/json Authorization: {{auth-token}} @@ -58,7 +58,7 @@ Authorization: Bearer {{auth-token}} ### query row POST http://localhost:9001/api/vehicle/query Content-Type: application/json -Authorization: set-auth-token +Authorization: {{set-auth-token}} { "sql": "select sys_pk, tenant_id, deleted_on, deleted_by, deleted, version, created_at, modified_at, created_by, modified_by, data, tags, comments, unique_identifier, entity_name from data_model where data ->> 'number' = :number", @@ -94,5 +94,41 @@ DELETE http://localhost:9001/api/vehicle/KA01HD6667 Authorization: {{auth-token}} ### get po for id -GET http://localhost:9001/api/vendor/po/12345 +GET http://localhost:9001/api/vendor/product +Authorization: {{auth-token}} + +### get product for id +GET http://localhost:9001/api/vendor/product/6 +Authorization: {{auth-token}} + +### get row +GET http://localhost:9001/api/vendor/product/7 +Authorization: Bearer {{auth-token}} + +### create product +POST http://localhost:9001/api/vendor/product +Content-Type: application/json +Authorization: {{auth-token}} + +{ + "name": "Shirt", + "description": "White Shirt", + "hsnCode": "WSM1XL" + +} + +### update field +PATCH http://localhost:9001/api/vendor/product/11 +Content-Type: application/json +Authorization: {{auth-token}} + + +### upate a row +PUT http://localhost:9001/api/vendor/product/11 +Content-Type: application/json +Authorization: {{auth-token}} + + +### delete a row +DELETE http://localhost:9001/api/vendor/product/2 Authorization: {{auth-token}} \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 2389d6a..c8ea14f 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -6,6 +6,7 @@ import com.restapi.config.AppConfig.Companion.appConfig import com.restapi.config.Auth.validateAuthToken import com.restapi.controllers.* import com.restapi.domain.DataNotFoundException +import com.restapi.domain.Product import com.restapi.domain.Session import com.restapi.domain.Session.currentTenant import com.restapi.domain.Session.currentUser @@ -135,10 +136,12 @@ fun main(args: Array) { delete("/{id}", Quotation::delete, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) } path("/product"){ - post("", Product::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - get("/{id}", Product::get, Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE")))) - delete("{id}", Product::delete, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - get("/doc/{id}", Product::getDoc, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_VIEW_DOC")))) + post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) + get("/{id}", ProductCtrl::get, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + put("/{id}", ProductCtrl::update, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "ROLE_ADMIN")))) + patch("/{id}", ProductCtrl::patch, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "ROLE_ADMIN")))) + delete("/{id}", ProductCtrl::delete, Roles(Role.Explicit(listOf("ROLE_PRODUCT_DELETE", "ROLE_ADMIN")))) + get("") {ctx -> ctx.json(Session.database.find(Product::class.java).findList())} } path("/doc"){ post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index 1aff54e..6dde2f4 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.restapi.domain.* +import com.restapi.domain.Product import com.restapi.domain.PurchaseOrder import com.restapi.domain.Quotation import com.restapi.domain.Session.currentUser @@ -411,6 +412,33 @@ object PurchaseOrder { } } +object ProductCtrl { + fun get(ctx :Context){ + val id = ctx.pathParam("id") + val product = database.find(Product::class.java, id) ?: throw NotFoundResponse("Product not found for $id") + + ctx.json(product) + } + fun create(ctx :Context){ + val product = ctx.bodyAsClass() + database.save(product) + } + + fun delete(ctx: Context) { + val id = ctx.pathParam("id") + val product = database.delete(Product::class.java, id) + } + + fun patch(ctx: Context) { + + } + + fun update(ctx: Context) { + val id = ctx.pathParam("id") + + } +} + object Quotation { fun get(ctx :Context){ val id = ctx.pathParam("id") @@ -452,36 +480,12 @@ object Quotation { fun reqForQuote(ctx :Context){ val reqForQuoteNum = ctx.pathParam(("rfqNum")) val rfq = database.find(ReqForQuote::class.java) - .where() - .eq("reqForQuoteNum", reqForQuoteNum) - ?: throw NotFoundResponse("request for quote not found for this quotation") + .where() + .eq("reqForQuoteNum", reqForQuoteNum) + ?: throw NotFoundResponse("request for quote not found for this quotation") ctx.json(rfq) } } -object Product { - fun get(ctx :Context){ - val id = ctx.pathParam("id") - val product = database.find(Product::class.java, id) ?: throw NotFoundResponse("product nor found for id $id") - ctx.json(product) - } - fun create(ctx :Context){ - val product = ctx.bodyAsClass() - database.save(product) - ctx.result("product created") - } - fun update(ctx :Context){ - - } - fun delete(ctx: Context){ - val id = ctx.pathParam(("id")) - val product = database.find(Product::class.java, id) ?:throw NotFoundResponse("product not found for id $id") - //product.delete() - ctx.result("product with id $id deleted") - } - fun getDoc(ctx :Context){ - - } -} object Document { fun get(ctx :Context){ val id = ctx.pathParam("id") diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index dab5b6b..63d8ae0 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -275,6 +275,7 @@ enum class UOM { } @Entity open class Product :BaseTenantModel() { + var id: Int? = null var name :String = "" var description :String = "" var hsnCode :String = ""