/
alerts.go
136 lines (118 loc) · 2.59 KB
/
alerts.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package database
import (
"errors"
"log"
)
// GetActiveAlerts gets alerts from db for UI display
func (d *Database) GetActiveAlerts() []interface{} {
Alerts := make([]interface{}, 0)
rows, err := d.db.Query(
`SELECT id, name, type, content, active, allow_dismiss, registered_only FROM alert WHERE active IS TRUE;`,
)
if err == nil {
defer rows.Close()
for rows.Next() {
var a Alert
if err := rows.Scan(
&a.AlertID,
&a.Name,
&a.Type,
&a.Content,
&a.Active,
&a.AllowDismiss,
&a.RegisteredOnly,
); err != nil {
log.Println(err)
} else {
Alerts = append(Alerts, &a)
}
}
}
return Alerts
}
// AlertsList gets alerts from db for admin listing
func (d *Database) AlertsList(Limit int, Offset int) []interface{} {
Alerts := make([]interface{}, 0)
rows, err := d.db.Query(
`SELECT id, name, type, content, active, allow_dismiss, registered_only, created_date, updated_date
FROM alert
LIMIT $1
OFFSET $2;
`,
Limit,
Offset,
)
if err == nil {
defer rows.Close()
for rows.Next() {
var a Alert
if err := rows.Scan(
&a.AlertID,
&a.Name,
&a.Type,
&a.Content,
&a.Active,
&a.AllowDismiss,
&a.RegisteredOnly,
&a.CreatedDate,
&a.UpdatedDate,
); err != nil {
log.Println(err)
} else {
Alerts = append(Alerts, &a)
}
}
}
return Alerts
}
// AlertsCreate creates
func (d *Database) AlertsCreate(Name string, Type string, Content string, Active bool, AllowDismiss bool, RegisteredOnly bool) error {
if _, err := d.db.Exec(
`INSERT INTO alert (name, type, content, active, allow_dismiss, registered_only)
VALUES ($1, $2, $3, $4, $5, $6);
`,
Name,
Type,
Content,
Active,
AllowDismiss,
RegisteredOnly,
); err != nil {
log.Println(err)
return errors.New("error attempting to add new alert")
}
return nil
}
// AlertsUpdate updates an alert
func (d *Database) AlertsUpdate(ID string, Name string, Type string, Content string, Active bool, AllowDismiss bool, RegisteredOnly bool) error {
if _, err := d.db.Exec(
`
UPDATE alert
SET name = $2, type = $3, content = $4, active = $5, allow_dismiss = $6, registered_only = $7
WHERE id = $1;
`,
ID,
Name,
Type,
Content,
Active,
AllowDismiss,
RegisteredOnly,
); err != nil {
log.Println(err)
return errors.New("error attempting to update alert")
}
return nil
}
// AlertDelete deletes an alert
func (d *Database) AlertDelete(AlertID string) error {
_, err := d.db.Exec(
`DELETE FROM alert WHERE id = $1;`,
AlertID,
)
if err != nil {
log.Println("Unable to delete alert: ", err)
return err
}
return nil
}