From 8c906c98b2a2994286731357831ca8005ef0d73f Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Sun, 8 Aug 2021 16:11:08 +0530 Subject: Added better support for saving client data --- server/database/database.go | 15 ++++++++-- server/database/people.go | 70 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 14 deletions(-) (limited to 'server/database') diff --git a/server/database/database.go b/server/database/database.go index 68c3c76..a7a4f34 100644 --- a/server/database/database.go +++ b/server/database/database.go @@ -44,9 +44,18 @@ func InitDB() { Name TEXT, Phone TEXT, Email TEXT, - BillAddress BLOB, - ShipAddress BLOB, - GSTIN TEXT)`, + + BillAddressLine TEXT, + BillAddressCity TEXT, + BillAddressState TEXT, + BillAddressPINCode TEXT, + BillAddressCountry TEXT, + + ShipAddressLine TEXT, + ShipAddressCity TEXT, + ShipAddressState TEXT, + ShipAddressPINCode TEXT, + ShipAddressCountry TEXT)`, ) init_people.Exec() diff --git a/server/database/people.go b/server/database/people.go index 25aa393..f0b05b8 100644 --- a/server/database/people.go +++ b/server/database/people.go @@ -4,13 +4,14 @@ * Licensed under the MIT license - https://opensource.org/licenses/MIT * * Copyright (c) 2021 Vidhu Kant Sharma -*/ + */ // handles all People related database functions -package database +package database import ( - _ "github.com/mattn/go-sqlite3" + _ "github.com/mattn/go-sqlite3" + "fmt" ) @@ -22,38 +23,85 @@ type Person struct { Email string BillAddress Address ShipAddress Address - GSTIN string } func GetAllPeople() []Person { var allPeople []Person rows, _ := myDatabase.Query ( - `SELECT ID, Name, Phone, Email, BillAddress, ShipAddress, GSTIN FROM People`, + `SELECT ID, Name, Phone, Email, + + BillAddressLine, BillAddressCity, + BillAddressState, BillAddressPINCode, + BillAddressCountry, + + ShipAddressLine, + ShipAddressCity, ShipAddressState, + ShipAddressPINCode, ShipAddressCountry + + FROM People`, ) var ( id int - name, phone, email, gstin string + name, phone, email string + billAddressLine, billAddressCity, billAddressState, billAddressPINCode, billAddressCountry string + shipAddressLine, shipAddressCity, shipAddressState, shipAddressPINCode, shipAddressCountry string billAddress, shipAddress Address ) for rows.Next() { - rows.Scan(&id, &name, &phone, &email, &billAddress, &shipAddress, &gstin) - allPeople = append(allPeople, Person{id, name, phone, email, billAddress, shipAddress, gstin}) + rows.Scan( + &id, &name, &phone, &email, + + &billAddressLine, &billAddressCity, + &billAddressState, &billAddressPINCode, + &billAddressCountry, + + &shipAddressLine, &shipAddressCity, + &shipAddressState, &shipAddressPINCode, + &shipAddressCountry, + ) + + billAddress = Address{billAddressLine, billAddressCity, billAddressState, billAddressPINCode, billAddressCountry} + shipAddress = Address{shipAddressLine, shipAddressCity, shipAddressState, shipAddressPINCode, shipAddressCountry} + + allPeople = append(allPeople, Person{id, name, phone, email, billAddress, shipAddress}) + fmt.Println(allPeople) } return allPeople } func RegisterPerson(person Person) bool { + fmt.Println(person) register_person, _ := myDatabase.Prepare( `INSERT INTO People - (Name, Phone, Email, BillAddress, ShipAddress, GSTIN) - VALUES (?, ?, ?, ?, ?, ?)`, + (Name, Phone, Email, + + BillAddressLine, BillAddressCity, + BillAddressState, BillAddressPINCode, + BillAddressCountry, + + ShipAddressLine, ShipAddressCity, + ShipAddressState, ShipAddressPINCode, + ShipAddressCountry) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ) register_person.Exec( - person.Name, person.Phone, person.Email, person.BillAddress, person.ShipAddress, person.GSTIN, + person.Name, person.Phone, person.Email, + + person.BillAddress.AddressLine, + person.BillAddress.City, + person.BillAddress.State, + person.BillAddress.PINCode, + person.BillAddress.Country, + + person.ShipAddress.AddressLine, + person.ShipAddress.City, + person.ShipAddress.State, + person.ShipAddress.PINCode, + person.ShipAddress.Country, ) return true -- cgit v1.2.3