-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.go
61 lines (53 loc) · 1.74 KB
/
credit.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
package main
import (
"database/sql"
)
// CreditCodeLen is the length of the randomly generated code to be used to activate credit on a users account
const CreditCodeLen = 100
// GetUserPermCode requests a users perm code if they have one
func GetUserPermCode(db *sql.DB, user User) (permCode sql.NullString, customCode sql.NullString) {
result := db.QueryRow(`
SELECT perm_user_code, custom_user_code
FROM credit
WHERE UUID = ?
LIMIT 1`, Hash(user.UUID))
Handle(result.Scan(&permCode, &customCode))
return
}
// RemovePermCodes will remove the users stored perm code
func RemovePermCodes(db *sql.DB, user User) error {
return UpdateErr(db.Exec(`
UPDATE credit
SET perm_user_code = NULL, custom_user_code = NULL
WHERE UUID=?`, Hash(user.UUID)))
}
// SetCustomCode sets a permanent custom code for a user
func SetCustomCode(db *sql.DB, user User) error {
return UpdateErr(db.Exec(`
UPDATE credit
SET custom_user_code=?
WHERE UUID=?`, user.Code, Hash(user.UUID)))
}
// SetPermCode sets a permanent code for a user
func SetPermCode(db *sql.DB, user User) error {
return UpdateErr(db.Exec(`
UPDATE credit
SET perm_user_code=?
WHERE UUID=?`, user.Code, Hash(user.UUID)))
}
// SetCreditCode associates a credit code to an account
func SetCreditCode(db *sql.DB, user User, activationCode string) error {
return UpdateErr(db.Exec(`
UPDATE credit
SET UUID=?, activation_dttm=NOW()
WHERE activation_code=?
AND UUID IS NULL`, Hash(user.UUID), activationCode))
}
// GetCredit fetches the amount of credit the user has linked to their account
func GetCredit(db *sql.DB, user User) (credit sql.NullFloat64) {
result := db.QueryRow(`SELECT SUM(credit) as total_credit
FROM credit
WHERE UUID = ?`, Hash(user.UUID))
Handle(result.Scan(&credit))
return credit
}