webhook.go 3.0 KB

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