You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.8 KiB

  1. package mysql
  2. import (
  3. "context"
  4. "encoding/json"
  5. "git.aiterp.net/lucifer/new-server/models"
  6. "github.com/jmoiron/sqlx"
  7. )
  8. type eventHandlerRecord struct {
  9. ID int `db:"id"`
  10. EventName string `db:"event_name"`
  11. OneShot bool `db:"one_shot"`
  12. Priority int `db:"priority"`
  13. TargetKind string `db:"target_kind"`
  14. TargetValue string `db:"target_value"`
  15. ConditionsJSON json.RawMessage `db:"conditions"`
  16. ActionsJSON json.RawMessage `db:"actions"`
  17. }
  18. type EventHandlerRepo struct {
  19. DBX *sqlx.DB
  20. }
  21. func (r *EventHandlerRepo) FindByID(ctx context.Context, id int) (*models.EventHandler, error) {
  22. var record eventHandlerRecord
  23. err := r.DBX.GetContext(ctx, &record, "SELECT * FROM event_handler WHERE id = ?", id)
  24. if err != nil {
  25. return nil, dbErr(err)
  26. }
  27. return r.populateOne(&record)
  28. }
  29. func (r *EventHandlerRepo) FetchAll(ctx context.Context) ([]models.EventHandler, error) {
  30. var records []eventHandlerRecord
  31. err := r.DBX.SelectContext(ctx, &records, "SELECT * FROM event_handler ORDER BY priority DESC, id")
  32. if err != nil {
  33. return nil, dbErr(err)
  34. }
  35. return r.populate(records)
  36. }
  37. func (r *EventHandlerRepo) Save(ctx context.Context, handler *models.EventHandler) error {
  38. conditionsJson, err := json.Marshal(handler.Conditions)
  39. if err != nil {
  40. return dbErr(err)
  41. }
  42. actionsJson, err := json.Marshal(handler.Actions)
  43. if err != nil {
  44. return dbErr(err)
  45. }
  46. record := eventHandlerRecord{
  47. ID: handler.ID,
  48. EventName: handler.EventName,
  49. OneShot: handler.OneShot,
  50. Priority: handler.Priority,
  51. TargetKind: string(handler.TargetKind),
  52. TargetValue: handler.TargetValue,
  53. ConditionsJSON: conditionsJson,
  54. ActionsJSON: actionsJson,
  55. }
  56. if record.ID == 0 {
  57. res, err := r.DBX.NamedExecContext(ctx, `
  58. INSERT INTO event_handler (event_name, one_shot, priority, target_kind, target_value, conditions, actions)
  59. VALUES (:event_name, :one_shot, :priority, :target_kind, :target_value, :conditions, :actions);
  60. `, record)
  61. if err != nil {
  62. return dbErr(err)
  63. }
  64. id, err := res.LastInsertId()
  65. if err != nil {
  66. return dbErr(err)
  67. }
  68. handler.ID = int(id)
  69. } else {
  70. _, err := r.DBX.NamedExecContext(ctx, `
  71. UPDATE event_handler SET
  72. event_name = :event_name,
  73. one_shot = :one_shot,
  74. priority = :priority,
  75. target_kind = :target_kind,
  76. target_value = :target_value,
  77. conditions = :conditions,
  78. actions = :actions
  79. WHERE id = :id
  80. `, record)
  81. if err != nil {
  82. return dbErr(err)
  83. }
  84. }
  85. return nil
  86. }
  87. func (r *EventHandlerRepo) Delete(ctx context.Context, handler *models.EventHandler) error {
  88. _, err := r.DBX.ExecContext(ctx, "DELETE FROM event_handler WHERE id=?", handler.ID)
  89. if err != nil {
  90. return dbErr(err)
  91. }
  92. return nil
  93. }
  94. func (r *EventHandlerRepo) populateOne(record *eventHandlerRecord) (*models.EventHandler, error) {
  95. records, err := r.populate([]eventHandlerRecord{*record})
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &records[0], nil
  100. }
  101. func (r *EventHandlerRepo) populate(records []eventHandlerRecord) ([]models.EventHandler, error) {
  102. res := make([]models.EventHandler, 0, len(records))
  103. for _, record := range records {
  104. handler := models.EventHandler{
  105. ID: record.ID,
  106. EventName: record.EventName,
  107. OneShot: record.OneShot,
  108. Priority: record.Priority,
  109. TargetKind: models.ReferenceKind(record.TargetKind),
  110. TargetValue: record.TargetValue,
  111. Conditions: make(map[string]models.EventCondition),
  112. }
  113. err := json.Unmarshal(record.ConditionsJSON, &handler.Conditions)
  114. if err != nil {
  115. return nil, dbErr(err)
  116. }
  117. err = json.Unmarshal(record.ActionsJSON, &handler.Actions)
  118. if err != nil {
  119. return nil, dbErr(err)
  120. }
  121. res = append(res, handler)
  122. }
  123. return res, nil
  124. }