From d5566eca6444157cdf5c524523a5f26d99904f5d Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Fri, 30 Apr 2021 10:31:54 +0530 Subject: added more columns for gst, category and brand of product --- server/database/database.go | 59 +++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'server/database') diff --git a/server/database/database.go b/server/database/database.go index 8b7c7f6..b3c1e78 100644 --- a/server/database/database.go +++ b/server/database/database.go @@ -21,58 +21,75 @@ type Item struct { Desc string `json:"Description"` Price float64 HSN int + GST float64 + Cat string `json:"Category"` + Brand string } var myDatabase *sql.DB -var registered_items *sql.Stmt var register_item *sql.Stmt func init() { myDatabase, _ = sql.Open("sqlite3", "./openbills.db") - registered_items, _ = myDatabase.Prepare( + init_registered_items, _ := myDatabase.Prepare( `CREATE TABLE IF NOT EXISTS registered_items (id INTEGER PRIMARY KEY AUTOINCREMENT, - model TEXT NOT NULL, - desc TEXT, - price REAL, - hsn BLOB)`, + model TEXT NOT NULL, + desc TEXT, + price REAL, + hsn BLOB, + gst REAL, + category TEXT, + brand TEXT)`, ) - registered_items.Exec() + init_registered_items.Exec() register_item, _ = myDatabase.Prepare( `INSERT INTO registered_items - (model, desc, price, hsn) - VALUES (?, ?, ?, ?)`, + (model, desc, price, hsn, gst, category, brand) + VALUES (?, ?, ?, ?, ?, ?, ?)`, ) } func GetAllItems() []Item { var allItems []Item rows, _ := myDatabase.Query( - `SELECT model, desc, price, hsn FROM registered_items`, + `SELECT model, desc, price, hsn, gst, category, brand FROM registered_items`, ) var ( - model, desc string - price float64 + model, desc, cat, brand string + price, GST float64 HSN int ) for rows.Next() { - rows.Scan(&model, &desc, &price, &HSN) - allItems = append(allItems, Item{model, desc, price, HSN}) + rows.Scan(&model, &desc, &price, &HSN, &GST, &cat, &brand) + allItems = append(allItems, Item{model, desc, price, HSN, GST, cat, brand}) } return allItems } -func RegisterItem(model string, desc string, price float64, HSN int) { - /* - var item Item = Item{ - model, desc, price, HSN, +func RegisterItem(item Item) bool { + itemNames, _ := myDatabase.Query("SELECT model FROM registered_items") + + // check if item already exists + // probably this should be handled by front end + // so we can check this without need of using api + for itemNames.Next() { + var rModel string + itemNames.Scan(&rModel) + if rModel == item.Model { + return false + } } - register_item.Exec(item.Model, item.Desc, item.Price, item.HSN) - */ - register_item.Exec(model, desc, price, HSN) + register_item.Exec( + item.Model, item.Desc, + item.Price, item.HSN, + item.GST, item.Cat, + item.Brand, + ) + return true } -- cgit v1.2.3