The main server, and probably only repository in this org.
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.

77 lines
2.3 KiB

  1. package sqlite
  2. import (
  3. "context"
  4. "database/sql"
  5. "git.aiterp.net/lucifer/lucifer/models"
  6. )
  7. type lightRepository struct{}
  8. // LightRepository is a sqlite datbase repository for the Light model.
  9. var LightRepository models.LightRepository = &lightRepository{}
  10. func (r *lightRepository) FindByID(ctx context.Context, id int) (models.Light, error) {
  11. light := models.Light{}
  12. err := db.GetContext(ctx, &light, "SELECT * FROM light WHERE id=?", id)
  13. return light, err
  14. }
  15. func (r *lightRepository) FindByInternalID(ctx context.Context, internalID string) (models.Light, error) {
  16. light := models.Light{}
  17. err := db.GetContext(ctx, &light, "SELECT * FROM light WHERE internal_id=?", internalID)
  18. return light, err
  19. }
  20. func (r *lightRepository) List(ctx context.Context) ([]models.Light, error) {
  21. lights := make([]models.Light, 0, 64)
  22. err := db.SelectContext(ctx, &lights, "SELECT * FROM light")
  23. return lights, err
  24. }
  25. func (r *lightRepository) ListByBridge(ctx context.Context, bridge models.Bridge) ([]models.Light, error) {
  26. lights := make([]models.Light, 0, 64)
  27. err := db.SelectContext(ctx, &lights, "SELECT * FROM light WHERE bridge_id=?", bridge.ID)
  28. return lights, err
  29. }
  30. func (r *lightRepository) ListByGroup(ctx context.Context, group models.Group) ([]models.Light, error) {
  31. lights := make([]models.Light, 0, 64)
  32. err := db.SelectContext(ctx, &lights, "SELECT * FROM light WHERE group_id=?", group.ID)
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. }
  36. return lights, err
  37. }
  38. func (r *lightRepository) Insert(ctx context.Context, light models.Light) (models.Light, error) {
  39. res, err := db.NamedExecContext(ctx, "INSERT INTO light (bridge_id, internal_id, group_id, name, enabled, color, brightness) VALUES(:bridge_id, :internal_id, :group_id, :name, :enabled, :color, :brightness)", light)
  40. if err != nil {
  41. return models.Light{}, err
  42. }
  43. id, err := res.LastInsertId()
  44. if err != nil {
  45. return models.Light{}, err
  46. }
  47. light.ID = int(id)
  48. return light, nil
  49. }
  50. func (r *lightRepository) Update(ctx context.Context, light models.Light) error {
  51. _, err := db.NamedExecContext(ctx, "UPDATE light SET group_id=:group_id, name=:name, enabled=:enabled, color=:color, brightness=:brightness WHERE id=:id", light)
  52. return err
  53. }
  54. func (r *lightRepository) Remove(ctx context.Context, light models.Light) error {
  55. _, err := db.NamedExecContext(ctx, "DELETE FROM light WHERE id=:id", light)
  56. return err
  57. }