memos/store/db/postgres/webhook.go

118 lines
3 KiB
Go
Raw Normal View History

package postgres
import (
"context"
2024-01-06 17:12:10 +08:00
"strings"
"github.com/usememos/memos/store"
)
2024-04-13 11:54:37 +08:00
func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
2024-01-06 17:12:10 +08:00
fields := []string{"name", "url", "creator_id"}
2024-04-13 12:11:59 +08:00
args := []any{create.Name, create.URL, create.CreatorID}
2024-01-06 17:12:10 +08:00
stmt := "INSERT INTO webhook (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts, row_status"
var rowStatus string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
2024-04-13 11:54:37 +08:00
&create.ID,
2024-01-06 17:12:10 +08:00
&create.CreatedTs,
&create.UpdatedTs,
&rowStatus,
); err != nil {
return nil, err
}
2024-04-13 11:54:37 +08:00
create.RowStatus = store.RowStatus(rowStatus)
2024-01-06 17:12:10 +08:00
webhook := create
return webhook, nil
}
2024-04-13 11:54:37 +08:00
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
2024-01-06 17:12:10 +08:00
where, args := []string{"1 = 1"}, []any{}
if find.ID != nil {
2024-01-06 17:12:10 +08:00
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
}
if find.CreatorID != nil {
2024-01-06 17:12:10 +08:00
where, args = append(where, "creator_id = "+placeholder(len(args)+1)), append(args, *find.CreatorID)
}
rows, err := d.db.QueryContext(ctx, `
SELECT
id,
created_ts,
updated_ts,
row_status,
creator_id,
name,
url
FROM webhook
WHERE `+strings.Join(where, " AND ")+`
ORDER BY id DESC`,
args...,
)
if err != nil {
return nil, err
}
defer rows.Close()
2024-04-13 11:54:37 +08:00
list := []*store.Webhook{}
for rows.Next() {
2024-04-13 11:54:37 +08:00
webhook := &store.Webhook{}
var rowStatus string
if err := rows.Scan(
2024-04-13 11:54:37 +08:00
&webhook.ID,
2023-12-09 23:19:57 +08:00
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
2024-04-13 11:54:37 +08:00
&webhook.CreatorID,
&webhook.Name,
2024-04-13 12:11:59 +08:00
&webhook.URL,
); err != nil {
return nil, err
}
2024-04-13 11:54:37 +08:00
webhook.RowStatus = store.RowStatus(rowStatus)
list = append(list, webhook)
}
if err := rows.Err(); err != nil {
return nil, err
}
return list, nil
}
2024-04-13 11:54:37 +08:00
func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
2024-01-06 17:12:10 +08:00
set, args := []string{}, []any{}
if update.RowStatus != nil {
2024-01-06 17:12:10 +08:00
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
}
if update.Name != nil {
2024-01-06 17:12:10 +08:00
set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
}
if update.URL != nil {
2024-01-06 17:12:10 +08:00
set, args = append(set, "url = "+placeholder(len(args)+1)), append(args, *update.URL)
}
stmt := "UPDATE webhook SET " + strings.Join(set, ", ") + " WHERE id = " + placeholder(len(args)+1) + " RETURNING id, created_ts, updated_ts, row_status, creator_id, name, url"
args = append(args, update.ID)
2024-04-13 11:54:37 +08:00
webhook := &store.Webhook{}
2024-01-06 17:12:10 +08:00
var rowStatus string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
2024-04-13 11:54:37 +08:00
&webhook.ID,
2024-01-06 17:12:10 +08:00
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
2024-04-13 11:54:37 +08:00
&webhook.CreatorID,
2024-01-06 17:12:10 +08:00
&webhook.Name,
2024-04-13 12:11:59 +08:00
&webhook.URL,
2024-01-06 17:12:10 +08:00
); err != nil {
return nil, err
}
2024-04-13 11:54:37 +08:00
webhook.RowStatus = store.RowStatus(rowStatus)
return webhook, nil
}
func (d *DB) DeleteWebhook(ctx context.Context, delete *store.DeleteWebhook) error {
2024-01-06 17:12:10 +08:00
_, err := d.db.ExecContext(ctx, "DELETE FROM webhook WHERE id = $1", delete.ID)
return err
}