webhook.go 3.0 KB

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