From 0fa16426f56120c4eb18bc18c202703c460daed8 Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Sat, 1 May 2021 08:22:47 +0530 Subject: divided package database into multiple files for ease --- server/database/database.go | 95 -------------------------------------------- server/database/items.go | 74 ++++++++++++++++++++++++++++++++++ server/database/main.go | 33 +++++++++++++++ server/main.go | 1 + server/openbills.db | Bin 20480 -> 0 bytes 5 files changed, 108 insertions(+), 95 deletions(-) delete mode 100644 server/database/database.go create mode 100644 server/database/items.go create mode 100644 server/database/main.go delete mode 100644 server/openbills.db diff --git a/server/database/database.go b/server/database/database.go deleted file mode 100644 index b3c1e78..0000000 --- a/server/database/database.go +++ /dev/null @@ -1,95 +0,0 @@ -/* - * OpenBills - Self hosted browser app to generate and keep track of simple invoices - * Version - 0 - * Licensed under the MIT license - https://opensource.org/licenses/MIT - * - * Copyright (c) 2021 Vidhu Kant Sharma -*/ - -// Idk how databases work this package is supposed to handle the sqlite database -// will figure that out - -package database - -import ( - "database/sql" - _ "github.com/mattn/go-sqlite3" -) - -type Item struct { - Model string - Desc string `json:"Description"` - Price float64 - HSN int - GST float64 - Cat string `json:"Category"` - Brand string -} - -var myDatabase *sql.DB -var register_item *sql.Stmt -func init() { - myDatabase, _ = sql.Open("sqlite3", "./openbills.db") - - 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, - gst REAL, - category TEXT, - brand TEXT)`, - ) - init_registered_items.Exec() - - register_item, _ = myDatabase.Prepare( - `INSERT INTO registered_items - (model, desc, price, hsn, gst, category, brand) - VALUES (?, ?, ?, ?, ?, ?, ?)`, - ) -} - -func GetAllItems() []Item { - var allItems []Item - rows, _ := myDatabase.Query( - `SELECT model, desc, price, hsn, gst, category, brand FROM registered_items`, - ) - - var ( - model, desc, cat, brand string - price, GST float64 - HSN int - ) - - for rows.Next() { - rows.Scan(&model, &desc, &price, &HSN, &GST, &cat, &brand) - allItems = append(allItems, Item{model, desc, price, HSN, GST, cat, brand}) - } - - return allItems -} - -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, - item.GST, item.Cat, - item.Brand, - ) - return true -} diff --git a/server/database/items.go b/server/database/items.go new file mode 100644 index 0000000..d3d56fd --- /dev/null +++ b/server/database/items.go @@ -0,0 +1,74 @@ +/* + * OpenBills - Self hosted browser app to generate and keep track of simple invoices + * Version - 0 + * Licensed under the MIT license - https://opensource.org/licenses/MIT + * + * Copyright (c) 2021 Vidhu Kant Sharma +*/ + +// handles all Items related database functions + +package database + +import ( + _ "github.com/mattn/go-sqlite3" +) + +type Item struct { + Model string + Desc string `json:"Description"` + Price float64 + HSN int + GST float64 + Cat string `json:"Category"` + Brand string +} + +func GetAllItems() []Item { + var allItems []Item + rows, _ := myDatabase.Query( + `SELECT model, desc, price, hsn, gst, category, brand FROM registered_items`, + ) + + var ( + model, desc, cat, brand string + price, GST float64 + HSN int + ) + + for rows.Next() { + rows.Scan(&model, &desc, &price, &HSN, &GST, &cat, &brand) + allItems = append(allItems, Item{model, desc, price, HSN, GST, cat, brand}) + } + + return allItems +} + +func RegisterItem(item Item) bool { + itemNames, _ := myDatabase.Query("SELECT model FROM registered_items") + + register_item, _ := myDatabase.Prepare( + `INSERT INTO registered_items + (model, desc, price, hsn, gst, category, brand) + VALUES (?, ?, ?, ?, ?, ?, ?)`, + ) + + // 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, + item.GST, item.Cat, + item.Brand, + ) + return true +} diff --git a/server/database/main.go b/server/database/main.go new file mode 100644 index 0000000..ecff904 --- /dev/null +++ b/server/database/main.go @@ -0,0 +1,33 @@ +/* + * OpenBills - Self hosted browser app to generate and keep track of simple invoices + * Version - 0 + * Licensed under the MIT license - https://opensource.org/licenses/MIT + * + * Copyright (c) 2021 Vidhu Kant Sharma +*/ + +package database + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" +) + +var myDatabase *sql.DB +func StartDB() { + myDatabase, _ = sql.Open("sqlite3", "./openbills.db") + + 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, + gst REAL, + category TEXT, + brand TEXT)`, + ) + init_registered_items.Exec() +} + diff --git a/server/main.go b/server/main.go index c75a5de..2d1e60d 100644 --- a/server/main.go +++ b/server/main.go @@ -21,6 +21,7 @@ import ( ) func main() { + db.StartDB() myRouter := gin.New() myRouter.Use(gin.Logger()) diff --git a/server/openbills.db b/server/openbills.db deleted file mode 100644 index 9cbbae4..0000000 Binary files a/server/openbills.db and /dev/null differ -- cgit v1.2.3