-
Notifications
You must be signed in to change notification settings - Fork 4
/
whitelabelstatuses.go
64 lines (54 loc) · 1.63 KB
/
whitelabelstatuses.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
package database
import (
"context"
"fmt"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type WhitelabelStatuses struct {
*pgxpool.Pool
}
func newWhitelabelStatuses(db *pgxpool.Pool) *WhitelabelStatuses {
return &WhitelabelStatuses{
db,
}
}
func (w WhitelabelStatuses) Schema() string {
return `
CREATE TABLE IF NOT EXISTS whitelabel_statuses(
"bot_id" int8 UNIQUE NOT NULL,
"status" varchar(255) NOT NULL,
"status_type" int2 NOT NULL DEFAULT 2,
FOREIGN KEY("bot_id") REFERENCES whitelabel("bot_id") ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY("bot_id")
);
`
}
// Get Returns (status, status_type, exists, error)
func (w *WhitelabelStatuses) Get(ctx context.Context, botId uint64) (string, int16, bool, error) {
query := `SELECT "status", "status_type" FROM whitelabel_statuses WHERE "bot_id" = $1;`
var status string
var statusType int16
if err := w.QueryRow(ctx, query, botId).Scan(&status, &statusType); err != nil {
if err == pgx.ErrNoRows {
return "", 0, false, nil
} else {
return "", 0, false, err
}
}
return status, statusType, true, nil
}
func (w *WhitelabelStatuses) Set(ctx context.Context, botId uint64, status string, statusType int16) (err error) {
query := `
INSERT INTO whitelabel_statuses("bot_id", "status", "status_type")
VALUES($1, $2, $3)
ON CONFLICT("bot_id") DO UPDATE SET "status" = $2, "status_type" = $3;`
_, err = w.Exec(ctx, query, botId, status, statusType)
return
}
func (w *WhitelabelStatuses) Delete(ctx context.Context, botId uint64) (err error) {
query := `DELETE FROM whitelabel_statuses WHERE "bot_id"=$1;`
_, err = w.Exec(ctx, query, botId)
fmt.Println()
return
}