Browse Source

Add insert and select queries for user licenses

master
Immanuel Onyeka 1 year ago
parent
commit
fb64597116
1 changed files with 51 additions and 4 deletions
  1. +51
    -4
      skouter.go

+ 51
- 4
skouter.go View File

@@ -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) {


Loading…
Cancel
Save