-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.registrations.go
95 lines (79 loc) · 2.32 KB
/
database.registrations.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
package main
import (
"github.com/lordmortis/goBungieNet"
)
type Registration struct {
newRecord bool
discordID string
bungieID int64
network goBungieNet.BungieMembershipType
}
func (rego *Registration)GetProfile(components []goBungieNet.DestinyComponentType) (*goBungieNet.GetProfileResponse, error) {
return goBungieNet.GetProfile(rego.bungieID, rego.network, components)
}
func migrateRegoTable() error {
createSQL := `CREATE TABLE IF NOT EXISTS 'registrations' (
'discord_id' TEXT,
'bungie_id' INTEGER,
'network_type' INTEGER
)`
// todo - handle if table exists
_, err := db.Exec(createSQL)
return err
}
func (rego *Registration)Save() error {
sql := ""
if rego.newRecord {
sql = `INSERT INTO registrations
(bungie_id, network_type, discord_id)
VALUES (?, ?, ?)`
} else {
sql = `UPDATE registrations
SET bungie_id = ?, network_type = ?
WHERE discord_id = ?`
}
_, err := db.Exec(sql, rego.bungieID, rego.network, rego.discordID)
return err
}
func loadRego(discordID string) (*Registration, error){
sql := `SELECT bungie_id, network_type FROM registrations WHERE discord_id = $1`
rows, err := db.Query(sql, discordID)
if err != nil { return nil, err }
if !rows.Next() { return nil, nil }
defer rows.Close()
rego := Registration{ newRecord: false, discordID: discordID, }
err = rows.Scan(®o.bungieID, ®o.network)
return ®o, err
}
func loadRegos() (*[]Registration, error) {
sql := `SELECT discord_id, bungie_id, network_type FROM registrations`
rows, err := db.Query(sql)
if err != nil { return nil, err }
defer rows.Close()
var regos []Registration
for rows.Next() {
rego := Registration{newRecord: false}
err = rows.Scan(®o.discordID, ®o.bungieID, ®o.network)
if err != nil { return ®os, err }
regos = append(regos, rego)
}
return ®os, nil
}
func createRego(discordID string, bungieID int64, network goBungieNet.BungieMembershipType) error {
rego, err := loadRego(discordID)
if err != nil { return err }
if rego == nil {
rego = &Registration{
newRecord: true,
discordID: discordID,
bungieID: bungieID,
network: network,
}
} else {
rego.bungieID = bungieID
rego.network = network
}
err = rego.Save()
if err == nil { rego.newRecord = false }
return err
}