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.

123 lines
2.5 KiB

  1. package mysql
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "git.aiterp.net/lucifer/new-server/models"
  7. "github.com/jmoiron/sqlx"
  8. )
  9. type sceneRecord struct {
  10. ID int `db:"id"`
  11. Name string `db:"name"`
  12. IntervalMS int64 `db:"interval_ms"`
  13. RoleJSON json.RawMessage `db:"roles"`
  14. }
  15. type SceneRepo struct {
  16. DBX *sqlx.DB
  17. }
  18. func (r *SceneRepo) Find(ctx context.Context, id int) (*models.Scene, error) {
  19. var scene sceneRecord
  20. err := r.DBX.GetContext(ctx, &scene, "SELECT * FROM scene WHERE id = ?", id)
  21. if err != nil {
  22. return nil, dbErr(err)
  23. }
  24. return r.populateOne(&scene)
  25. }
  26. func (r *SceneRepo) FetchAll(ctx context.Context) ([]models.Scene, error) {
  27. scenes := make([]sceneRecord, 0, 8)
  28. err := r.DBX.SelectContext(ctx, &scenes, "SELECT * FROM scene")
  29. if err != nil {
  30. if err == sql.ErrNoRows {
  31. return []models.Scene{}, nil
  32. }
  33. return nil, dbErr(err)
  34. }
  35. return r.populate(scenes)
  36. }
  37. func (r *SceneRepo) Save(ctx context.Context, scene *models.Scene) error {
  38. j, err := json.Marshal(scene.Roles)
  39. if err != nil {
  40. return err
  41. }
  42. if scene.ID > 0 {
  43. _, err := r.DBX.ExecContext(
  44. ctx,
  45. "UPDATE scene SET name = ?, interval_ms = ?, roles = ? WHERE id = ?",
  46. scene.Name, scene.IntervalMS, j, scene.ID,
  47. )
  48. if err != nil {
  49. return dbErr(err)
  50. }
  51. } else {
  52. rs, err := r.DBX.ExecContext(
  53. ctx,
  54. "INSERT INTO scene (name, interval_ms, roles) VALUES (?, ?, ?)",
  55. scene.Name, scene.IntervalMS, j,
  56. )
  57. if err != nil {
  58. return dbErr(err)
  59. }
  60. id, err := rs.LastInsertId()
  61. if err != nil {
  62. return dbErr(err)
  63. }
  64. scene.ID = int(id)
  65. }
  66. return nil
  67. }
  68. func (r *SceneRepo) Delete(ctx context.Context, scene *models.Scene) error {
  69. _, err := r.DBX.ExecContext(ctx, "DELETE FROM scene WHERE id = ?", scene.ID)
  70. if err != nil {
  71. return dbErr(err)
  72. }
  73. scene.ID = 0
  74. return nil
  75. }
  76. func (r *SceneRepo) populateOne(record *sceneRecord) (*models.Scene, error) {
  77. records, err := r.populate([]sceneRecord{*record})
  78. if err != nil {
  79. return nil, err
  80. }
  81. return &records[0], nil
  82. }
  83. func (r *SceneRepo) populate(records []sceneRecord) ([]models.Scene, error) {
  84. res := make([]models.Scene, 0, len(records))
  85. for _, record := range records {
  86. scene := models.Scene{
  87. ID: record.ID,
  88. Name: record.Name,
  89. IntervalMS: record.IntervalMS,
  90. Roles: make([]models.SceneRole, 0, 8),
  91. }
  92. err := json.Unmarshal(record.RoleJSON, &scene.Roles)
  93. if err != nil {
  94. return nil, dbErr(err)
  95. }
  96. res = append(res, scene)
  97. }
  98. return res, nil
  99. }