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.

65 lines
2.1 KiB

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