All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 40s
94 lines
2.6 KiB
C#
94 lines
2.6 KiB
C#
using GameIdeas.Shared.Constants;
|
|
using GameIdeas.Shared.Dto;
|
|
using GameIdeas.WebAPI.Services.Games;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace GameIdeas.WebAPI.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
|
|
public class GameController(
|
|
IGameReadService gameReadService,
|
|
IGameWriteService gameWriteService,
|
|
ILoggerFactory loggerFactory) : Controller
|
|
{
|
|
private readonly ILogger<GameController> logger = loggerFactory.CreateLogger<GameController>();
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<GameDto>>> SearchGames([FromQuery] GameFilterDto filter)
|
|
{
|
|
try
|
|
{
|
|
return Ok(await gameReadService.GetGames(filter));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Internal error while search games");
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<ActionResult<GameDetailDto>> GetGameById(int id)
|
|
{
|
|
try
|
|
{
|
|
return Ok(await gameReadService.GetGameById(id));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Internal error while get game with id {id}", id);
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
}
|
|
|
|
[Authorize(Roles = GlobalConstants.ADMIN_MEMBER)]
|
|
[HttpPost("Create")]
|
|
public async Task<ActionResult<int>> CreateGame([FromBody] GameDetailDto game)
|
|
{
|
|
try
|
|
{
|
|
var gameResult = await gameWriteService.CreateGame(game);
|
|
return Created("/Create", gameResult.Id);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Internal error while create game");
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
}
|
|
|
|
[Authorize(Roles = GlobalConstants.ADMIN_MEMBER)]
|
|
[HttpPut("Update")]
|
|
public async Task<ActionResult<int>> UpdateGame([FromBody] GameDetailDto game)
|
|
{
|
|
try
|
|
{
|
|
var gameResult = await gameWriteService.UpdateGame(game);
|
|
return Created($"/Update", gameResult.Id);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Internal error while update game");
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
}
|
|
|
|
[Authorize(Roles = GlobalConstants.ADMIN_MEMBER)]
|
|
[HttpDelete("Delete/{id:int}")]
|
|
public async Task<ActionResult<bool>> DeleteGame(int id)
|
|
{
|
|
try
|
|
{
|
|
return Ok(await gameWriteService.DeleteGame(id));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Internal error while delete game");
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
}
|
|
}
|