From 60f5d0cf22adc77b236f6c5a6e6aa2c7ff39ed77 Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Wed, 28 Apr 2021 10:12:46 +0530 Subject: implemented a small package to get items from sqlite db --- server/database/database.go | 44 ++++++++++++++++++++++++++++++++++++------ server/go.mod | 1 + server/go.sum | 2 ++ server/main.go | 46 ++------------------------------------------ server/openbills.db | Bin 0 -> 8192 bytes 5 files changed, 43 insertions(+), 50 deletions(-) create mode 100644 server/openbills.db diff --git a/server/database/database.go b/server/database/database.go index 3e60311..16b6f65 100644 --- a/server/database/database.go +++ b/server/database/database.go @@ -6,17 +6,49 @@ * Copyright (c) 2021 Vidhu Kant Sharma */ -// Idk how databases work -// this package is supposed to handle the sqlite database +// Idk how databases work this package is supposed to handle the sqlite database // will figure that out package database import ( - "fmt" + _ "github.com/mattn/go-sqlite3" + "database/sql" ) -// very important function that fucking needed a new package -func SayHello() { - fmt.Println("Hello") +type Item struct { + Model string + Desc string `json:"Description"` + Price int // *float32 + HSN int +} + + +var myDatabase *sql.DB +func init() { + myDatabase, _ = sql.Open("sqlite3", "./openbills.db") +} + +var myItems *sql.Stmt +var addToMyItems *sql.Stmt +func init() { + myItems, _ = myDatabase.Prepare("CREATE TABLE IF NOT EXISTS RegisteredItems (id INTEGER PRIMARY KEY, model TEXT, desc TEXT, price INTEGER, HSN INTEGER)") + myItems.Exec() + + addToMyItems, _ = myDatabase.Prepare("INSERT INTO RegisteredItems (id, model, desc, price, hsn) VALUES (?, ?, ?, ?, ?)") +} + +func GetAllItems() []Item { + var allItems []Item + rows, _ := myDatabase.Query("SELECT model, desc, price, hsn FROM RegisteredItems") + + var model string + var desc string + var price int + var hsn int + for rows.Next() { + rows.Scan(&model, &desc, &price, &hsn) + allItems = append(allItems, Item{model, desc, price, hsn}) + } + return allItems } diff --git a/server/go.mod b/server/go.mod index 1969712..713c770 100644 --- a/server/go.mod +++ b/server/go.mod @@ -5,4 +5,5 @@ go 1.16 require ( github.com/gin-gonic/contrib v0.0.0-20201101042839-6a891bf89f19 github.com/gin-gonic/gin v1.7.1 + github.com/mattn/go-sqlite3 v1.14.7 ) diff --git a/server/go.sum b/server/go.sum index 0353ffe..48b44d4 100644 --- a/server/go.sum +++ b/server/go.sum @@ -24,6 +24,8 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= +github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= diff --git a/server/main.go b/server/main.go index 5eab33c..b514a31 100644 --- a/server/main.go +++ b/server/main.go @@ -12,23 +12,15 @@ package main import ( - "net/http" "github.com/gin-gonic/gin" "github.com/gin-gonic/contrib/static" + "net/http" // this handles all the database functions db "github.com/MikunoNaka/openbills/database" ) -type Item struct { - Model string - Desc string `json:"Description"` - Price float32 - HSN int -} - func main() { - db.SayHello() myRouter := gin.New() myRouter.Use(gin.Logger()) @@ -46,40 +38,6 @@ func main() { } func getAllItems(ctx *gin.Context) { - allItems := [6]Item{ - { - "Kisan Chair", - "Very Good Chair", - 100, - 9403, - }, { - "Supreme Chair", - "Even Better Chair", - 200, - 9403, - }, { - "Action Houseware", - "Not a chair", - 50, - 69, - }, { - "Coirfit Mattress", - "I wanna sleep", - 900, - 420, - }, { - "AVRO Chair", - "Formerly AVON lol", - 150, - 9403, - }, { - "Mystery Item", - "hehe hehehehe", - 1000, - 177013, - }, - } - ctx.Header("Content-Type", "application/json") - ctx.JSON(http.StatusOK, allItems) + ctx.JSON(http.StatusOK, db.GetAllItems()) } diff --git a/server/openbills.db b/server/openbills.db new file mode 100644 index 0000000..1f2afd2 Binary files /dev/null and b/server/openbills.db differ -- cgit v1.2.3