From 438daacdc6f4e18db6ccf321b33ed92d8aabb03a Mon Sep 17 00:00:00 2001 From: vinay Date: Fri, 19 Jan 2024 17:47:18 +0530 Subject: [PATCH 1/8] ProductFilters --- src/main/kotlin/com/restapi/controllers/Excel.kt | 4 ++-- src/main/kotlin/com/restapi/controllers/Filters.kt | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 84e8f45..d5c4851 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -1,9 +1,9 @@ package com.restapi.controllers -import org.apache.poi + import java.io.FileOutputStream enum class DataType { QUOTE, PO, VENDOR } fun CreateExcel(cols :List, excelFor :DataType) { - val wb = HSSFWorkbook() + //val wb = HSSFWorkbook() } \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/controllers/Filters.kt b/src/main/kotlin/com/restapi/controllers/Filters.kt index 1dce9c8..0d89cf3 100644 --- a/src/main/kotlin/com/restapi/controllers/Filters.kt +++ b/src/main/kotlin/com/restapi/controllers/Filters.kt @@ -1,10 +1,8 @@ package com.restapi.controllers -import com.restapi.domain.DocType +import com.restapi.domain.* import com.restapi.domain.PurchaseOrder import com.restapi.domain.Quotation -import com.restapi.domain.ReqForQuote -import com.restapi.domain.UOM import java.time.LocalDate import com.restapi.domain.Session.database @@ -135,4 +133,13 @@ fun searchRFQ(commonFilters: CommonFilters, rfqFilters: RFQFilters) : List { + val p = database.find(Product::class.java) + .where() + .ilike("hsnCode", productFilters.hsnLike) + .ilike("Pname", productFilters.nameLike) + applySortHelper(p, commonFilters.sortBy, commonFilters.sortAsc) + return p.findList() } \ No newline at end of file From 8745db21272b4e31fd1bce309e8010cb2f630e43 Mon Sep 17 00:00:00 2001 From: vinay Date: Mon, 22 Jan 2024 15:12:58 +0530 Subject: [PATCH 2/8] Excel R/W --- build.gradle.kts | 4 +- src/main/kotlin/com/restapi/Main.kt | 1 - .../kotlin/com/restapi/controllers/Excel.kt | 47 +++++++++++++++++-- src/main/kotlin/com/restapi/domain/models.kt | 1 + 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2894a75..821aee9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -36,8 +36,8 @@ dependencies { implementation("org.yaml:snakeyaml:2.2") implementation("io.minio:minio:8.5.7") implementation("org.apache.httpcomponents:httpclient:4.5.14") - implementation("org.apache.poi:poi:5.0.0") - implementation("org.apache.poi:poi-ooxml:5.0.0") + implementation("org.apache.poi:poi:5.2.3") + implementation("org.apache.poi:poi-ooxml:5.2.3") api ("net.cactusthorn.config:config-core:0.81") api ("net.cactusthorn.config:config-yaml:0.81") kapt("net.cactusthorn.config:config-compiler:0.81") diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 6bed341..0c97abc 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -7,7 +7,6 @@ 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 import com.restapi.domain.Session.objectMapper diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index d5c4851..91b1f41 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -1,9 +1,46 @@ package com.restapi.controllers -import java.io.FileOutputStream -enum class DataType { - QUOTE, PO, VENDOR +import com.restapi.domain.Product +import org.apache.poi.ss.usermodel.Cell +import org.apache.poi.ss.usermodel.Row +import org.apache.poi.ss.usermodel.WorkbookFactory +import org.apache.poi.xssf.usermodel.XSSFWorkbook +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.FileInputStream +import java.io.InputStream + +fun CreateExcel(productList: List): InputStream { + val wb = XSSFWorkbook() + val sh = wb.createSheet() + sh.createRow(0).createCell(0).setCellValue("Name") + sh.createRow(0).createCell(1).setCellValue("Description") + sh.createRow(0).createCell(2).setCellValue("HSN") + sh.createRow(0).createCell(3).setCellValue("UOM") + + var rowNum = 1 + for (product in productList) { + val row: Row = sh.createRow(rowNum++) + + row.createCell(0).setCellValue(product.name) + row.createCell(1).setCellValue(product.description) + row.createCell(2).setCellValue(product.hsnCode) + + val uomCell: Cell = row.createCell(3) + uomCell.setCellValue(product.uom?.name ?: "") + } + + val baos = ByteArrayOutputStream() + wb.write(baos) + wb.close() + + return ByteArrayInputStream(baos.toByteArray()) + } -fun CreateExcel(cols :List, excelFor :DataType) { - //val wb = HSSFWorkbook() + +fun ExcelRead(filename: String){ + val inputStream = FileInputStream("./${filename}.xlsx") + val workbook = WorkbookFactory.create(inputStream) + val workSheet = workbook.getSheetAt(0) + } \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 2753361..a9258b6 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -9,6 +9,7 @@ import io.ebean.annotation.* import io.ebean.annotation.Index import java.time.LocalDate import java.time.LocalDateTime +import java.util.* import javax.persistence.* data class Comments(val text: String = "", val by: String = "", val at: LocalDateTime = LocalDateTime.now()) From a3f76149795d6320bd08b508b97fb5f581e88abc Mon Sep 17 00:00:00 2001 From: vinay Date: Tue, 23 Jan 2024 16:59:25 +0530 Subject: [PATCH 3/8] Product Excel Validation --- api.http | 10 +- src/main/kotlin/com/restapi/Main.kt | 4 + .../com/restapi/controllers/Entities.kt | 11 +- .../kotlin/com/restapi/controllers/Excel.kt | 116 ++++++++++++++++-- 4 files changed, 127 insertions(+), 14 deletions(-) diff --git a/api.http b/api.http index 69bd308..b0d12b8 100644 --- a/api.http +++ b/api.http @@ -93,16 +93,16 @@ Authorization: {{auth-token}} DELETE http://localhost:9001/api/vehicle/KA01HD6667 Authorization: {{auth-token}} -### get po for id +### get products GET http://localhost:9001/api/vendor/product Authorization: {{auth-token}} -### get product for id -GET http://localhost:9001/api/vendor/product/7 +### create excel for products +POST http://localhost:9001/api/vendor/product/product-excel Authorization: {{auth-token}} -### get row -GET http://localhost:9001/api/vendor/product/7 +### get +GET http://localhost:9001/api/vendor/product/product-import Authorization: Bearer {{auth-token}} ### create product diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 0c97abc..42fe33e 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -7,6 +7,7 @@ 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.a import com.restapi.domain.Session.currentTenant import com.restapi.domain.Session.currentUser import com.restapi.domain.Session.objectMapper @@ -24,6 +25,7 @@ import io.javalin.http.util.RateLimitUtil import io.javalin.json.JavalinJackson import org.jose4j.jwt.consumer.InvalidJwtException import org.slf4j.LoggerFactory +import java.io.InputStream import java.security.MessageDigest import java.time.LocalDateTime import java.util.* @@ -140,6 +142,8 @@ fun main(args: Array) { //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("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + post("/product-excel", ProductCtrl::prodExcel) + get("/product-import"){ ctx -> ctx.json(ExcelRead())} } 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 8654c34..60cc77f 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -18,6 +18,8 @@ import io.ebean.CallableSql import io.ebean.RawSqlBuilder import io.javalin.http.* import org.slf4j.LoggerFactory +import java.io.File +import java.io.InputStream import java.sql.Types import java.time.LocalDate import java.time.LocalDateTime @@ -446,9 +448,14 @@ object ProductCtrl { } fun update(ctx: Context) { - val id = ctx.pathParam("id") } + fun prodExcel(it: Context) { + val product = database.find(Product::class.java).findList() + it.result(CreateExcel(product)).contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + .header("Content-Disposition", "attachment; filename=\"product.xlsx\"") + } + } object Quotation { @@ -590,4 +597,4 @@ object RequestForQuote { //shuld we compare the new body fields with preexisting ones and prepare a sql query to update those fields?? } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 91b1f41..1478b08 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -1,7 +1,11 @@ package com.restapi.controllers +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.restapi.domain.Product import org.apache.poi.ss.usermodel.Cell +import org.apache.poi.ss.usermodel.CellType import org.apache.poi.ss.usermodel.Row import org.apache.poi.ss.usermodel.WorkbookFactory import org.apache.poi.xssf.usermodel.XSSFWorkbook @@ -13,11 +17,12 @@ import java.io.InputStream fun CreateExcel(productList: List): InputStream { val wb = XSSFWorkbook() val sh = wb.createSheet() - sh.createRow(0).createCell(0).setCellValue("Name") - sh.createRow(0).createCell(1).setCellValue("Description") - sh.createRow(0).createCell(2).setCellValue("HSN") - sh.createRow(0).createCell(3).setCellValue("UOM") - + val rows: Row = sh.createRow(0) + rows.createCell(0).setCellValue("Name") + rows.createCell(1).setCellValue("Description") + rows.createCell(2).setCellValue("HSN") + rows.createCell(3).setCellValue("UOM") + var rowNum = 1 for (product in productList) { val row: Row = sh.createRow(rowNum++) @@ -38,9 +43,106 @@ fun CreateExcel(productList: List): InputStream { } -fun ExcelRead(filename: String){ - val inputStream = FileInputStream("./${filename}.xlsx") +data class validateExcel( + val name: String, + val description: String, + val hsnCode: String, + val ok: Boolean, + val err: String, +) + +val app_common_om = jacksonObjectMapper().apply { + registerModule(JavaTimeModule()) + configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) +} + +fun ExcelRead(): String{ + val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") val workbook = WorkbookFactory.create(inputStream) val workSheet = workbook.getSheetAt(0) + var h = false + //Header check +// if(workSheet.getRow(0).getCell(0).stringCellValue.equals("Name")){ +// if(workSheet.getRow(0).getCell(1).stringCellValue.equals("Description")){ +// if(workSheet.getRow(0).getCell(2).stringCellValue.equals("HSN")){ +// if(workSheet.getRow(0).getCell(3).stringCellValue.equals("UOM")){ +// return true +// } +// } +// } +// } + val resp = arrayListOf() + workSheet.rowIterator().forEach { row -> + + if (row == null) return@forEach + + if (h) { + + val pName = row.getCell(0).run { + when { + this == null -> "" + this.cellType == CellType.STRING -> this.stringCellValue + else -> "" + } + } + + val pDesc = row.getCell(1).run { + when { + this == null -> "" + this.cellType == CellType.STRING -> this.stringCellValue + else -> "" + } + } + val pHsn = row.getCell(2).run { + when { + this == null -> "" + this.cellType == CellType.STRING -> this.stringCellValue + else -> "" + } + } + + if (pName.isEmpty() && pDesc.isEmpty() && pHsn.isEmpty()) { + return@forEach + } + if (pName.isEmpty()) { + resp.add( + validateExcel( + name = pName, + description = pDesc, + hsnCode = pHsn, + ok = false, + err = "Product name is required" + ) + ) + return@forEach + } + if (pDesc.isEmpty()) { + resp.add( + validateExcel( + name = pName, + description = pDesc, + hsnCode = pHsn, + ok = false, + err = "Product description is required" + ) + ) + return@forEach + } + if (pHsn.isEmpty()) { + resp.add( + validateExcel( + name = pName, + description = pDesc, + hsnCode = pHsn, + ok = false, + err = "Product HSN is required" + ) + ) + return@forEach + } + } + h = true + } + return app_common_om.writeValueAsString(resp) } \ No newline at end of file From 976aebec5bfbba322c29256b9baea54f6524b9ba Mon Sep 17 00:00:00 2001 From: vinay Date: Tue, 23 Jan 2024 18:18:55 +0530 Subject: [PATCH 4/8] Product Excel Validation --- .../kotlin/com/restapi/controllers/Excel.kt | 151 +++++++++--------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 1478b08..89bce83 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -60,89 +60,92 @@ fun ExcelRead(): String{ val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") val workbook = WorkbookFactory.create(inputStream) val workSheet = workbook.getSheetAt(0) - var h = false + var h = true //Header check -// if(workSheet.getRow(0).getCell(0).stringCellValue.equals("Name")){ -// if(workSheet.getRow(0).getCell(1).stringCellValue.equals("Description")){ -// if(workSheet.getRow(0).getCell(2).stringCellValue.equals("HSN")){ -// if(workSheet.getRow(0).getCell(3).stringCellValue.equals("UOM")){ -// return true -// } -// } -// } -// } + if(workSheet.getRow(0).getCell(0).stringCellValue.equals("Name")) { + if (workSheet.getRow(0).getCell(1).stringCellValue.equals("Description")) { + if (workSheet.getRow(0).getCell(2).stringCellValue.equals("HSN")) { + if (workSheet.getRow(0).getCell(3).stringCellValue.equals("UOM")) { + h = false + }else return "Header UOM mismatch" + }else return "Header-HSN mismatch" + }else return "Header-Desc mismatch" + }else return "Header-Name mismatch" val resp = arrayListOf() - workSheet.rowIterator().forEach { row -> - if (row == null) return@forEach + if(h==false) { + workSheet.rowIterator().forEach { row -> - if (h) { + if (row == null) return@forEach - val pName = row.getCell(0).run { - when { - this == null -> "" - this.cellType == CellType.STRING -> this.stringCellValue - else -> "" + if (h) { + + val pName = row.getCell(0).run { + when { + this == null -> "" + this.cellType == CellType.STRING -> this.stringCellValue + else -> "" + } + } + + val pDesc = row.getCell(1).run { + when { + this == null -> "" + this.cellType == CellType.STRING -> this.stringCellValue + else -> "" + } + } + val pHsn = row.getCell(2).run { + when { + this == null -> "" + this.cellType == CellType.STRING -> this.stringCellValue + else -> "" + } + } + + if (pName.isEmpty() && pDesc.isEmpty() && pHsn.isEmpty()) { + return@forEach + } + if (pName.isEmpty()) { + resp.add( + validateExcel( + name = pName, + description = pDesc, + hsnCode = pHsn, + ok = false, + err = "Product name is required" + ) + ) + return@forEach + } + if (pDesc.isEmpty()) { + resp.add( + validateExcel( + name = pName, + description = pDesc, + hsnCode = pHsn, + ok = false, + err = "Product description is required" + ) + ) + return@forEach + } + if (pHsn.isEmpty()) { + resp.add( + validateExcel( + name = pName, + description = pDesc, + hsnCode = pHsn, + ok = false, + err = "Product HSN is required" + ) + ) + return@forEach } } - - val pDesc = row.getCell(1).run { - when { - this == null -> "" - this.cellType == CellType.STRING -> this.stringCellValue - else -> "" - } - } - val pHsn = row.getCell(2).run { - when { - this == null -> "" - this.cellType == CellType.STRING -> this.stringCellValue - else -> "" - } - } - - if (pName.isEmpty() && pDesc.isEmpty() && pHsn.isEmpty()) { - return@forEach - } - if (pName.isEmpty()) { - resp.add( - validateExcel( - name = pName, - description = pDesc, - hsnCode = pHsn, - ok = false, - err = "Product name is required" - ) - ) - return@forEach - } - if (pDesc.isEmpty()) { - resp.add( - validateExcel( - name = pName, - description = pDesc, - hsnCode = pHsn, - ok = false, - err = "Product description is required" - ) - ) - return@forEach - } - if (pHsn.isEmpty()) { - resp.add( - validateExcel( - name = pName, - description = pDesc, - hsnCode = pHsn, - ok = false, - err = "Product HSN is required" - ) - ) - return@forEach - } + h = true } - h = true } return app_common_om.writeValueAsString(resp) } \ No newline at end of file From d23ac5261dc0a0ac4033ec4c26f9b4f2d759f47c Mon Sep 17 00:00:00 2001 From: vinay Date: Wed, 24 Jan 2024 11:25:18 +0530 Subject: [PATCH 5/8] Excel to Db --- src/main/kotlin/com/restapi/Main.kt | 209 +++++++++++++----- .../com/restapi/controllers/Entities.kt | 32 ++- src/main/kotlin/com/restapi/domain/models.kt | 1 + 3 files changed, 182 insertions(+), 60 deletions(-) diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 42fe33e..b5525d5 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -113,80 +113,173 @@ fun main(args: Array) { it.json(mapOf("status" to true)) } - path("/vendor"){ - path("/"){ + path("/vendor") { + path("/") { post("", Vendor::create, Roles(Role.Explicit(listOf("ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) - get("", Vendor::get, Roles(Role.Explicit(listOf("ROLE_VENDOR_VIEW", "ROLE_VENDOR_CREATE", "ROLE_ADMIN")))) - get("quotes/{id}", Vendor::getQuotes, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_QUOTE_VIEW", "ROLE_QUOTE_CREATE", "ROLE_VENDOR_VIEW")))) - get("pos/{id}", Vendor::getPos, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_PO_VIEW", "ROLE_PO_CREATE`")))) + get( + "", + Vendor::get, + Roles(Role.Explicit(listOf("ROLE_VENDOR_VIEW", "ROLE_VENDOR_CREATE", "ROLE_ADMIN"))) + ) + get( + "quotes/{id}", + Vendor::getQuotes, + Roles( + Role.Explicit( + listOf( + "ROLE_ADMIN", + "ROLE_QUOTE_VIEW", + "ROLE_QUOTE_CREATE", + "ROLE_VENDOR_VIEW" + ) + ) + ) + ) + get( + "pos/{id}", + Vendor::getPos, + Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_PO_VIEW", "ROLE_PO_CREATE`"))) + ) put("/rate/{id}/{rating}", Vendor::rate, Roles(Role.Explicit(listOf("ROLE_VENDOR_CREATE")))) } - path("/po"){ + path("/po") { post("", PurchaseOrder::create, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_ADMIN")))) - get("/{id}", PurchaseOrder::get, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_PO_VIEW", "ROLE_QUOTE_CREATE")))) - put("/approve/{id}", PurchaseOrder::approve, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_APPROVE")))) - put("/reject/{id}", PurchaseOrder::reject, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_APPROVE")))) - get("/refQuote/{id}", PurchaseOrder::quoteReference, Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_PO_VIEW")))) + get( + "/{id}", + PurchaseOrder::get, + Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_PO_VIEW", "ROLE_QUOTE_CREATE"))) + ) + put( + "/approve/{id}", + PurchaseOrder::approve, + Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_APPROVE"))) + ) + put( + "/reject/{id}", + PurchaseOrder::reject, + Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_APPROVE"))) + ) + get( + "/refQuote/{id}", + PurchaseOrder::quoteReference, + Roles(Role.Explicit(listOf("ROLE_PO_CREATE", "ROLE_PO_VIEW"))) + ) } - path("/quote"){ + path("/quote") { post("", Quotation::create, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) - get("/{id}", Quotation::get, Roles(Role.Explicit(listOf("ROLE_QUOTE_VIEW", "ROLE_ADMIN", "ROLE_PO_CREATE", "ROLE_QUOTE_CREATE")))) - get("/po/{id}", Quotation::generatePO, Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_PO_CRETE")))) - get("/rfq/{rfqNum}", Quotation::reqForQuote, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_QUOTE_VIEW")))) - delete("/{id}", Quotation::delete, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) + get( + "/{id}", + Quotation::get, + Roles( + Role.Explicit( + listOf( + "ROLE_QUOTE_VIEW", + "ROLE_ADMIN", + "ROLE_PO_CREATE", + "ROLE_QUOTE_CREATE" + ) + ) + ) + ) + get( + "/po/{id}", + Quotation::generatePO, + Roles(Role.Explicit(listOf("ROLE_ADMIN", "ROLE_PO_CRETE"))) + ) + get( + "/rfq/{rfqNum}", + Quotation::reqForQuote, + Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_QUOTE_VIEW"))) + ) + delete( + "/{id}", + Quotation::delete, + Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN"))) + ) } - path("/product"){ + path("/product") { post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) //get("/{hsnCode}", ProductCtrl::get, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) - put("/{id}", ProductCtrl::update, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "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")))) + delete( + "/{id}", + ProductCtrl::delete, + Roles(Role.Explicit(listOf("ROLE_PRODUCT_DELETE", "ROLE_ADMIN"))) + ) get("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) post("/product-excel", ProductCtrl::prodExcel) - get("/product-import"){ ctx -> ctx.json(ExcelRead())} + get("/product-import") { ctx -> //ctx.json(ExcelRead())} + val fileItem = ctx.uploadedFiles("file") + if (fileItem != null) { + ctx.result("Data imported successfully!") + } else { + ctx.result("No file uploaded") + } + } } - path("/doc"){ - post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) - //why type and refid are clubbed ?? - get("/{type}/{refId}", Document::getWithRefId, Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE")))) - get("/{id}", Document::get, Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE")))) - get("/print/{id}", Document::print, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_DOC_VIEW")))) - delete("/{id}", Document::delete, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE")))) - } - path("/reqForQuote"){ - post("", RequestForQuote::create, Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE")))) - get("/{id}", RequestForQuote::get, Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE", "ROLE_RFQ_VIEW")))) - put("/{id}", RequestForQuote::update, Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE")))) + path("/doc") { + post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) + //why type and refid are clubbed ?? + get( + "/{type}/{refId}", + Document::getWithRefId, + Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE"))) + ) + get( + "/{id}", + Document::get, + Roles(Role.Explicit(listOf("ROLE_DOC_VIEW", "ROLE_ADMIN", "ROLE_PRODUCT_CREATE"))) + ) + get( + "/print/{id}", + Document::print, + Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_DOC_VIEW"))) + ) + delete("/{id}", Document::delete, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE")))) + } + path("/reqForQuote") { + post("", RequestForQuote::create, Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE")))) + get( + "/{id}", + RequestForQuote::get, + Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE", "ROLE_RFQ_VIEW"))) + ) + put("/{id}", RequestForQuote::update, Roles(Role.Explicit(listOf("ROLE_RFQ_CREATE")))) + } } + post("/script/database/{name}", Entities::executeStoredProcedure, Roles(adminRole, Role.DbOps)) + post("/script/{file}/{name}", Entities::executeScript, Roles(adminRole, Role.DbOps)) + + get("/{entity}/{id}", Entities::view, Roles(adminRole, viewRole)) + post("/{entity}/query/{id}", Entities::sqlQueryById, Roles(adminRole, viewRole)) + post("/{entity}/query", Entities::sqlQueryRaw, Roles(adminRole, viewRole)) + post("/{entity}", Entities::create, Roles(adminRole, createRole)) + + put("/{entity}/approve/{id}", Entities::approve, Roles(adminRole, approveOrRejectRole)) + put("/{entity}/reject/{id}", Entities::reject, Roles(adminRole, approveOrRejectRole)) + put("/{entity}/{action}/{id}", Entities::action, Roles(adminRole, Role.Entity)) + + put("/{entity}/{id}", Entities::update, Roles(adminRole, updateRole)) + patch("/{entity}/{id}", Entities::patch, Roles(adminRole, updateRole)) + delete("/{entity}/{id}", Entities::delete, Roles(adminRole, Role.Standard(Action.DELETE))) } - post("/script/database/{name}", Entities::executeStoredProcedure, Roles(adminRole, Role.DbOps)) - post("/script/{file}/{name}", Entities::executeScript, Roles(adminRole, Role.DbOps)) - - get("/{entity}/{id}", Entities::view, Roles(adminRole, viewRole)) - post("/{entity}/query/{id}", Entities::sqlQueryById, Roles(adminRole, viewRole)) - post("/{entity}/query", Entities::sqlQueryRaw, Roles(adminRole, viewRole)) - post("/{entity}", Entities::create, Roles(adminRole, createRole)) - - put("/{entity}/approve/{id}", Entities::approve, Roles(adminRole, approveOrRejectRole)) - put("/{entity}/reject/{id}", Entities::reject, Roles(adminRole, approveOrRejectRole)) - put("/{entity}/{action}/{id}", Entities::action, Roles(adminRole, Role.Entity)) - - put("/{entity}/{id}", Entities::update, Roles(adminRole, updateRole)) - patch("/{entity}/{id}", Entities::patch, Roles(adminRole, updateRole)) - delete("/{entity}/{id}", Entities::delete, Roles(adminRole, Role.Standard(Action.DELETE))) } + .exception(DuplicateKeyException::class.java, Exceptions.dupKeyExceptionHandler) + .exception(DataIntegrityException::class.java, Exceptions.dataIntegrityException) + .exception(DataNotFoundException::class.java, Exceptions.dataNotFoundException) + .exception(IllegalArgumentException::class.java, Exceptions.illegalArgumentException) + .exception(JsonMappingException::class.java, Exceptions.jsonMappingException) + .exception(InvalidJwtException::class.java, Exceptions.invalidJwtException) + .start(appConfig.portNumber()) } - .exception(DuplicateKeyException::class.java, Exceptions.dupKeyExceptionHandler) - .exception(DataIntegrityException::class.java, Exceptions.dataIntegrityException) - .exception(DataNotFoundException::class.java, Exceptions.dataNotFoundException) - .exception(IllegalArgumentException::class.java, Exceptions.illegalArgumentException) - .exception(JsonMappingException::class.java, Exceptions.jsonMappingException) - .exception(InvalidJwtException::class.java, Exceptions.invalidJwtException) - .start(appConfig.portNumber()) -} -private fun Context.getAuthHeader() = header("Authorization") - ?.replace("Bearer ", "") - ?.replace("Bearer: ", "") - ?.trim() + private fun Context.getAuthHeader() = header("Authorization") + ?.replace("Bearer ", "") + ?.replace("Bearer: ", "") + ?.trim() diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index 60cc77f..1994478 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -17,8 +17,10 @@ import com.restapi.integ.Scripting import io.ebean.CallableSql import io.ebean.RawSqlBuilder import io.javalin.http.* +import org.apache.poi.ss.usermodel.WorkbookFactory import org.slf4j.LoggerFactory import java.io.File +import java.io.FileInputStream import java.io.InputStream import java.sql.Types import java.time.LocalDate @@ -416,7 +418,6 @@ object PurchaseOrder { data class ProductSearch( var isSort: String? = null - ) object ProductCtrl { @@ -429,7 +430,7 @@ object ProductCtrl { fun getAll(ctx: Context){ val productList = Session.database.find(Product::class.java) .findList() - .sortedBy { it.hsnCode } + //.sortedBy { it.hsnCode } ctx.json(productList) } @@ -456,6 +457,33 @@ object ProductCtrl { .header("Content-Disposition", "attachment; filename=\"product.xlsx\"") } + data class ProductList( + val name: String, + val description: String, + val hsnCode: String, + val uom: String?, + ) + + fun excelToDb(it: Context){ + val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") + val workbook = WorkbookFactory.create(inputStream) + val workSheet = workbook.getSheetAt(0) + + val dataList = mutableListOf() + + for (row in workSheet) { + val cell1Value = row.getCell(0).stringCellValue + val cell2Value = row.getCell(1).stringCellValue + val cell3Value = row.getCell(2).stringCellValue + val cell4Value = row.getCell(3).stringCellValue + + val data = ProductList(cell1Value, cell2Value, cell3Value, cell4Value) + dataList.add(data) + + } + database.saveAll(dataList) + } + } object Quotation { diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index a9258b6..952489c 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -273,6 +273,7 @@ open class PurchaseOrder :BaseTenantModel() { enum class UOM { NOS, LTR, MTR, ALL } + @Entity open class Product :BaseTenantModel() { var id: Int? = null From 471c043218a939b95d148cf1c8e9637aacefc90a Mon Sep 17 00:00:00 2001 From: vinay Date: Wed, 24 Jan 2024 17:49:30 +0530 Subject: [PATCH 6/8] merge complete --- src/main/kotlin/com/restapi/Main.kt | 25 +++++++++-- .../kotlin/com/restapi/controllers/Excel.kt | 41 +++++++++++++++---- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index cf14702..4da6e62 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -141,13 +141,30 @@ fun main(args: Array) { get("/rfq/{rfqNum}", QuotationCtrl::reqForQuote, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_QUOTE_VIEW")))) delete("/{id}", QuotationCtrl::delete, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) } - path("/product"){ - post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN", "ROLE_VENDOR_CREATE")))) + path("/product") { + post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) //get("/{hsnCode}", ProductCtrl::get, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) - put("/{id}", ProductCtrl::update, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "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")))) + delete( + "/{id}", + ProductCtrl::delete, + Roles(Role.Explicit(listOf("ROLE_PRODUCT_DELETE", "ROLE_ADMIN"))) + ) get("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + post("/product-excel", ProductCtrl::prodExcel) + get("/product-import") { ctx -> //ctx.json(ExcelRead())} + val fileItem = ctx.uploadedFiles("file") + if (fileItem != null) { + ctx.result("Data imported successfully!") + } else { + ctx.result("No file uploaded") + } + } } path("/doc"){ post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 039bcc4..83a5d2d 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -14,15 +14,10 @@ import com.restapi.domain.Session.database import com.restapi.domain.Vendor import org.apache.poi.hssf.usermodel.DVConstraint import org.apache.poi.hssf.usermodel.HSSFDataValidation -import org.apache.poi.ss.usermodel.Cell -import org.apache.poi.ss.usermodel.CellType -import org.apache.poi.ss.usermodel.DateUtil -import org.apache.poi.ss.usermodel.Workbook -import org.apache.poi.ss.usermodel.WorkbookFactory +import org.apache.poi.ss.usermodel.* import org.apache.poi.ss.util.CellRangeAddressList -import java.io.File -import java.io.FileInputStream -import java.io.FileOutputStream +import org.apache.poi.xssf.usermodel.XSSFWorkbook +import java.io.* import java.text.SimpleDateFormat import java.time.LocalDate import java.time.ZoneId @@ -526,4 +521,34 @@ fun ExcelRead(): String{ } } return app_common_om.writeValueAsString(resp) +} + + +fun CreateExcel(productList: List): InputStream { + val wb = XSSFWorkbook() + val sh = wb.createSheet() + val rows: Row = sh.createRow(0) + rows.createCell(0).setCellValue("Name") + rows.createCell(1).setCellValue("Description") + rows.createCell(2).setCellValue("HSN") + rows.createCell(3).setCellValue("UOM") + + var rowNum = 1 + for (product in productList) { + val row: Row = sh.createRow(rowNum++) + + row.createCell(0).setCellValue(product.name) + row.createCell(1).setCellValue(product.description) + row.createCell(2).setCellValue(product.hsnCode) + + val uomCell: Cell = row.createCell(3) + uomCell.setCellValue(product.uom?.name ?: "") + } + + val baos = ByteArrayOutputStream() + wb.write(baos) + wb.close() + + return ByteArrayInputStream(baos.toByteArray()) + } \ No newline at end of file From 4688b92314a6287df93d5363895cceadfb9e94ba Mon Sep 17 00:00:00 2001 From: vinay Date: Wed, 24 Jan 2024 18:33:01 +0530 Subject: [PATCH 7/8] ExcelToDb complete --- api.http | 3 ++ src/main/kotlin/com/restapi/Main.kt | 26 ++++------------- .../kotlin/com/restapi/controllers/Excel.kt | 29 ++++++++++++++++++- .../kotlin/com/restapi/controllers/Filters.kt | 6 +++- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/api.http b/api.http index 72763d3..c122e9a 100644 --- a/api.http +++ b/api.http @@ -359,3 +359,6 @@ Authorization: {{auth-token}} "quoteFilters": {} } +### +POST http://localhost:9001/api/vendor/product/excelDb +Authorization: {{auth-token}} diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 4da6e62..5576b34 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -143,28 +143,14 @@ fun main(args: Array) { } path("/product") { post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - //get("/{hsnCode}", 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("/{hsnCode}", 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("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) post("/product-excel", ProductCtrl::prodExcel) - get("/product-import") { ctx -> //ctx.json(ExcelRead())} - val fileItem = ctx.uploadedFiles("file") - if (fileItem != null) { - ctx.result("Data imported successfully!") - } else { - ctx.result("No file uploaded") - } - } + get("/product-import") { ctx -> ctx.json(ExcelRead()) } + post("/excelDb") {ctx -> ctx.json(excelToDb())} } path("/doc"){ post("", Document::create, Roles(Role.Explicit(listOf("ROLE_DOC_CREATE", "ROLE_ADMIN")))) diff --git a/src/main/kotlin/com/restapi/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 83a5d2d..f23b09d 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -387,7 +387,7 @@ fun ImportFromExcel(fileType: FileType, filePath : String) { "nos" -> UOM.NOS "ltr" -> UOM.LTR "mtr" -> UOM.MTR - else -> UOM.ALL + else -> UOM.LTR } } } @@ -551,4 +551,31 @@ fun CreateExcel(productList: List): InputStream { return ByteArrayInputStream(baos.toByteArray()) +} + +fun excelToDb(): List { + val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") + val workbook = WorkbookFactory.create(inputStream) + val workSheet = workbook.getSheetAt(0) + + for (row in workSheet) { + val cell1Value = row.getCell(0).stringCellValue + val cell2Value = row.getCell(1).stringCellValue + val cell3Value = row.getCell(2).stringCellValue + val cell4Value = row?.getCell(3)?.stringCellValue + + val prod = Product() + prod.name = cell1Value + prod.description = cell2Value + prod.hsnCode = cell3Value + prod.uom = when(cell4Value) { + "nos" -> UOM.NOS + "ltr" -> UOM.LTR + "mtr" -> UOM.MTR + else -> UOM.ALL + } + database.saveAll(prod) + } + val productList = Session.database.find(Product::class.java).findList() + return productList } \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/controllers/Filters.kt b/src/main/kotlin/com/restapi/controllers/Filters.kt index ec909b6..8676dc6 100644 --- a/src/main/kotlin/com/restapi/controllers/Filters.kt +++ b/src/main/kotlin/com/restapi/controllers/Filters.kt @@ -29,10 +29,14 @@ data class POFilters ( val validBefore: LocalDate = maxDate, val refQuotation :String = IGNORE, ) : CustomFilters + +enum class UOMFilter { + ALL //fixme: later +} data class ProductFilters ( val nameLike :String = IGNORE, val hsnLike :String = IGNORE, - val uom :UOM = UOM.ALL, + val uom :UOMFilter = UOMFilter.ALL, ) : CustomFilters data class DocumentFilters ( val nameLike :String = IGNORE, From 7e8f70a7c239bee64582229314cd94805a9e3649 Mon Sep 17 00:00:00 2001 From: vinay Date: Mon, 5 Feb 2024 09:42:06 +0530 Subject: [PATCH 8/8] UI --- api.http | 76 +++++++++---------- src/main/kotlin/com/restapi/Main.kt | 16 ++-- .../com/restapi/controllers/Entities.kt | 12 +-- 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/api.http b/api.http index c122e9a..79fbf82 100644 --- a/api.http +++ b/api.http @@ -93,46 +93,6 @@ Authorization: {{auth-token}} DELETE http://localhost:9001/api/vehicle/KA01HD6667 Authorization: {{auth-token}} -### get products -GET http://localhost:9001/api/vendor/product -Authorization: {{auth-token}} - -### create excel for products -POST http://localhost:9001/api/vendor/product/product-excel -Authorization: {{auth-token}} - -### get -GET http://localhost:9001/api/vendor/product/product-import -Authorization: Bearer {{auth-token}} - -### create product -POST http://localhost:9001/api/vendor/product -Content-Type: application/json -Authorization: {{auth-token}} - -{ - "name": "Shirt", - "description": "Black Shirt", - "hsnCode": "BSM1XL" - -} - -### 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}} - ### create vendor POST http://localhost:9001/api/vendor/ Content-Type: application/json @@ -359,6 +319,38 @@ Authorization: {{auth-token}} "quoteFilters": {} } -### -POST http://localhost:9001/api/vendor/product/excelDb +### CREATE PRODUCT +POST http://localhost:9001/api/vendor/product/create +Content-Type: application/json Authorization: {{auth-token}} + +{ + "name" : "aa", + "description": "aa", + "hsnCode" : "aa", + "uom": "NOS" +} + +### GET ALL PRODUCTS +GET http://localhost:9001/api/vendor/product/getAll +Authorization: {{auth-token}} + +### GET PRODUCT BY KEY +GET http://localhost:9001/api/vendor/product/of/6 +Authorization: {{auth-token}} + +### GET PRODUCT BY HSNCODE +DELETE http://localhost:9001/api/vendor/product/3 +Authorization: {{auth-token}} + +### PRODUCT EXCEl +GET http://localhost:9001/api/vendor/product/to/excel +Authorization: {{auth-token}} + +### EXCEL VALIDATE +GET http://localhost:9001/api/vendor/product/valid/excel +Authorization: {{auth-token}} + +### EXCEL TO DB (IMPORT) +POST http://localhost:9001/api/vendor/product/excelToDb +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 5576b34..f0261d8 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -19,6 +19,7 @@ import io.javalin.Javalin import io.javalin.apibuilder.ApiBuilder.* import io.javalin.http.ContentType import io.javalin.http.Context +import io.javalin.http.HandlerType import io.javalin.http.UnauthorizedResponse import io.javalin.http.util.NaiveRateLimit import io.javalin.http.util.RateLimitUtil @@ -32,7 +33,6 @@ import java.util.* import java.util.concurrent.TimeUnit import kotlin.jvm.optionals.getOrDefault - fun main(args: Array) { val logger = LoggerFactory.getLogger("api") val adminRole = Role.Standard(Action.ADMIN) @@ -80,6 +80,8 @@ fun main(args: Array) { TimeUnit.MINUTES ) + if(ctx.method() == HandlerType.OPTIONS) return@before + val authToken = ctx.getAuthHeader() ?: throw UnauthorizedResponse() @@ -142,15 +144,15 @@ fun main(args: Array) { delete("/{id}", QuotationCtrl::delete, Roles(Role.Explicit(listOf("ROLE_QUOTE_CREATE", "ROLE_ADMIN")))) } path("/product") { - post("", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) - get("/{hsnCode}", ProductCtrl::get, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + post("/create", ProductCtrl::create, Roles(Role.Explicit(listOf("ROLE_PRODUCT_CREATE", "ROLE_ADMIN")))) + get("/getAll", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) + get("/of/{key}", 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("", ProductCtrl::getAll, Roles(Role.Explicit(listOf("ROLE_PRODUCT_VIEW", "ROLE_ADMIN")))) - post("/product-excel", ProductCtrl::prodExcel) - get("/product-import") { ctx -> ctx.json(ExcelRead()) } - post("/excelDb") {ctx -> ctx.json(excelToDb())} + get("/to/excel", ProductCtrl::prodExcel) + get("/valid/excel") { ctx -> ctx.json(ExcelRead()) } + post("/excelToDb") {ctx -> ctx.json(excelToDb())} } 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 25aa06c..228c367 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -444,15 +444,15 @@ data class ProductSearch( object ProductCtrl { fun get(ctx :Context){ - val hsnCode = ctx.pathParam("hsnCode") - val product = database.find(Product::class.java, hsnCode) ?: throw NotFoundResponse("Product not found for $hsnCode") + val key = ctx.pathParam("key") + val product = database.find(Product::class.java, key) ?: throw NotFoundResponse("Product not found for $key") ctx.json(product) } fun getAll(ctx: Context){ - val productList = Session.database.find(Product::class.java) + val productList = database.find(Product::class.java) .findList() - //.sortedBy { it.hsnCode } + //.removeAt(4) ctx.json(productList) } @@ -464,7 +464,7 @@ object ProductCtrl { fun delete(ctx: Context) { val id = ctx.pathParam("id") - val product = database.delete(Product::class.java, id) + val product = database.deletePermanent(Product::class.java,id) } fun patch(ctx: Context) { @@ -472,7 +472,7 @@ object ProductCtrl { } fun update(ctx: Context) { - + //have to implement } fun prodExcel(it: Context) { val product = database.find(Product::class.java).findList()