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
65 lines
2.1 KiB
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.aiterp.net/lucifer/lucifer/models"
|
|
)
|
|
|
|
type buttonRepository struct{}
|
|
|
|
// ButtonRepository is a sqlite datbase repository for the Button model.
|
|
var ButtonRepository models.ButtonRepository = &buttonRepository{}
|
|
|
|
func (r *buttonRepository) FindByID(ctx context.Context, id int) (models.Button, error) {
|
|
button := models.Button{}
|
|
err := db.GetContext(ctx, &button, "SELECT * FROM button WHERE id=?", id)
|
|
|
|
return button, err
|
|
}
|
|
|
|
func (r *buttonRepository) FindByInternalID(ctx context.Context, internalID string) (models.Button, error) {
|
|
button := models.Button{}
|
|
err := db.GetContext(ctx, &button, "SELECT * FROM button WHERE internal_id=?", internalID)
|
|
|
|
return button, err
|
|
}
|
|
|
|
func (r *buttonRepository) List(ctx context.Context) ([]models.Button, error) {
|
|
lights := make([]models.Button, 0, 64)
|
|
err := db.SelectContext(ctx, &lights, "SELECT * FROM button")
|
|
|
|
return lights, err
|
|
}
|
|
|
|
func (r *buttonRepository) ListByBridge(ctx context.Context, bridge models.Bridge) ([]models.Button, error) {
|
|
lights := make([]models.Button, 0, 64)
|
|
err := db.SelectContext(ctx, &lights, "SELECT * FROM button WHERE bridge_id=?", bridge.ID)
|
|
|
|
return lights, err
|
|
}
|
|
|
|
func (r *buttonRepository) Insert(ctx context.Context, button models.Button) (models.Button, error) {
|
|
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)
|
|
if err != nil {
|
|
return models.Button{}, err
|
|
}
|
|
|
|
id, err := res.LastInsertId()
|
|
if err != nil {
|
|
return models.Button{}, err
|
|
}
|
|
|
|
button.ID = int(id)
|
|
return button, nil
|
|
}
|
|
|
|
func (r *buttonRepository) Update(ctx context.Context, button models.Button) error {
|
|
_, err := db.NamedExecContext(ctx, "UPDATE button SET internal_index=:internal_index,missing=:missing WHERE id=:id", button)
|
|
return err
|
|
}
|
|
|
|
func (r *buttonRepository) Remove(ctx context.Context, button models.Button) error {
|
|
_, err := db.NamedExecContext(ctx, "DELETE FROM button WHERE id=:id", button)
|
|
return err
|
|
}
|