-
Notifications
You must be signed in to change notification settings - Fork 1
/
customers_db.go
102 lines (94 loc) · 2.5 KB
/
customers_db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package db
import (
"database/sql"
"fmt"
)
func getCustomersTableName() string {
return "customers"
}
func GetCustomerNames() []string {
dbSt, _ := ctxCache.DbInfo.(*dbState)
customers := []string{}
tblName := getCustomersTableName()
rows, err := dbSt.db.Query("select customerName from " + tblName)
if err != nil {
logger.Error().AnErr("Error", err).Msg("Querying list of Customers..")
return customers
}
for rows.Next() {
var name string
err := rows.Scan(&name)
if err != nil {
logger.Error().AnErr("Error", err).Msg("Scanning rows...")
}
customers = append(customers, name)
}
rows.Close()
return customers
}
func IsValidCustomer(custName string) (status string, ok bool) {
dbSt, _ := ctxCache.DbInfo.(*dbState)
status = ""
ok = false
tblName := getCustomersTableName()
rows, err := dbSt.db.Query("select status from "+tblName+" where customerName = ?", custName)
if err != nil {
logger.Error().AnErr("Error", err).Msg("Querying list of Customers..")
return
}
for rows.Next() {
err := rows.Scan(&status)
if err == nil {
if status == "Active" {
ok = true
break
}
}
}
rows.Close()
logger.Debug().Caller().Str("Customer", custName).
Str("Status", status).
Bool("valid", ok).Msg("Checking customer")
return
}
func IsValidCustomerSecret(custName string, inSecret string) bool {
dbSt, _ := ctxCache.DbInfo.(*dbState)
validCustomer := false
tblName := getCustomersTableName()
rows, err := dbSt.db.Query("select customerSecret from "+tblName+" where customerName = ?", custName)
if err != nil {
logger.Error().AnErr("Error", err).Msg("Querying list of Customers..")
}
for rows.Next() {
var expSecret string
err := rows.Scan(&expSecret)
if err == nil {
if expSecret == inSecret {
validCustomer = true
break
}
}
}
rows.Close()
logger.Debug().Caller().Str("Customer", custName).Bool("validSecret", validCustomer).Msg("")
return validCustomer
}
func checkAddCustomer(db *sql.DB, custName string) error {
st, ok := IsValidCustomer(custName)
if ok {
return nil
}
if st != "" {
//TODO - this customer is NOT active - will need to update
return nil
}
tblName := getCustomersTableName()
addStmt := fmt.Sprintf("insert into %s (customerName, customerSecret, id, status) values (?, ?, ?, ?)", tblName)
defaultCustSecret := custName + "123"
_, err := db.Exec(addStmt, custName, defaultCustSecret, 0, "Active")
if err != nil {
logger.Debug().Caller().AnErr("Error", err).Str("SqlStmt", addStmt).Msg("Adding Customer")
return err
}
return nil
}