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

package sqlite
import (
"context"
"database/sql"
"git.aiterp.net/lucifer/lucifer/models"
)
type lightRepository struct{}
// LightRepository is a sqlite datbase repository for the Light model.
var LightRepository models.LightRepository = &lightRepository{}
func (r *lightRepository) FindByID(ctx context.Context, id int) (models.Light, error) {
light := models.Light{}
err := db.GetContext(ctx, &light, "SELECT * FROM light WHERE id=?", id)
return light, err
}
func (r *lightRepository) FindByInternalID(ctx context.Context, internalID string) (models.Light, error) {
light := models.Light{}
err := db.GetContext(ctx, &light, "SELECT * FROM light WHERE internal_id=?", internalID)
return light, err
}
func (r *lightRepository) List(ctx context.Context) ([]models.Light, error) {
lights := make([]models.Light, 0, 64)
err := db.SelectContext(ctx, &lights, "SELECT * FROM light")
return lights, err
}
func (r *lightRepository) ListByBridge(ctx context.Context, bridge models.Bridge) ([]models.Light, error) {
lights := make([]models.Light, 0, 64)
err := db.SelectContext(ctx, &lights, "SELECT * FROM light WHERE bridge_id=?", bridge.ID)
return lights, err
}
func (r *lightRepository) ListByGroup(ctx context.Context, group models.Group) ([]models.Light, error) {
lights := make([]models.Light, 0, 64)
err := db.SelectContext(ctx, &lights, "SELECT * FROM light WHERE group_id=?", group.ID)
if err == sql.ErrNoRows {
err = nil
}
return lights, err
}
func (r *lightRepository) Insert(ctx context.Context, light models.Light) (models.Light, error) {
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)
if err != nil {
return models.Light{}, err
}
id, err := res.LastInsertId()
if err != nil {
return models.Light{}, err
}
light.ID = int(id)
return light, nil
}
func (r *lightRepository) Update(ctx context.Context, light models.Light) error {
_, err := db.NamedExecContext(ctx, "UPDATE light SET group_id=:group_id, name=:name, enabled=:enabled, color=:color, brightness=:brightness WHERE id=:id", light)
return err
}
func (r *lightRepository) Remove(ctx context.Context, light models.Light) error {
_, err := db.NamedExecContext(ctx, "DELETE FROM light WHERE id=:id", light)
return err
}