aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/database/items.go10
-rw-r--r--server/database/main.go29
-rw-r--r--server/database/people.go67
-rw-r--r--server/main.go42
4 files changed, 121 insertions, 27 deletions
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
+}
diff --git a/server/main.go b/server/main.go
index 2d1e60d..1740ac6 100644
--- a/server/main.go
+++ b/server/main.go
@@ -31,41 +31,59 @@ func main() {
// define routes
api := myRouter.Group("/api")
+ people := api.Group("/people")
items := api.Group("/items")
+ // items API routes
items.GET("/", getAllItems)
items.POST("/", registerItem)
- // items.POST("/", registerItem)
+
+ // people API routes
+ people.GET("/", getAllPeople)
myRouter.Run(":8080")
}
+// items API functions
func getAllItems(ctx *gin.Context) {
ctx.Header("Content-Type", "application/json")
ctx.JSON(http.StatusOK, db.GetAllItems())
}
func registerItem(ctx *gin.Context) {
- // extract data
- model := ctx.Query("model")
- desc := ctx.Query("desc")
+ // extract data not string
price, _ := strconv.ParseFloat(ctx.Query("price"), 64)
hsn, _ := strconv.Atoi(ctx.Query("hsn"))
gst, _ := strconv.ParseFloat(ctx.Query("gst"), 64)
cat := "cat coming soon"
brand := "brand coming soon"
- // why does it show warnings
item := db.Item {
- model,
- desc,
- price,
- hsn,
- gst,
- cat,
- brand,
+ Model: ctx.Query("model"),
+ Desc: ctx.Query("desc"),
+ Price: price,
+ HSN: hsn,
+ GST: gst,
+ Cat: cat,
+ Brand: brand,
}
db.RegisterItem(item)
}
+
+// people API functions
+func getAllPeople(ctx *gin.Context) {
+ ctx.Header("Content-Type", "application/json")
+ ctx.JSON(http.StatusOK, db.GetAllPeople())
+}
+
+func registerPerson(ctx *gin.Context) {
+ person := db.Person {
+ Name: ctx.Query("name"),
+ Phone: ctx.Query("phone"),
+ Email: ctx.Query("email"),
+ }
+
+ db.RegisterPerson(person)
+}