yep
This commit is contained in:
@@ -19,17 +19,20 @@ public class SessionController : ControllerBase
|
||||
private readonly IModelMapper mapper;
|
||||
private readonly ISessionRepository sessionRepository;
|
||||
private readonly IQueryRespository queryRespository;
|
||||
private readonly ILogger<SessionController> logger;
|
||||
|
||||
public SessionController(
|
||||
ISocketConnectionManager communicationManager,
|
||||
IModelMapper mapper,
|
||||
ISessionRepository sessionRepository,
|
||||
IQueryRespository queryRespository)
|
||||
IQueryRespository queryRespository,
|
||||
ILogger<SessionController> logger)
|
||||
{
|
||||
this.communicationManager = communicationManager;
|
||||
this.mapper = mapper;
|
||||
this.sessionRepository = sessionRepository;
|
||||
this.queryRespository = queryRespository;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -37,13 +40,17 @@ public class SessionController : ControllerBase
|
||||
{
|
||||
var userId = User.GetShogiUserId();
|
||||
if (string.IsNullOrWhiteSpace(userId)) return this.Unauthorized();
|
||||
var session = new Domain.ShogiBoard(request.Name, Domain.BoardState.StandardStarting, userId);
|
||||
var session = new Domain.Aggregates.Session(
|
||||
request.Name,
|
||||
userId,
|
||||
new Domain.ValueObjects.ShogiBoard(Domain.BoardState.StandardStarting));
|
||||
try
|
||||
{
|
||||
await sessionRepository.CreateSession(session);
|
||||
await sessionRepository.CreateShogiBoard(board, request.Name, userId);
|
||||
}
|
||||
catch (SqlException)
|
||||
catch (SqlException e)
|
||||
{
|
||||
logger.LogError(exception: e, message: "Uh oh");
|
||||
return this.Conflict();
|
||||
}
|
||||
|
||||
|
||||
22
Shogi.Sockets/Repositories/Dto/BoardStateDto.cs
Normal file
22
Shogi.Sockets/Repositories/Dto/BoardStateDto.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
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.
|
||||
@@ -1,14 +1,14 @@
|
||||
namespace Shogi.Api.Repositories.Dto
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Useful with Dapper to read from database.
|
||||
/// </summary>
|
||||
public class SessionDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Player1 { get; set; }
|
||||
public string Player2 { get; set; }
|
||||
public bool GameOver { get; set; }
|
||||
public string BoardState { get; set; }
|
||||
}
|
||||
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.
|
||||
@@ -21,19 +21,9 @@ public class QueryRepository : IQueryRespository
|
||||
"session.ReadAllSessionsMetadata",
|
||||
commandType: System.Data.CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task<SessionMetadata?> ReadSession(string name)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryAsync<SessionDto>(
|
||||
"session.ReadSession",
|
||||
commandType: System.Data.CommandType.StoredProcedure);
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IQueryRespository
|
||||
{
|
||||
Task<IEnumerable<SessionMetadata>> ReadAllSessionsMetadata();
|
||||
Task<SessionMetadata?> ReadSession(string name);
|
||||
}
|
||||
@@ -1,6 +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;
|
||||
@@ -16,22 +18,53 @@ public class SessionRepository : ISessionRepository
|
||||
connectionString = configuration.GetConnectionString("ShogiDatabase");
|
||||
}
|
||||
|
||||
public async Task CreateSession(ShogiBoard session, string player1)
|
||||
public async Task CreateSession(Session session)
|
||||
{
|
||||
var initialBoardState = JsonSerializer.Serialize(session.BoardState);
|
||||
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
|
||||
{
|
||||
InitialBoardStateDocument = initialBoardState,
|
||||
Name = sessionName,
|
||||
InitialBoardStateDocument = JsonSerializer.Serialize(boardStateDto),
|
||||
Player1Name = player1,
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task<ShogiBoard?> ReadShogiBoard(string name)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryAsync<SessionDto>(
|
||||
"session.ReadSession",
|
||||
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);
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISessionRepository
|
||||
{
|
||||
Task CreateSession(ShogiBoard session, string player1);
|
||||
Task CreateSession(Session session);
|
||||
Task<ShogiBoard?> ReadShogiBoard(string name);
|
||||
}
|
||||
Reference in New Issue
Block a user