/
webhook.go
62 lines (52 loc) · 1.77 KB
/
webhook.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
package repository
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/allisson/postmand"
)
// Webhook implements postmand.WebhookRepository interface.
type Webhook struct {
db *sqlx.DB
}
// Get returns postmand.Webhook by options filter.
func (w Webhook) Get(ctx context.Context, getOptions postmand.RepositoryGetOptions) (*postmand.Webhook, error) {
webhook := postmand.Webhook{}
query, args := getQuery("webhooks", getOptions)
err := w.db.GetContext(ctx, &webhook, query, args...)
if err == sql.ErrNoRows {
return &webhook, postmand.ErrWebhookNotFound
}
return &webhook, err
}
// List returns a slice of postmand.Webhook by options filter.
func (w Webhook) List(ctx context.Context, listOptions postmand.RepositoryListOptions) ([]*postmand.Webhook, error) {
webhooks := []*postmand.Webhook{}
query, args := listQuery("webhooks", listOptions)
err := w.db.SelectContext(ctx, &webhooks, query, args...)
return webhooks, err
}
// Create postmand.Webhook on database.
func (w Webhook) Create(ctx context.Context, webhook *postmand.Webhook) error {
query, args := insertQuery("webhooks", webhook)
_, err := w.db.ExecContext(ctx, query, args...)
return err
}
// Update postmand.Webhook on database.
func (w Webhook) Update(ctx context.Context, webhook *postmand.Webhook) error {
query, args := updateQuery("webhooks", webhook.ID, webhook)
_, err := w.db.ExecContext(ctx, query, args...)
return err
}
// Delete postmand.Webhook on database.
func (w Webhook) Delete(ctx context.Context, id postmand.ID) error {
query := `
DELETE FROM webhooks WHERE id = $1
`
_, err := w.db.ExecContext(ctx, query, id)
return err
}
// NewWebhook will create an implementation of postmand.WebhookRepository.
func NewWebhook(db *sqlx.DB) *Webhook {
return &Webhook{db: db}
}