This commit is contained in:
2022-10-31 08:08:58 -05:00
parent 689de35c3b
commit 2241ab23fe
6 changed files with 14 additions and 30 deletions

View File

@@ -1,14 +1,14 @@
CREATE PROCEDURE [session].[CreateSession]
@InitialBoardStateDocument [session].[JsonDocument]
@InitialBoardStateDocument [session].[JsonDocument],
@Player1Name [user].[UserName]
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [session].[Session] ([Name], BoardState, Player1Id)
INSERT INTO [session].[Session] (BoardState, Player1Id)
SELECT
JSON_VALUE(@InitialBoardStateDocument, '$.Name'),
@InitialBoardStateDocument,
Id
FROM [user].[User]
WHERE [Name] = JSON_VALUE(@InitialBoardStateDocument, '$.Player1');
WHERE [Name] = @Player1Name
END

View File

@@ -6,7 +6,6 @@ BEGIN
SELECT
sess.[Name],
GameOver,
BoardState,
p1.[Name] as Player1,
p2.[Name] as Player2

View File

@@ -2,12 +2,12 @@
(
Id BIGINT NOT NULL PRIMARY KEY IDENTITY,
Created DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET(),
DomainDocument [session].[JsonDocument] NOT NULL,
[Name] AS JSON_VALUE(DomainDocument, '$.Name') UNIQUE,
Player1Id BIGINT NOT NULL,
Player2Id BIGINT NULL,
BoardState [session].[JsonDocument] NOT NULL,
[Name] AS JSON_VALUE(BoardState, '$.Name') UNIQUE,
CONSTRAINT [BoardState must be json] CHECK (isjson(DomainDocument)=1),
CONSTRAINT [BoardState must be json] CHECK (isjson(BoardState)=1),
CONSTRAINT FK_Player1_User FOREIGN KEY (Player1Id) REFERENCES [user].[User] (Id)
ON DELETE CASCADE
ON UPDATE CASCADE,

View File

@@ -1,5 +1,8 @@
namespace Shogi.Api.Repositories.Dto
{
/// <summary>
/// Useful with Dapper to read from database.
/// </summary>
public class SessionDto
{
public string Name { get; set; }

View File

@@ -1,4 +1,5 @@
using Dapper;
using Shogi.Api.Repositories.Dto;
using Shogi.Contracts.Types;
using System.Data.SqlClient;
@@ -24,7 +25,7 @@ public class QueryRepository : IQueryRespository
public async Task<SessionMetadata?> ReadSession(string name)
{
using var connection = new SqlConnection(connectionString);
var results = await connection.QueryAsync<SessionMetadata>(
var results = await connection.QueryAsync<SessionDto>(
"session.ReadSession",
commandType: System.Data.CommandType.StoredProcedure);
return results.SingleOrDefault();

View File

@@ -24,33 +24,14 @@ public class SessionRepository : ISessionRepository
"session.CreateSession",
new
{
SessionName = session.Name,
InitialBoardStateDocument = initialBoardState,
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);
Task CreateSession(ShogiBoard session, string player1);
}