50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
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<IEnumerable<SessionDto>> 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<SessionDto>();
|
|
}
|
|
|
|
public async Task<List<SessionStateDocument>> 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<SessionStateDocument>(20);
|
|
while (await reader.ReadAsync())
|
|
{
|
|
var json = reader.GetString("Document");
|
|
var document = JsonSerializer.Deserialize<SessionStateDocument>(json);
|
|
if (document != null)
|
|
{
|
|
documents.Add(document);
|
|
}
|
|
}
|
|
|
|
return documents;
|
|
}
|
|
}
|