From 4688b92314a6287df93d5363895cceadfb9e94ba Mon Sep 17 00:00:00 2001 From: vinay Date: Wed, 24 Jan 2024 18:33:01 +0530 Subject: [PATCH] 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,