create, read, playercount

This commit is contained in:
2022-11-09 16:08:04 -06:00
parent a1f996e508
commit da76917490
37 changed files with 999 additions and 814 deletions

View File

@@ -1,22 +0,0 @@
using Shogi.Domain;
using Shogi.Domain.ValueObjects;
using System.Collections.ObjectModel;
namespace Shogi.Api.Repositories.Dto;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class BoardStateDto
{
public ReadOnlyDictionary<string, Piece?> State { get; set; }
public List<Piece> Player1Hand { get; set; }
public List<Piece> Player2Hand { get; set; }
public Move PreviousMove { get; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; }
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

View File

@@ -0,0 +1,10 @@
using Shogi.Domain;
namespace Shogi.Api.Repositories.Dto;
/// <summary>
/// Useful with Dapper to read from database.
/// </summary>
public readonly record struct MoveDto(string From, string To, bool IsPromotion, WhichPiece? PieceFromHand)
{
}

View File

@@ -1,14 +1,5 @@
namespace Shogi.Api.Repositories.Dto;
/// <summary>
/// Useful with Dapper to read from database.
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class SessionDto
public readonly record struct SessionDto(string Name, string Player1, string Player2)
{
public string Name { get; set; }
public string Player1 { get; set; }
public string Player2 { get; set; }
public BoardStateDto BoardState { get; set; }
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

View File

@@ -1,5 +1,4 @@
using Dapper;
using Shogi.Api.Repositories.Dto;
using Shogi.Contracts.Types;
using System.Data.SqlClient;
@@ -14,16 +13,16 @@ public class QueryRepository : IQueryRespository
connectionString = configuration.GetConnectionString("ShogiDatabase");
}
public async Task<IEnumerable<SessionMetadata>> ReadAllSessionsMetadata()
public async Task<IEnumerable<SessionMetadata>> ReadSessionPlayerCount()
{
using var connection = new SqlConnection(connectionString);
return await connection.QueryAsync<SessionMetadata>(
"session.ReadAllSessionsMetadata",
"session.ReadSessionPlayerCount",
commandType: System.Data.CommandType.StoredProcedure);
}
}
public interface IQueryRespository
{
Task<IEnumerable<SessionMetadata>> ReadAllSessionsMetadata();
Task<IEnumerable<SessionMetadata>> ReadSessionPlayerCount();
}

View File

@@ -1,11 +1,8 @@
using Dapper;
using Shogi.Api.Repositories.Dto;
using Shogi.Domain;
using Shogi.Domain.Aggregates;
using Shogi.Domain.ValueObjects;
using System.Data;
using System.Data.SqlClient;
using System.Text.Json;
namespace Shogi.Api.Repositories;
@@ -20,45 +17,52 @@ public class SessionRepository : ISessionRepository
public async Task CreateSession(Session session)
{
var boardStateDto = new BoardStateDto
{
InCheck = session.BoardState.InCheck,
IsCheckmate = session.BoardState.IsCheckmate,
Player1Hand = session.BoardState.Player1Hand,
Player2Hand = session.BoardState.Player2Hand,
State = session.BoardState.State,
WhoseTurn = session.BoardState.WhoseTurn,
};
using var connection = new SqlConnection(connectionString);
await connection.ExecuteAsync(
"session.CreateSession",
new
{
Name = sessionName,
InitialBoardStateDocument = JsonSerializer.Serialize(boardStateDto),
Player1Name = player1,
session.Name,
Player1Name = session.Player1,
},
commandType: CommandType.StoredProcedure);
}
public async Task<ShogiBoard?> ReadShogiBoard(string name)
public async Task DeleteSession(string name)
{
using var connection = new SqlConnection(connectionString);
var results = await connection.QueryAsync<SessionDto>(
"session.ReadSession",
await connection.ExecuteAsync(
"session.DeleteSession",
new { Name = name },
commandType: CommandType.StoredProcedure);
var dto = results.SingleOrDefault();
if (dto == null) return null;
}
var boardState = new BoardState(
state: new(dto.BoardState.State),
player1Hand: dto.BoardState.Player1Hand,
player2Hand: dto.BoardState.Player2Hand,
whoseTurn: dto.BoardState.WhoseTurn,
playerInCheck: dto.BoardState.InCheck,
previousMove: dto.BoardState.PreviousMove);
var session = new ShogiBoard(boardState);
public async Task<Session?> ReadSession(string name)
{
using var connection = new SqlConnection(connectionString);
var results = await connection.QueryMultipleAsync(
"session.ReadSession",
new { Name = name },
commandType: CommandType.StoredProcedure);
var sessionDtos = await results.ReadAsync<SessionDto>();
if (!sessionDtos.Any()) return null;
var dto = sessionDtos.First();
var session = new Session(dto.Name, dto.Player1);
if (!string.IsNullOrWhiteSpace(dto.Player2)) session.AddPlayer2(dto.Player2);
var moveDtos = await results.ReadAsync<MoveDto>();
foreach (var move in moveDtos)
{
if (move.PieceFromHand.HasValue)
{
session.Board.Move(move.PieceFromHand.Value, move.To);
}
else
{
session.Board.Move(move.From, move.To, false);
}
}
return session;
}
}
@@ -66,5 +70,6 @@ public class SessionRepository : ISessionRepository
public interface ISessionRepository
{
Task CreateSession(Session session);
Task<ShogiBoard?> ReadShogiBoard(string name);
Task DeleteSession(string name);
Task<Session?> ReadSession(string name);
}