56 lines
1.3 KiB
C#
56 lines
1.3 KiB
C#
using Dapper;
|
|
using Shogi.Api.Repositories.Dto;
|
|
using Shogi.Domain;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Text.Json;
|
|
|
|
namespace Shogi.Api.Repositories;
|
|
|
|
public class SessionRepository : ISessionRepository
|
|
{
|
|
private readonly string connectionString;
|
|
|
|
public SessionRepository(IConfiguration configuration)
|
|
{
|
|
connectionString = configuration.GetConnectionString("ShogiDatabase");
|
|
}
|
|
|
|
public async Task CreateSession(ShogiBoard session, string player1)
|
|
{
|
|
var initialBoardState = JsonSerializer.Serialize(session.BoardState);
|
|
using var connection = new SqlConnection(connectionString);
|
|
await connection.ExecuteAsync(
|
|
"session.CreateSession",
|
|
new
|
|
{
|
|
SessionName = session.Name,
|
|
Player1Name = player1,
|
|
InitialBoardStateDocument = initialBoardState
|
|
},
|
|
commandType: CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task ReadSession(string name)
|
|
{
|
|
using var connection = new SqlConnection(connectionString);
|
|
var results = await connection.QueryAsync<SessionDto>(
|
|
"session.ReadSession",
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
if (!results.Any())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var dto = results.First();
|
|
return new Session(
|
|
name: dto.Name,
|
|
initialState: JsonSerializer.Deserialize< dto.BoardState)
|
|
}
|
|
}
|
|
|
|
public interface ISessionRepository
|
|
{
|
|
Task CreateSession(ShogiBoard session);
|
|
} |