2023-12-03 13:31:29 +08:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-06 17:12:10 +08:00
|
|
|
"strings"
|
2023-12-03 13:31:29 +08:00
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*storepb.Webhook, error) {
|
2024-01-06 17:12:10 +08:00
|
|
|
fields := []string{"name", "url", "creator_id"}
|
|
|
|
args := []any{create.Name, create.Url, create.CreatorId}
|
|
|
|
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(
|
|
|
|
&create.Id,
|
|
|
|
&create.CreatedTs,
|
|
|
|
&create.UpdatedTs,
|
|
|
|
&rowStatus,
|
|
|
|
); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-06 17:12:10 +08:00
|
|
|
create.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
|
|
|
|
webhook := create
|
|
|
|
return webhook, nil
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*storepb.Webhook, error) {
|
2024-01-06 17:12:10 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
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)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
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...,
|
|
|
|
)
|
2023-12-03 13:31:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
list := []*storepb.Webhook{}
|
|
|
|
for rows.Next() {
|
|
|
|
webhook := &storepb.Webhook{}
|
|
|
|
var rowStatus string
|
|
|
|
if err := rows.Scan(
|
|
|
|
&webhook.Id,
|
2023-12-09 23:19:57 +08:00
|
|
|
&webhook.CreatedTs,
|
|
|
|
&webhook.UpdatedTs,
|
2023-12-03 13:31:29 +08:00
|
|
|
&rowStatus,
|
|
|
|
&webhook.CreatorId,
|
|
|
|
&webhook.Name,
|
|
|
|
&webhook.Url,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
webhook.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
|
|
|
|
list = append(list, webhook)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*storepb.Webhook, error) {
|
2024-01-06 17:12:10 +08:00
|
|
|
set, args := []string{}, []any{}
|
2023-12-03 13:31:29 +08:00
|
|
|
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())
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
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)
|
2023-12-03 13:31:29 +08:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
webhook := &storepb.Webhook{}
|
|
|
|
var rowStatus string
|
|
|
|
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
|
|
|
&webhook.Id,
|
|
|
|
&webhook.CreatedTs,
|
|
|
|
&webhook.UpdatedTs,
|
|
|
|
&rowStatus,
|
|
|
|
&webhook.CreatorId,
|
|
|
|
&webhook.Name,
|
|
|
|
&webhook.Url,
|
|
|
|
); err != nil {
|
2023-12-03 13:31:29 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-06 17:12:10 +08:00
|
|
|
webhook.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
|
2023-12-03 13:31:29 +08:00
|
|
|
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)
|
2023-12-03 13:31:29 +08:00
|
|
|
return err
|
|
|
|
}
|