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.
146 lines
3.5 KiB
146 lines
3.5 KiB
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.aiterp.net/lucifer/lucifer/models"
|
|
)
|
|
|
|
type lightRepository struct{}
|
|
|
|
// LightRepository is a sqlite datbase repository for the Light model.
|
|
var LightRepository = &lightRepository{}
|
|
|
|
type structScanner interface {
|
|
StructScan(v interface{}) error
|
|
}
|
|
|
|
func scanLight(row structScanner, light *models.Light) error {
|
|
scannedLight := struct {
|
|
ID int `db:"id"`
|
|
BridgeID int `db:"bridge_id"`
|
|
InternalID string `db:"internal_id"`
|
|
Name string `db:"name"`
|
|
On bool `db:"on"`
|
|
Color string `db:"color"`
|
|
}{}
|
|
|
|
if err := row.StructScan(&scannedLight); err != nil {
|
|
return err
|
|
}
|
|
|
|
light.ID = scannedLight.ID
|
|
light.BridgeID = scannedLight.BridgeID
|
|
light.InternalID = scannedLight.InternalID
|
|
light.Name = scannedLight.Name
|
|
light.On = scannedLight.On
|
|
|
|
return light.Color.Parse(scannedLight.Color)
|
|
}
|
|
|
|
func (r *lightRepository) FindByID(ctx context.Context, id int) (models.Light, error) {
|
|
row := db.QueryRowxContext(ctx, "SELECT * FROM light WHERE id=?", id)
|
|
if err := row.Err(); err != nil {
|
|
return models.Light{}, err
|
|
}
|
|
|
|
light := models.Light{}
|
|
if err := scanLight(row, &light); err != nil {
|
|
return models.Light{}, err
|
|
}
|
|
|
|
return light, nil
|
|
}
|
|
|
|
func (r *lightRepository) FindByInternalID(ctx context.Context, internalID string) (models.Light, error) {
|
|
row := db.QueryRowxContext(ctx, "SELECT * FROM light WHERE internal_id=?", internalID)
|
|
if err := row.Err(); err != nil {
|
|
return models.Light{}, err
|
|
}
|
|
|
|
light := models.Light{}
|
|
if err := scanLight(row, &light); err != nil {
|
|
return models.Light{}, err
|
|
}
|
|
|
|
return light, nil
|
|
}
|
|
|
|
func (r *lightRepository) List(ctx context.Context) ([]models.Light, error) {
|
|
res, err := db.QueryxContext(ctx, "SELECT * FROM light")
|
|
if err != nil {
|
|
return nil, err
|
|
} else if err := res.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lights := make([]models.Light, 0, 64)
|
|
for res.Next() {
|
|
light := models.Light{}
|
|
if err := scanLight(res, &light); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lights = append(lights, light)
|
|
}
|
|
|
|
return lights, nil
|
|
}
|
|
|
|
func (r *lightRepository) ListByBridge(ctx context.Context, bridge models.Bridge) ([]models.Light, error) {
|
|
res, err := db.QueryxContext(ctx, "SELECT * FROM light WHERE bridge_id=?", bridge.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if err := res.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lights := make([]models.Light, 0, 64)
|
|
for res.Next() {
|
|
light := models.Light{}
|
|
if err := scanLight(res, &light); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lights = append(lights, light)
|
|
}
|
|
|
|
return lights, nil
|
|
}
|
|
|
|
func (r *lightRepository) Insert(ctx context.Context, light models.Light) (models.Light, error) {
|
|
data := struct {
|
|
BridgeID int `db:"bridge_id"`
|
|
InternalID string `db:"internal_id"`
|
|
Name string `db:"name"`
|
|
On bool `db:"on"`
|
|
Color string `db:"color"`
|
|
}{
|
|
BridgeID: light.BridgeID,
|
|
InternalID: light.InternalID,
|
|
Name: light.Name,
|
|
On: light.On,
|
|
Color: light.Color.String(),
|
|
}
|
|
|
|
res, err := db.NamedExecContext(ctx, "INSERT INTO light (bridge_id, internal_id, name, `on`, color) VALUES(:bridge_id, :internal_id, :name, :on, :color)", data)
|
|
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 {
|
|
panic("not implemented")
|
|
}
|
|
|
|
func (r *lightRepository) Remove(ctx context.Context, light models.Light) error {
|
|
panic("not implemented")
|
|
}
|