From 45a3b94f133ebcd9abff2baf266d0b3f691d2790 Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Sat, 1 May 2021 09:43:59 +0530 Subject: defined api group to handle DB functions regarding people/customers --- server/database/items.go | 10 +++---- server/database/main.go | 29 +++++++++++++------- server/database/people.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 server/database/people.go (limited to 'server/database') diff --git a/server/database/items.go b/server/database/items.go index d3d56fd..325b7c1 100644 --- a/server/database/items.go +++ b/server/database/items.go @@ -2,7 +2,6 @@ * 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 */ @@ -27,7 +26,7 @@ type Item struct { func GetAllItems() []Item { var allItems []Item rows, _ := myDatabase.Query( - `SELECT model, desc, price, hsn, gst, category, brand FROM registered_items`, + `SELECT Model, Desc, Price, Hsn, Gst, Category, Brand FROM Items`, ) var ( @@ -45,11 +44,11 @@ func GetAllItems() []Item { } func RegisterItem(item Item) bool { - itemNames, _ := myDatabase.Query("SELECT model FROM registered_items") + itemNames, _ := myDatabase.Query("SELECT model FROM Items") register_item, _ := myDatabase.Prepare( - `INSERT INTO registered_items - (model, desc, price, hsn, gst, category, brand) + `INSERT INTO Items + (Model, Desc, Price, Hsn, Gst, Category, Brand) VALUES (?, ?, ?, ?, ?, ?, ?)`, ) @@ -70,5 +69,6 @@ func RegisterItem(item Item) bool { item.GST, item.Cat, item.Brand, ) + return true } diff --git a/server/database/main.go b/server/database/main.go index ecff904..de2365c 100644 --- a/server/database/main.go +++ b/server/database/main.go @@ -17,17 +17,26 @@ var myDatabase *sql.DB func StartDB() { myDatabase, _ = sql.Open("sqlite3", "./openbills.db") - init_registered_items, _ := myDatabase.Prepare( - `CREATE TABLE IF NOT EXISTS registered_items + init_items, _ := myDatabase.Prepare( + `CREATE TABLE IF NOT EXISTS Items (id INTEGER PRIMARY KEY AUTOINCREMENT, - model TEXT NOT NULL, - desc TEXT, - price REAL, - hsn BLOB, - gst REAL, - category TEXT, - brand TEXT)`, + Model TEXT NOT NULL, + Desc TEXT, + Price REAL, + Hsn BLOB, + Gst REAL, + Category TEXT, + Brand TEXT)`, ) - init_registered_items.Exec() + init_items.Exec() + + init_people, _ := myDatabase.Prepare( + `CREATE TABLE IF NOT EXISTS People + (id INTEGER PRIMARY KEY AUTOINCREMENT, + Name TEXT, + Phone TEXT, + Email TEXT)`, + ) + init_people.Exec() } diff --git a/server/database/people.go b/server/database/people.go new file mode 100644 index 0000000..0ff3acb --- /dev/null +++ b/server/database/people.go @@ -0,0 +1,67 @@ +/* + * 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 People related database functions + +package database + +import ( + _ "github.com/mattn/go-sqlite3" + "fmt" +) + +type Person struct { + Name string + Phone string + Email string +} + +func GetAllPeople() []Person { + var allPeople []Person + rows, _ := myDatabase.Query( + `SELECT Name, Phone, Email FROM People`, + ) + + var ( + name, phone, email string + ) + + for rows.Next() { + rows.Scan(&name, &phone, &email) + allPeople = append(allPeople, Person{name, phone, email}) + } + + return allPeople +} + +func RegisterPerson(person Person) bool { + personNames, _ := myDatabase.Query("SELECT Name FROM People") + + // check if already exists + // probs shouldnt make it + // make front end handle it + for personNames.Next() { + var rPerson string + if rPerson == person.Name { + fmt.Println(person.Name, "already exists") + return false + } + } + + register_person, _ := myDatabase.Prepare( + `INSERT INTO People + (Name, Phone, Email) + VALUES (?, ?, ?)`, + ) + + register_person.Exec( + person.Name, person.Phone, person.Email, + ) + + return true +} -- cgit v1.2.3