webhook.go 2.9 KB

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