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,32 +1,32 @@
using Shogi.Api.Managers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shogi.Api.Extensions;
using Shogi.Api.Managers;
using Shogi.Api.Repositories;
using Shogi.Contracts.Api;
using Shogi.Contracts.Socket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Data.SqlClient;
using Shogi.Contracts.Types;
using Shogi.Api.Extensions;
using System.Data.SqlClient;
namespace Shogi.Api.Controllers;
[ApiController]
[Route("[controller]")]
[Authorize]
public class SessionController : ControllerBase
public class SessionsController : ControllerBase
{
private readonly ISocketConnectionManager communicationManager;
private readonly IModelMapper mapper;
private readonly ISessionRepository sessionRepository;
private readonly IQueryRespository queryRespository;
private readonly ILogger<SessionController> logger;
private readonly ILogger<SessionsController> logger;
public SessionController(
public SessionsController(
ISocketConnectionManager communicationManager,
IModelMapper mapper,
ISessionRepository sessionRepository,
IQueryRespository queryRespository,
ILogger<SessionController> logger)
ILogger<SessionsController> logger)
{
this.communicationManager = communicationManager;
this.mapper = mapper;
@@ -39,14 +39,10 @@ public class SessionController : ControllerBase
public async Task<IActionResult> CreateSession([FromBody] CreateSessionCommand request)
{
var userId = User.GetShogiUserId();
if (string.IsNullOrWhiteSpace(userId)) return this.Unauthorized();
var session = new Domain.Aggregates.Session(
request.Name,
userId,
new Domain.ValueObjects.ShogiBoard(Domain.BoardState.StandardStarting));
var session = new Domain.Aggregates.Session(request.Name, userId);
try
{
await sessionRepository.CreateShogiBoard(board, request.Name, userId);
await sessionRepository.CreateSession(session);
}
catch (SqlException e)
{
@@ -58,6 +54,23 @@ public class SessionController : ControllerBase
return CreatedAtAction(nameof(CreateSession), new { sessionName = request.Name }, null);
}
[HttpDelete("{name}")]
public async Task<IActionResult> DeleteSession(string name)
{
var userId = User.GetShogiUserId();
var session = await sessionRepository.ReadSession(name);
if (session == null) return this.NoContent();
if (session.Player1 == userId)
{
await sessionRepository.DeleteSession(name);
return this.NoContent();
}
return this.Unauthorized("Cannot delete sessions created by others.");
}
//[HttpPost("{sessionName}/Move")]
//public async Task<IActionResult> MovePiece([FromRoute] string sessionName, [FromBody] MovePieceCommand request)
//{
@@ -156,12 +169,12 @@ public class SessionController : ControllerBase
// return Ok(response);
//}
[HttpGet]
public async Task<ActionResult<ReadAllSessionsResponse>> GetSessions()
[HttpGet("PlayerCount")]
public async Task<ActionResult<ReadSessionsPlayerCountResponse>> GetSessionsPlayerCount()
{
var sessions = await this.queryRespository.ReadAllSessionsMetadata();
var sessions = await this.queryRespository.ReadSessionPlayerCount();
return Ok(new ReadAllSessionsResponse
return Ok(new ReadSessionsPlayerCountResponse
{
PlayerHasJoinedSessions = Array.Empty<SessionMetadata>(),
AllOtherSessions = sessions.ToList()
@@ -171,8 +184,26 @@ public class SessionController : ControllerBase
[HttpGet("{name}")]
public async Task<ActionResult<ReadSessionResponse>> GetSession(string name)
{
await Task.CompletedTask;
return new ReadSessionResponse();
var session = await sessionRepository.ReadSession(name);
if (session == null) return this.NotFound();
return new ReadSessionResponse
{
Session = new Session
{
BoardState = new BoardState
{
Board = session.Board.BoardState.State.ToContract(),
Player1Hand = session.Board.BoardState.Player1Hand.ToContract(),
Player2Hand = session.Board.BoardState.Player2Hand.ToContract(),
PlayerInCheck = session.Board.BoardState.InCheck?.ToContract(),
WhoseTurn = session.Board.BoardState.WhoseTurn.ToContract()
},
Player1 = session.Player1,
Player2 = session.Player2,
SessionName = session.Name
}
};
}
//[HttpPut("{sessionName}")]

View File

@@ -1,14 +1,11 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shogi.Contracts.Api;
using Shogi.Api.Extensions;
using Shogi.Api.Managers;
using Shogi.Api.Models;
using Shogi.Api.Repositories;
using System.Security.Claims;
using Shogi.Contracts.Api;
namespace Shogi.Api.Controllers;