using Dapper; using Shogi.Api.Repositories.Dto; using Shogi.Api.Repositories.Dto.SessionState; using System.Data; using System.Data.SqlClient; using System.Text.Json; namespace Shogi.Api.Repositories; public class QueryRepository(IConfiguration configuration) { private readonly string connectionString = configuration.GetConnectionString("ShogiDatabase") ?? throw new InvalidOperationException("No database configured for QueryRepository."); public async Task> ReadSessionsMetadata(string playerId) { using var connection = new SqlConnection(this.connectionString); var results = await connection.QueryMultipleAsync( "session.ReadSessionsMetadata", new { PlayerId = playerId }, commandType: CommandType.StoredProcedure); return await results.ReadAsync(); } public async Task> ReadSessionSnapshots(string sessionId) { using var connection = new SqlConnection(this.connectionString); var command = connection.CreateCommand(); command.CommandText = "session.ReadStatesBySession"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("SessionId", sessionId); await using var reader = await command.ExecuteReaderAsync(); var documents = new List(20); while (await reader.ReadAsync()) { var json = reader.GetString("Document"); var document = JsonSerializer.Deserialize(json); if (document != null) { documents.Add(document); } } return documents; } }