using Aegis.Application.Abstractions; using Aegis.Domain; using Aegis.Repository.SQLite.Queries; namespace Aegis.Repository.SQLite.Repositories; public class DataStoreReadRepository(SqliteConnectionFactory factory) : IDataStoreReadRepository { public async Task> ListForUserAsync(UserId userId, CancellationToken ct) { await using var conn = factory.Open(); await using var cmd = conn.CreateCommand(); cmd.CommandText = DataStoreQueries.ListForUser; cmd.Parameters.AddWithValue("$userId", userId.Value); var items = new List(); await using var reader = await cmd.ExecuteReaderAsync(ct); while (await reader.ReadAsync(ct)) { var datastoreId = new DataStoreId(reader.GetString(0)); var role = ParseRole(reader.GetString(1)); var nameEnc = (byte[])reader["name_enc"]; var nameNonce = (byte[])reader["name_nonce"]; var lkKid = reader.GetString(4); var lkVersion = reader.GetInt32(5); items.Add(new DataStoreRow(datastoreId, role, nameEnc, nameNonce, lkKid, lkVersion)); } return items; } private static DataStoreRole ParseRole(string role) => role.ToUpperInvariant() switch { "OWNER" => DataStoreRole.Owner, "ADMIN" => DataStoreRole.Admin, "EDITOR" => DataStoreRole.Editor, "VIEWER" => DataStoreRole.Viewer, _ => throw new InvalidOperationException($"Unknown role: {role}") }; }