webhook.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, row_status"
  11. var rowStatus string
  12. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  13. &create.ID,
  14. &create.CreatedTs,
  15. &create.UpdatedTs,
  16. &rowStatus,
  17. ); err != nil {
  18. return nil, err
  19. }
  20. create.RowStatus = store.RowStatus(rowStatus)
  21. webhook := create
  22. return webhook, nil
  23. }
  24. func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
  25. where, args := []string{"1 = 1"}, []any{}
  26. if find.ID != nil {
  27. where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
  28. }
  29. if find.CreatorID != nil {
  30. where, args = append(where, "creator_id = "+placeholder(len(args)+1)), append(args, *find.CreatorID)
  31. }
  32. rows, err := d.db.QueryContext(ctx, `
  33. SELECT
  34. id,
  35. created_ts,
  36. updated_ts,
  37. row_status,
  38. creator_id,
  39. name,
  40. url
  41. FROM webhook
  42. WHERE `+strings.Join(where, " AND ")+`
  43. ORDER BY id DESC`,
  44. args...,
  45. )
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer rows.Close()
  50. list := []*store.Webhook{}
  51. for rows.Next() {
  52. webhook := &store.Webhook{}
  53. var rowStatus string
  54. if err := rows.Scan(
  55. &webhook.ID,
  56. &webhook.CreatedTs,
  57. &webhook.UpdatedTs,
  58. &rowStatus,
  59. &webhook.CreatorID,
  60. &webhook.Name,
  61. &webhook.URL,
  62. ); err != nil {
  63. return nil, err
  64. }
  65. webhook.RowStatus = store.RowStatus(rowStatus)
  66. list = append(list, webhook)
  67. }
  68. if err := rows.Err(); err != nil {
  69. return nil, err
  70. }
  71. return list, nil
  72. }
  73. func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
  74. set, args := []string{}, []any{}
  75. if update.RowStatus != nil {
  76. set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
  77. }
  78. if update.Name != nil {
  79. set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
  80. }
  81. if update.URL != nil {
  82. set, args = append(set, "url = "+placeholder(len(args)+1)), append(args, *update.URL)
  83. }
  84. 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"
  85. args = append(args, update.ID)
  86. webhook := &store.Webhook{}
  87. var rowStatus string
  88. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  89. &webhook.ID,
  90. &webhook.CreatedTs,
  91. &webhook.UpdatedTs,
  92. &rowStatus,
  93. &webhook.CreatorID,
  94. &webhook.Name,
  95. &webhook.URL,
  96. ); err != nil {
  97. return nil, err
  98. }
  99. webhook.RowStatus = store.RowStatus(rowStatus)
  100. return webhook, nil
  101. }
  102. func (d *DB) DeleteWebhook(ctx context.Context, delete *store.DeleteWebhook) error {
  103. _, err := d.db.ExecContext(ctx, "DELETE FROM webhook WHERE id = $1", delete.ID)
  104. return err
  105. }