-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
113 lines (103 loc) · 2.22 KB
/
repository.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
package webhook
import (
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/zen-en-tonal/mtw/database"
"github.com/zen-en-tonal/mtw/session"
"github.com/zen-en-tonal/mtw/webhook"
)
type webhookRepository struct {
conn *sqlx.DB
}
func newRepository(db *sql.DB) webhookRepository {
return webhookRepository{sqlx.NewDb(db, database.Driver)}
}
func (r webhookRepository) upsert(table webhookTable) error {
_, err := r.conn.Exec(`
INSERT INTO webhooks VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id)
DO
UPDATE SET
endpoint = $2
, auth = $3
, schema = $4
, method = $5
, content_type = $6
`,
table.ID,
table.Endpoint,
table.Auth,
table.Schema,
table.Method,
table.ContentType,
)
return err
}
func (r webhookRepository) findOne(id webhook.WebhookID) (*webhookTable, error) {
var tables []webhookTable
if err := r.conn.Select(&tables, `
SELECT
webhooks.*
FROM
webhooks
WHERE
id = $1
`,
id.String()); err != nil {
return nil, err
}
if len(tables) == 0 {
return nil, database.ErrNotFound
}
table := tables[0]
return &table, nil
}
func (r webhookRepository) findAll() (*[]webhookTable, error) {
var tables []webhookTable
if err := r.conn.Select(&tables, `SELECT webhooks.* FROM webhooks`); err != nil {
return nil, err
}
return &tables, nil
}
func (r webhookRepository) findByAddr(addr session.Address) (*[]webhookTable, error) {
var tables []webhookTable
if err := r.conn.Select(&tables, `
SELECT
webhooks.*
FROM
webhooks
JOIN
addresses_webhooks ON webhooks.id = addresses_webhooks.webhook_id
WHERE
addresses_webhooks.address = $1
`,
addr.String()); err != nil {
return nil, err
}
return &tables, nil
}
func (r *webhookRepository) insertAddressWebhook(addr session.Address, webhookID webhook.WebhookID) error {
_, err := r.conn.Exec(`
INSERT INTO addresses_webhooks (
address
, webhook_id
)
VALUES (
$1
, $2
)`,
addr.String(),
webhookID.String())
return err
}
func (r *webhookRepository) deleteAddressWebhook(addr session.Address, webhookID webhook.WebhookID) error {
_, err := r.conn.Exec(`
DELETE FROM addresses_webhooks
WHERE
address = $1
AND webhook_id = $2
`,
addr.String(),
webhookID.String())
return err
}