-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpanelrolementions.go
107 lines (87 loc) · 2.8 KB
/
panelrolementions.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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type PanelRoleMentions struct {
*pgxpool.Pool
}
func newPanelRoleMentions(db *pgxpool.Pool) *PanelRoleMentions {
return &PanelRoleMentions{
db,
}
}
func (p PanelRoleMentions) Schema() string {
return `
CREATE TABLE IF NOT EXISTS panel_role_mentions(
"panel_id" int NOT NULL,
"role_id" int8 NOT NULL,
FOREIGN KEY("panel_id") REFERENCES panels("panel_id") ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY("panel_id", "role_id")
);
CREATE INDEX IF NOT EXISTS panel_role_mentions_panel_id ON panel_role_mentions("panel_id");
`
}
func (p *PanelRoleMentions) GetRoles(ctx context.Context, panelId int) (roles []uint64, e error) {
query := `SELECT "role_id" from panel_role_mentions WHERE "panel_id"=$1;`
rows, err := p.Query(ctx, query, panelId)
defer rows.Close()
if err != nil {
e = err
return
}
for rows.Next() {
var roleId uint64
if err := rows.Scan(&roleId); err != nil {
continue
}
roles = append(roles, roleId)
}
return
}
func (p *PanelRoleMentions) Add(ctx context.Context, panelId int, roleId uint64) (err error) {
query := `INSERT INTO panel_role_mentions("panel_id", "role_id") VALUES($1, $2) ON CONFLICT("panel_id", "role_id") DO NOTHING;`
_, err = p.Exec(ctx, query, panelId, roleId)
return
}
func (p *PanelRoleMentions) DeleteAll(ctx context.Context, panelId int) (err error) {
query := `DELETE FROM panel_role_mentions WHERE "panel_id"=$1;`
_, err = p.Exec(ctx, query, panelId)
return
}
func (p *PanelRoleMentions) DeleteAllRole(ctx context.Context, roleId uint64) (err error) {
query := `DELETE FROM panel_role_mentions WHERE "role_id"=$1;`
_, err = p.Exec(ctx, query, roleId)
return
}
func (p *PanelRoleMentions) Delete(ctx context.Context, panelId int, roleId uint64) (err error) {
query := `DELETE FROM panel_role_mentions WHERE "panel_id"=$1 AND "role_id"=$2;`
_, err = p.Exec(ctx, query, panelId, roleId)
return
}
func (p *PanelRoleMentions) Replace(ctx context.Context, panelId int, roleIds []uint64) error {
tx, err := p.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
if err := p.ReplaceWithTx(ctx, tx, panelId, roleIds); err != nil {
return err
}
return tx.Commit(ctx)
}
func (p *PanelRoleMentions) ReplaceWithTx(ctx context.Context, tx pgx.Tx, panelId int, roleIds []uint64) error {
// Remove existing mentions from panel
if _, err := tx.Exec(ctx, `DELETE FROM panel_role_mentions WHERE "panel_id" = $1;`, panelId); err != nil {
return err
}
// Add each provided mention to panel
for _, roleId := range roleIds {
query := `INSERT INTO panel_role_mentions("panel_id", "role_id") VALUES($1, $2) ON CONFLICT("panel_id", "role_id") DO NOTHING;`
if _, err := tx.Exec(ctx, query, panelId, roleId); err != nil {
return err
}
}
return nil
}