From 09885ef1e4b7610d05377596f02d08c5079c0434 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 22 Jan 2025 00:46:49 +0100 Subject: feat(webui): implement states list --- pkg/database/states.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'pkg/database/states.go') diff --git a/pkg/database/states.go b/pkg/database/states.go index d88e717..1bd3d76 100644 --- a/pkg/database/states.go +++ b/pkg/database/states.go @@ -6,6 +6,8 @@ import ( "fmt" "slices" "time" + + "git.adyxax.org/adyxax/tfstated/pkg/model" ) // returns true in case of successful deletion @@ -43,6 +45,34 @@ func (db *DB) GetState(path string) ([]byte, error) { return db.dataEncryptionKey.DecryptAES256(encryptedData) } +func (db *DB) LoadStatesByPath() ([]model.State, error) { + rows, err := db.Query( + `SELECT created, id, lock, path, updated FROM states;`) + if err != nil { + return nil, fmt.Errorf("failed to load states from database: %w", err) + } + defer rows.Close() + states := make([]model.State, 0) + for rows.Next() { + var state model.State + var ( + created int64 + updated int64 + ) + err = rows.Scan(&created, &state.Id, &state.Lock, &state.Path, &updated) + if err != nil { + return nil, fmt.Errorf("failed to load states from row: %w", err) + } + state.Created = time.Unix(created, 0) + state.Updated = time.Unix(updated, 0) + states = append(states, state) + } + if err := rows.Err(); err != nil { + return nil, fmt.Errorf("failed to load states from rows: %w", err) + } + return states, nil +} + // returns true in case of id mismatch func (db *DB) SetState(path string, accountID int, data []byte, lockID string) (bool, error) { encryptedData, err := db.dataEncryptionKey.EncryptAES256(data) -- cgit v1.2.3