121 lines
3.3 KiB
C#
121 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Shogi.BackEnd.Application;
|
|
using Shogi.BackEnd.Extensions;
|
|
using Shogi.BackEnd.Repositories;
|
|
using Shogi.BackEnd.Types;
|
|
|
|
namespace Shogi.BackEnd.Controllers;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("backend/[controller]")]
|
|
public class SessionsController(
|
|
SessionRepository sessionRepository,
|
|
ShogiApplication application) : ControllerBase
|
|
{
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateSession()
|
|
{
|
|
var id = this.User.GetId();
|
|
if (string.IsNullOrEmpty(id))
|
|
{
|
|
return this.Unauthorized();
|
|
}
|
|
return await application.CreateSession(id);
|
|
}
|
|
|
|
[HttpDelete("{sessionId}")]
|
|
public async Task<IActionResult> DeleteSession(string sessionId)
|
|
{
|
|
var id = this.User.GetId();
|
|
if (id == null)
|
|
{
|
|
return this.Unauthorized();
|
|
}
|
|
|
|
var (session, _) = await sessionRepository.ReadSessionAndMoves(sessionId);
|
|
if (!session.HasValue) return this.NoContent();
|
|
|
|
if (session.Value.Player1Id == id)
|
|
{
|
|
await sessionRepository.DeleteSession(sessionId);
|
|
return this.NoContent();
|
|
}
|
|
|
|
return this.StatusCode(StatusCodes.Status403Forbidden, "Cannot delete sessions created by others.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch the session and latest board state. Also subscribe the user to socket events for this session.
|
|
/// </summary>
|
|
/// <param name="sessionId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{sessionId}")]
|
|
[AllowAnonymous]
|
|
public async Task<ActionResult<Session>> GetSession(Guid sessionId)
|
|
{
|
|
var domainSession = await application.ReadSession(sessionId.ToString());
|
|
if (domainSession is null) return this.NotFound();
|
|
|
|
return new Session
|
|
{
|
|
BoardState = new BoardState
|
|
{
|
|
Board = domainSession.Board.BoardState.State.ToContract(),
|
|
Player1Hand = domainSession.Board.BoardState.Player1Hand.ToContract(),
|
|
Player2Hand = domainSession.Board.BoardState.Player2Hand.ToContract(),
|
|
PlayerInCheck = domainSession.Board.BoardState.InCheck?.ToContract(),
|
|
WhoseTurn = domainSession.Board.BoardState.WhoseTurn.ToContract(),
|
|
Victor = domainSession.Board.BoardState.IsCheckmate
|
|
? domainSession.Board.BoardState.InCheck == Domains.ValueObjects.WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1
|
|
: null
|
|
},
|
|
Player1 = application.GetUsername(domainSession.Player1),
|
|
Player2 = application.GetUsername(domainSession.Player2),
|
|
SessionId = domainSession.Id
|
|
};
|
|
}
|
|
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
public async Task<ActionResult<SessionMetadata[]>> ReadAllSessionsMetadata()
|
|
{
|
|
var id = this.User.GetId() ?? string.Empty;
|
|
var dtos = await application.ReadAllSessionMetadatas(id);
|
|
return dtos
|
|
.Select(dto => new SessionMetadata
|
|
{
|
|
Player1 = application.GetUsername(dto.Player1Id),
|
|
Player2 = application.GetUsername(dto.Player2Id),
|
|
SessionId = Guid.Parse(dto.Id),
|
|
})
|
|
.ToArray();
|
|
}
|
|
|
|
[HttpPatch("{sessionId}/Join")]
|
|
public async Task<IActionResult> JoinSession(string sessionId)
|
|
{
|
|
var id = this.User.GetId();
|
|
if (id == null)
|
|
{
|
|
return this.Unauthorized();
|
|
}
|
|
|
|
return await application.JoinSession(sessionId, id);
|
|
}
|
|
|
|
[HttpPatch("{sessionId}/Move")]
|
|
public async Task<IActionResult> Move([FromRoute] string sessionId, [FromBody] MovePieceCommand command)
|
|
{
|
|
var id = this.User.GetId();
|
|
if (id == null)
|
|
{
|
|
return this.Unauthorized();
|
|
}
|
|
|
|
return await application.MovePiece(id, sessionId, command);
|
|
}
|
|
}
|