-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwhitelabelerrors.go
More file actions
62 lines (51 loc) · 1.33 KB
/
whitelabelerrors.go
File metadata and controls
62 lines (51 loc) · 1.33 KB
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
package database
import (
"context"
"github.com/jackc/pgx/v4/pgxpool"
"time"
)
type WhitelabelErrors struct {
*pgxpool.Pool
}
func newWhitelabelErrors(db *pgxpool.Pool) *WhitelabelErrors {
return &WhitelabelErrors{
db,
}
}
type WhitelabelError struct {
Message string `json:"message"`
Time time.Time `json:"time"`
}
func (w WhitelabelErrors) Schema() string {
return `
CREATE TABLE IF NOT EXISTS whitelabel_errors(
"error_id" serial,
"user_id" int8 NOT NULL,
"error" varchar(255) NOT NULL,
"error_time" timestamptz NOT NULL,
PRIMARY KEY("error_id")
);
`
}
func (w *WhitelabelErrors) GetRecent(ctx context.Context, userId uint64, limit int) (errors []WhitelabelError, e error) {
query := `SELECT "error", "error_time" FROM whitelabel_errors WHERE "user_id" = $1 ORDER BY "error_id" DESC LIMIT $2;`
rows, err := w.Query(ctx, query, userId, limit)
defer rows.Close()
if err != nil {
e = err
return
}
for rows.Next() {
var error WhitelabelError
if e = rows.Scan(&error.Message, &error.Time); e != nil {
continue
}
errors = append(errors, error)
}
return
}
func (w *WhitelabelErrors) Append(ctx context.Context, userId uint64, error string) (err error) {
query := `INSERT INTO whitelabel_errors("user_id", "error", "error_time") VALUES($1, $2, NOW());`
_, err = w.Exec(ctx, query, userId, error)
return
}