mirror of https://github.com/usememos/memos
refactor: webhook service
parent
03399a6007
commit
d6a75bba4c
@ -1,108 +0,0 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
|
||||
fields := []string{"`name`", "`url`", "`creator_id`"}
|
||||
placeholder := []string{"?", "?", "?"}
|
||||
args := []any{create.Name, create.URL, create.CreatorID}
|
||||
|
||||
stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
|
||||
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
create.ID = int32(id)
|
||||
return d.GetWebhook(ctx, &store.FindWebhook{ID: &create.ID})
|
||||
}
|
||||
|
||||
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "`id` = ?"), append(args, *find.ID)
|
||||
}
|
||||
if find.CreatorID != nil {
|
||||
where, args = append(where, "`creator_id` = ?"), append(args, *find.CreatorID)
|
||||
}
|
||||
|
||||
rows, err := d.db.QueryContext(ctx, "SELECT `id`, UNIX_TIMESTAMP(`created_ts`), UNIX_TIMESTAMP(`updated_ts`), `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()
|
||||
|
||||
list := []*store.Webhook{}
|
||||
for rows.Next() {
|
||||
webhook := &store.Webhook{}
|
||||
if err := rows.Scan(
|
||||
&webhook.ID,
|
||||
&webhook.CreatedTs,
|
||||
&webhook.UpdatedTs,
|
||||
&webhook.CreatorID,
|
||||
&webhook.Name,
|
||||
&webhook.URL,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, webhook)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (d *DB) GetWebhook(ctx context.Context, find *store.FindWebhook) (*store.Webhook, error) {
|
||||
list, err := d.ListWebhooks(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.Name != nil {
|
||||
set, args = append(set, "`name` = ?"), append(args, *update.Name)
|
||||
}
|
||||
if update.URL != nil {
|
||||
set, args = append(set, "`url` = ?"), append(args, *update.URL)
|
||||
}
|
||||
args = append(args, update.ID)
|
||||
|
||||
stmt := "UPDATE `webhook` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
|
||||
_, err := d.db.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
webhook, err := d.GetWebhook(ctx, &store.FindWebhook{ID: &update.ID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return webhook, nil
|
||||
}
|
||||
|
||||
func (d *DB) DeleteWebhook(ctx context.Context, delete *store.DeleteWebhook) error {
|
||||
_, err := d.db.ExecContext(ctx, "DELETE FROM `webhook` WHERE `id` = ?", delete.ID)
|
||||
return err
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
|
||||
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"
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&create.ID,
|
||||
&create.CreatedTs,
|
||||
&create.UpdatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhook := create
|
||||
return webhook, nil
|
||||
}
|
||||
|
||||
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
|
||||
}
|
||||
if find.CreatorID != nil {
|
||||
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,
|
||||
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()
|
||||
|
||||
list := []*store.Webhook{}
|
||||
for rows.Next() {
|
||||
webhook := &store.Webhook{}
|
||||
if err := rows.Scan(
|
||||
&webhook.ID,
|
||||
&webhook.CreatedTs,
|
||||
&webhook.UpdatedTs,
|
||||
&webhook.CreatorID,
|
||||
&webhook.Name,
|
||||
&webhook.URL,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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) (*store.Webhook, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.Name != nil {
|
||||
set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
|
||||
}
|
||||
if update.URL != nil {
|
||||
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, creator_id, name, url"
|
||||
args = append(args, update.ID)
|
||||
webhook := &store.Webhook{}
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&webhook.ID,
|
||||
&webhook.CreatedTs,
|
||||
&webhook.UpdatedTs,
|
||||
&webhook.CreatorID,
|
||||
&webhook.Name,
|
||||
&webhook.URL,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return webhook, nil
|
||||
}
|
||||
|
||||
func (d *DB) DeleteWebhook(ctx context.Context, delete *store.DeleteWebhook) error {
|
||||
_, err := d.db.ExecContext(ctx, "DELETE FROM webhook WHERE id = $1", delete.ID)
|
||||
return err
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
|
||||
fields := []string{"`name`", "`url`", "`creator_id`"}
|
||||
placeholder := []string{"?", "?", "?"}
|
||||
args := []any{create.Name, create.URL, create.CreatorID}
|
||||
stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`"
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&create.ID,
|
||||
&create.CreatedTs,
|
||||
&create.UpdatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhook := create
|
||||
return webhook, nil
|
||||
}
|
||||
|
||||
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "`id` = ?"), append(args, *find.ID)
|
||||
}
|
||||
if find.CreatorID != nil {
|
||||
where, args = append(where, "`creator_id` = ?"), append(args, *find.CreatorID)
|
||||
}
|
||||
|
||||
rows, err := d.db.QueryContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
created_ts,
|
||||
updated_ts,
|
||||
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()
|
||||
|
||||
list := []*store.Webhook{}
|
||||
for rows.Next() {
|
||||
webhook := &store.Webhook{}
|
||||
if err := rows.Scan(
|
||||
&webhook.ID,
|
||||
&webhook.CreatedTs,
|
||||
&webhook.UpdatedTs,
|
||||
&webhook.CreatorID,
|
||||
&webhook.Name,
|
||||
&webhook.URL,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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) (*store.Webhook, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.Name != nil {
|
||||
set, args = append(set, "name = ?"), append(args, *update.Name)
|
||||
}
|
||||
if update.URL != nil {
|
||||
set, args = append(set, "url = ?"), append(args, *update.URL)
|
||||
}
|
||||
args = append(args, update.ID)
|
||||
|
||||
stmt := "UPDATE `webhook` SET " + strings.Join(set, ", ") + " WHERE `id` = ? RETURNING `id`, `created_ts`, `updated_ts`, `creator_id`, `name`, `url`"
|
||||
webhook := &store.Webhook{}
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&webhook.ID,
|
||||
&webhook.CreatedTs,
|
||||
&webhook.UpdatedTs,
|
||||
&webhook.CreatorID,
|
||||
&webhook.Name,
|
||||
&webhook.URL,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return webhook, nil
|
||||
}
|
||||
|
||||
func (d *DB) DeleteWebhook(ctx context.Context, delete *store.DeleteWebhook) error {
|
||||
_, err := d.db.ExecContext(ctx, "DELETE FROM `webhook` WHERE `id` = ?", delete.ID)
|
||||
return err
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package teststore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
|
||||
func TestWebhookStore(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ts := NewTestingStore(ctx, t)
|
||||
user, err := createTestingHostUser(ctx, ts)
|
||||
require.NoError(t, err)
|
||||
webhook, err := ts.CreateWebhook(ctx, &store.Webhook{
|
||||
CreatorID: user.ID,
|
||||
Name: "test_webhook",
|
||||
URL: "https://example.com",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "test_webhook", webhook.Name)
|
||||
require.Equal(t, user.ID, webhook.CreatorID)
|
||||
webhooks, err := ts.ListWebhooks(ctx, &store.FindWebhook{
|
||||
CreatorID: &user.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(webhooks))
|
||||
require.Equal(t, webhook, webhooks[0])
|
||||
newName := "test_webhook_new"
|
||||
updatedWebhook, err := ts.UpdateWebhook(ctx, &store.UpdateWebhook{
|
||||
ID: webhook.ID,
|
||||
Name: &newName,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, newName, updatedWebhook.Name)
|
||||
require.Equal(t, webhook.CreatorID, updatedWebhook.CreatorID)
|
||||
err = ts.DeleteWebhook(ctx, &store.DeleteWebhook{
|
||||
ID: webhook.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
webhooks, err = ts.ListWebhooks(ctx, &store.FindWebhook{
|
||||
CreatorID: &user.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0, len(webhooks))
|
||||
ts.Close()
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Webhook struct {
|
||||
ID int32
|
||||
CreatedTs int64
|
||||
UpdatedTs int64
|
||||
CreatorID int32
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
type FindWebhook struct {
|
||||
ID *int32
|
||||
CreatorID *int32
|
||||
}
|
||||
|
||||
type UpdateWebhook struct {
|
||||
ID int32
|
||||
Name *string
|
||||
URL *string
|
||||
}
|
||||
|
||||
type DeleteWebhook struct {
|
||||
ID int32
|
||||
}
|
||||
|
||||
func (s *Store) CreateWebhook(ctx context.Context, create *Webhook) (*Webhook, error) {
|
||||
return s.driver.CreateWebhook(ctx, create)
|
||||
}
|
||||
|
||||
func (s *Store) ListWebhooks(ctx context.Context, find *FindWebhook) ([]*Webhook, error) {
|
||||
return s.driver.ListWebhooks(ctx, find)
|
||||
}
|
||||
|
||||
func (s *Store) GetWebhook(ctx context.Context, find *FindWebhook) (*Webhook, error) {
|
||||
list, err := s.ListWebhooks(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (s *Store) UpdateWebhook(ctx context.Context, update *UpdateWebhook) (*Webhook, error) {
|
||||
return s.driver.UpdateWebhook(ctx, update)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteWebhook(ctx context.Context, delete *DeleteWebhook) error {
|
||||
return s.driver.DeleteWebhook(ctx, delete)
|
||||
}
|
Loading…
Reference in New Issue