78 lines
2.1 KiB
C#
78 lines
2.1 KiB
C#
using ChessDotNet;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NLog;
|
|
using TURN.Services.Chess.DBModel;
|
|
|
|
namespace TURN.Services.Chess.Controllers
|
|
{
|
|
[Authorize(AuthenticationSchemes = AuthSchemes, Roles = "admin")]
|
|
[Route("/[controller]")]
|
|
[ApiController]
|
|
public class TurnController : ControllerBase
|
|
{
|
|
public const string AuthSchemes = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
private readonly ModelBaseContext _db;
|
|
private readonly Logger _logger;
|
|
|
|
public TurnController(ModelBaseContext db, Logger logger)
|
|
{
|
|
_db = db;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Тестовый гет.
|
|
/// </summary>
|
|
[HttpGet("test/{param}")]
|
|
public ActionResult<string> Get(string param)
|
|
{
|
|
try
|
|
{
|
|
var game = new ChessGame();
|
|
|
|
return Ok(param);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex);
|
|
return StatusCode(500, $"Internal server error: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создать игру.
|
|
/// </summary>
|
|
[HttpGet("startgame/{blackUser}/{whiteUser}")]
|
|
public ActionResult<string> StartGame(string blackUser, string whiteUser)
|
|
{
|
|
try
|
|
{
|
|
var game = new ChessGame();
|
|
|
|
Game obj = new()
|
|
{
|
|
BlackUserName = blackUser,
|
|
WhiteUserName = whiteUser,
|
|
StartDate = DateTimeOffset.Now.ToUnixTimeSeconds().ToString()
|
|
};
|
|
|
|
_ = _db.Add(obj);
|
|
_ = _db.SaveChanges();
|
|
|
|
var mes = $"Game {obj.GameId} started.";
|
|
_logger.Info(mes);
|
|
|
|
return Ok(mes);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(ex);
|
|
return StatusCode(500, $"Internal server error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|