This commit is contained in:
2022-11-10 19:25:55 -06:00
parent f7f752b694
commit 02b83efc88
9 changed files with 64 additions and 12 deletions

View File

@@ -39,7 +39,7 @@ public class SessionsController : ControllerBase
public async Task<IActionResult> CreateSession([FromBody] CreateSessionCommand request)
{
var userId = User.GetShogiUserId();
var session = new Domain.Aggregates.Session(request.Name, userId);
var session = new Domain.Session(request.Name, userId);
try
{
await sessionRepository.CreateSession(session);
@@ -126,7 +126,7 @@ public class SessionsController : ControllerBase
}
else
{
session.Board.Move(command.From!, command.To, command.IsPromotion);
session.Board.Move(command.From!, command.To, command.IsPromotion!.Value);
}
}
catch (InvalidOperationException)

View File

@@ -50,6 +50,7 @@ public static class ContractsExtensions
public static Dictionary<string, Piece?> ToContract(this ReadOnlyDictionary<string, Domain.ValueObjects.Piece?> boardState) =>
boardState.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToContract());
public static Domain.WhichPiece? ToDomain(this WhichPiece? piece) => piece.HasValue ? piece.Value.ToDomain() : null;
public static Domain.WhichPiece ToDomain(this WhichPiece piece)
{
return piece switch

View File

@@ -5,6 +5,6 @@ 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)
public readonly record struct MoveDto(string? From, string To, bool? IsPromotion, WhichPiece? PieceFromHand)
{
}

View File

@@ -1,6 +1,7 @@
using Dapper;
using Shogi.Api.Extensions;
using Shogi.Api.Repositories.Dto;
using Shogi.Domain.Aggregates;
using Shogi.Domain;
using System.Data;
using System.Data.SqlClient;
@@ -65,6 +66,15 @@ public class SessionRepository : ISessionRepository
}
return session;
}
public async Task CreateMove(string sessionName, Contracts.Api.MovePieceCommand command)
{
using var connection = new SqlConnection(connectionString);
await connection.ExecuteAsync(
"session.CreateMove",
new { },
commandType: CommandType.StoredProcedure);
}
}
public interface ISessionRepository

View File

@@ -7,6 +7,33 @@ namespace Shogi.Contracts.Api;
public class MovePieceCommand : IValidatableObject
{
/// <summary>
/// For serialization.
/// </summary>
public MovePieceCommand()
{
To = string.Empty;
}
/// <summary>
/// Move a piece on the board.
/// </summary>
public MovePieceCommand(string from, string to, bool isPromotion)
{
From = from;
To = to;
IsPromotion = isPromotion;
}
/// <summary>
/// Add a piece to the board from the hand.
/// </summary>
public MovePieceCommand(WhichPiece pieceFromHand, string to)
{
PieceFromHand = pieceFromHand;
To = to;
}
/// <summary>
/// Mutually exclusive with <see cref="From"/>.
/// Set this property to indicate moving a piece from the hand onto the board.
@@ -26,7 +53,7 @@ public class MovePieceCommand : IValidatableObject
[Required]
public string To { get; set; }
public bool IsPromotion { get; set; }
public bool? IsPromotion { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
@@ -34,6 +61,10 @@ public class MovePieceCommand : IValidatableObject
{
yield return new ValidationResult($"{nameof(PieceFromHand)} and {nameof(From)} are mutually exclusive properties.");
}
if (PieceFromHand.HasValue && IsPromotion.HasValue)
{
yield return new ValidationResult($"{nameof(PieceFromHand)} and {nameof(IsPromotion)} are mutually exclusive properties.");
}
if (!Regex.IsMatch(To, "[A-I][1-9]"))
{
yield return new ValidationResult($"{nameof(To)} must be a valid board position, between A1 and I9");

View File

@@ -0,0 +1,9 @@
CREATE PROCEDURE [session].[CreateMove]
@To VARCHAR(2),
@From VARCHAR(2),
@IsPromotion BIT,
@PieceName NVARCHAR(13),
@SessionName [session].[SessionName]
AS
SELECT @param1, @param2
RETURN 0

View File

@@ -2,9 +2,9 @@
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[SessionId] BIGINT NOT NULL,
[From] VARCHAR(2) NOT NULL,
[To] VARCHAR(2) NOT NULL,
[IsPromotion] BIT NOT NULL,
[From] VARCHAR(2) NULL,
[IsPromotion] BIT NULL,
[PieceIdFromHand] INT NULL
CONSTRAINT [Cannot end where you start]

View File

@@ -87,6 +87,7 @@
<Build Include="Session\Tables\Move.sql" />
<Build Include="Session\Tables\Piece.sql" />
<Build Include="Session\Stored Procedures\DeleteSession.sql" />
<Build Include="Session\Stored Procedures\CreateMove.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Post Deployment\Script.PostDeployment.sql" />

View File

@@ -1,6 +1,6 @@
using Shogi.Domain.ValueObjects;
namespace Shogi.Domain.Aggregates;
namespace Shogi.Domain;
public class Session
{