webhook.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package postgres
  2. import (
  3. "context"
  4. "strings"
  5. storepb "github.com/usememos/memos/proto/gen/store"
  6. "github.com/usememos/memos/store"
  7. )
  8. func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*storepb.Webhook, error) {
  9. fields := []string{"name", "url", "creator_id"}
  10. args := []any{create.Name, create.Url, create.CreatorId}
  11. stmt := "INSERT INTO webhook (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") 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 = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
  22. webhook := create
  23. return webhook, nil
  24. }
  25. func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*storepb.Webhook, error) {
  26. where, args := []string{"1 = 1"}, []any{}
  27. if find.ID != nil {
  28. where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
  29. }
  30. if find.CreatorID != nil {
  31. where, args = append(where, "creator_id = "+placeholder(len(args)+1)), 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 := []*storepb.Webhook{}
  52. for rows.Next() {
  53. webhook := &storepb.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 = storepb.RowStatus(storepb.RowStatus_value[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) (*storepb.Webhook, error) {
  75. set, args := []string{}, []any{}
  76. if update.RowStatus != nil {
  77. set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
  78. }
  79. if update.Name != nil {
  80. set, args = append(set, "name = "+placeholder(len(args)+1)), append(args, *update.Name)
  81. }
  82. if update.URL != nil {
  83. set, args = append(set, "url = "+placeholder(len(args)+1)), append(args, *update.URL)
  84. }
  85. 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"
  86. args = append(args, update.ID)
  87. webhook := &storepb.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 = storepb.RowStatus(storepb.RowStatus_value[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 = $1", delete.ID)
  105. return err
  106. }