webhook.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package postgres
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/usememos/memos/store"
  6. )
  7. func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
  8. fields := []string{"name", "url", "creator_id"}
  9. args := []any{create.Name, create.URL, create.CreatorID}
  10. stmt := "INSERT INTO webhook (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts"
  11. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  12. &create.ID,
  13. &create.CreatedTs,
  14. &create.UpdatedTs,
  15. ); err != nil {
  16. return nil, err
  17. }
  18. webhook := create
  19. return webhook, nil
  20. }
  21. func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
  22. where, args := []string{"1 = 1"}, []any{}
  23. if find.ID != nil {
  24. where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
  25. }
  26. if find.CreatorID != nil {
  27. where, args = append(where, "creator_id = "+placeholder(len(args)+1)), append(args, *find.CreatorID)
  28. }
  29. rows, err := d.db.QueryContext(ctx, `
  30. SELECT
  31. id,
  32. created_ts,
  33. updated_ts,
  34. creator_id,
  35. name,
  36. url
  37. FROM webhook
  38. WHERE `+strings.Join(where, " AND ")+`
  39. ORDER BY id DESC`,
  40. args...,
  41. )
  42. if err != nil {
  43. return nil, err
  44. }
  45. defer rows.Close()
  46. list := []*store.Webhook{}
  47. for rows.Next() {
  48. webhook := &store.Webhook{}
  49. if err := rows.Scan(
  50. &webhook.ID,
  51. &webhook.CreatedTs,
  52. &webhook.UpdatedTs,
  53. &webhook.CreatorID,
  54. &webhook.Name,
  55. &webhook.URL,
  56. ); err != nil {
  57. return nil, err
  58. }
  59. list = append(list, webhook)
  60. }
  61. if err := rows.Err(); err != nil {
  62. return nil, err
  63. }
  64. return list, nil
  65. }
  66. func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
  67. set, args := []string{}, []any{}
  68. if update.Name != nil {
  69. set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
  70. }
  71. if update.URL != nil {
  72. set, args = append(set, "url = "+placeholder(len(args)+1)), append(args, *update.URL)
  73. }
  74. stmt := "UPDATE webhook SET " + strings.Join(set, ", ") + " WHERE id = " + placeholder(len(args)+1) + " RETURNING id, created_ts, updated_ts, creator_id, name, url"
  75. args = append(args, update.ID)
  76. webhook := &store.Webhook{}
  77. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  78. &webhook.ID,
  79. &webhook.CreatedTs,
  80. &webhook.UpdatedTs,
  81. &webhook.CreatorID,
  82. &webhook.Name,
  83. &webhook.URL,
  84. ); err != nil {
  85. return nil, err
  86. }
  87. return webhook, nil
  88. }
  89. func (d *DB) DeleteWebhook(ctx context.Context, delete *store.DeleteWebhook) error {
  90. _, err := d.db.ExecContext(ctx, "DELETE FROM webhook WHERE id = $1", delete.ID)
  91. return err
  92. }