From fb64597116ea42b0b341b463975f6f6274f7fed0 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Mon, 11 Sep 2023 17:55:31 -0400 Subject: [PATCH] Add insert and select queries for user licenses --- skouter.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/skouter.go b/skouter.go index f200d44..602e424 100644 --- a/skouter.go +++ b/skouter.go @@ -65,6 +65,7 @@ type User struct { Phone string `json:"phone"` Address Address `json:"address"` Branch Branch `json:"branch"` + License License `json:"license"` Status string `json:"status"` Country string `json:"country"` Title string `json:"title"` @@ -75,6 +76,7 @@ type User struct { type License struct { Id int `json:"id"` + UserId int `json:"userId"` Type string `json:"type"` Num string `json:"num"` } @@ -895,6 +897,51 @@ func insertBranch(db *sql.DB, branch Branch) (int, error){ return id, err } +// Inserts an address and returns it's ID along with any errors. +func insertLicense(db *sql.DB, license License) (int, error) { + var query string + var row *sql.Row + var err error + var id int // Inserted license's id + + query = `INSERT INTO license + ( + user_id, + type, + num, + ) + VALUES (?, ?, ?) + RETURNING id + ` + + row = db.QueryRow(query, + license.UserId, + license.Type, + license.Num, + ) + + err = row.Scan(&id) + + return id, err +} + +func queryLicense(db *sql.DB, user int) ( License, error ) { + var license License = License{UserId: user} + var err error + + row := db.QueryRow( + `SELECT id, type, num FROM license WHERE user_id = ?`, + user) + + err = row.Scan( + &license.Id, + &license.Type, + &license.Num, + ) + + return license, err +} + func queryAddress(db *sql.DB, id int) ( Address, error ) { var address Address = Address{Id: id} var err error @@ -2406,14 +2453,14 @@ func seedUsers(db *sql.DB, addresses []Address, branches []Branch) []User { return users } -func seedLicenses(db *sql.DB, users []User) []Branch { - licenses := make([]License, len(users) +func seedLicenses(db *sql.DB, users []User) []License { + licenses := make([]License, len(users)) for i := range licenses { licenses[i].UserId = users[i].Id licenses[i].Type = []string{"NMLS", "FSRA"}[gofakeit.Number(0, 1)] licenses[i].Num = gofakeit.UUID() - // id, err := insertLicense(db, licenses[i]) + id, err := insertLicense(db, licenses[i]) if err != nil {log.Println(err); break} licenses[i].Id = id } @@ -2425,7 +2472,7 @@ func dbSeed(db *sql.DB) { addresses := seedAddresses(db) branches := seedBranches(db, addresses) users := seedUsers(db, addresses, branches) - license := seedLicenses(db, users) + _ = seedLicenses(db, users) } func dev(args []string) {