From cfa3a7e2899efb71582bfc101c793cf454395b0c Mon Sep 17 00:00:00 2001 From: vinay Date: Wed, 24 Jan 2024 17:27:37 +0530 Subject: [PATCH] Excel to Db --- api.http | 14 +++++-- src/main/kotlin/com/restapi/Main.kt | 25 +++---------- .../com/restapi/controllers/Entities.kt | 29 +-------------- .../kotlin/com/restapi/controllers/Excel.kt | 37 ++++++++++++++++++- src/main/kotlin/com/restapi/domain/models.kt | 6 +-- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/api.http b/api.http index b0d12b8..de51065 100644 --- a/api.http +++ b/api.http @@ -97,12 +97,16 @@ Authorization: {{auth-token}} GET http://localhost:9001/api/vendor/product Authorization: {{auth-token}} +### get products by hsn code +GET http://localhost:9001/api/vendor/product/#123 +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 +### excel read +GET http://localhost:9001/api/vendor/product/validation Authorization: Bearer {{auth-token}} ### create product @@ -130,5 +134,9 @@ Authorization: {{auth-token}} ### delete a row -DELETE http://localhost:9001/api/vendor/product/2 +DELETE http://localhost:9001/api/vendor/product/#1 +Authorization: {{auth-token}} + +### +POST http://localhost:9001/api/vendor/product/import 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 b5525d5..a57e1be 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -199,28 +199,13 @@ 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")))) + patch("/{id}", ProductCtrl::patch, Roles(Role.Explicit(listOf("ROLE_PRODUCT_UPDATE", "ROLE_ADMIN")))) + put("/{id}", ProductCtrl::update, 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") - } - } + post("/import") {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 1994478..54203d9 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -17,6 +17,8 @@ import com.restapi.integ.Scripting import io.ebean.CallableSql import io.ebean.RawSqlBuilder import io.javalin.http.* +import org.apache.poi.ss.usermodel.Cell +import org.apache.poi.ss.usermodel.CellType import org.apache.poi.ss.usermodel.WorkbookFactory import org.slf4j.LoggerFactory import java.io.File @@ -457,33 +459,6 @@ 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/controllers/Excel.kt b/src/main/kotlin/com/restapi/controllers/Excel.kt index 89bce83..cb53d84 100644 --- a/src/main/kotlin/com/restapi/controllers/Excel.kt +++ b/src/main/kotlin/com/restapi/controllers/Excel.kt @@ -4,6 +4,11 @@ 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 com.restapi.domain.Session +import com.restapi.domain.Session.database +import com.restapi.domain.UOM +import io.ebean.text.json.JsonContext +import io.javalin.http.Context import org.apache.poi.ss.usermodel.Cell import org.apache.poi.ss.usermodel.CellType import org.apache.poi.ss.usermodel.Row @@ -57,7 +62,7 @@ val app_common_om = jacksonObjectMapper().apply { } fun ExcelRead(): String{ - val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\Untitled 1.xlsx") + val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\product-4.xlsx") val workbook = WorkbookFactory.create(inputStream) val workSheet = workbook.getSheetAt(0) var h = true @@ -148,4 +153,32 @@ fun ExcelRead(): String{ } } return app_common_om.writeValueAsString(resp) -} \ No newline at end of file +} + +fun excelToDb(): List { + val inputStream = FileInputStream("C:\\Users\\vinay\\IdeaProjects\\readymixerp_modules_api_git\\product-4.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 +} + diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 952489c..3367a39 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -325,10 +325,10 @@ open class ReqForQuote :BaseTenantModel() { var potentialVendors :List? = null @Enumerated(EnumType.STRING) var status :RFQStatus? = null -// @DbArray -// var docs :List? = null + @DbArray + var docs :List? = null @DbJsonB var products :List? = null - var reqForQuoteNum: String? = null + var reqForQuoteNum: String = "" var openTill: LocalDate? = null } \ No newline at end of file