Don't wipe out code slot names on restart

This commit is contained in:
Finn 2024-11-22 22:54:56 -08:00
parent cafbd63f98
commit 7077cf55fe
3 changed files with 29 additions and 5 deletions

View file

@ -129,8 +129,31 @@ func (q *Queries) GetLockCodesByCode(ctx context.Context, code string) ([]LockCo
return items, nil
}
const updateCodeSlot = `-- name: UpdateCodeSlot :exec
UPDATE lock_code_slots SET code = ?, enabled = ?, name = ? WHERE lock = ? AND slot = ?
`
type UpdateCodeSlotParams struct {
Code string
Enabled bool
Name string
Lock int64
Slot int64
}
func (q *Queries) UpdateCodeSlot(ctx context.Context, arg UpdateCodeSlotParams) error {
_, err := q.db.ExecContext(ctx, updateCodeSlot,
arg.Code,
arg.Enabled,
arg.Name,
arg.Lock,
arg.Slot,
)
return err
}
const upsertCodeSlot = `-- name: UpsertCodeSlot :exec
INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, ?) ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled, name=excluded.name
INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, "") ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled
`
type UpsertCodeSlotParams struct {
@ -138,7 +161,6 @@ type UpsertCodeSlotParams struct {
Slot int64
Code string
Enabled bool
Name string
}
func (q *Queries) UpsertCodeSlot(ctx context.Context, arg UpsertCodeSlotParams) error {
@ -147,7 +169,6 @@ func (q *Queries) UpsertCodeSlot(ctx context.Context, arg UpsertCodeSlotParams)
arg.Slot,
arg.Code,
arg.Enabled,
arg.Name,
)
return err
}

View file

@ -1,5 +1,8 @@
-- name: UpsertCodeSlot :exec
INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, ?) ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled, name=excluded.name;
INSERT INTO lock_code_slots (lock, slot, code, enabled, name) VALUES (?, ?, ?, ?, "") ON CONFLICT (lock, slot) DO UPDATE SET code=excluded.code, enabled=excluded.enabled;
-- name: UpdateCodeSlot :exec
UPDATE lock_code_slots SET code = ?, enabled = ?, name = ? WHERE lock = ? AND slot = ?;
-- name: GetLockCodeBySlot :one
SELECT * FROM lock_code_slots WHERE lock = ? AND slot = ?;

View file

@ -145,7 +145,7 @@ func lockCodeEditHandler(c echo.Context) error {
return fmt.Errorf("error pushing code to lock %s (ZWaveDeviceID=%d ID=%d): %v", lock.Name, lock.ZwaveDeviceID, lock.ID, err)
}
err = queries.UpsertCodeSlot(ctx, db.UpsertCodeSlotParams{
err = queries.UpdateCodeSlot(ctx, db.UpdateCodeSlotParams{
Lock: lockID,
Slot: slot,
Code: newCode,