Add services and controllers for games (#12)
Co-authored-by: Maxime Adler <madler@sqli.com> Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using GameIdeas.Shared.Dto;
|
||||
using GameIdeas.WebAPI.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace GameIdeas.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
|
||||
public class GameController(GameService gameService, ILoggerFactory loggerFactory) : Controller
|
||||
{
|
||||
private readonly ILogger<GameController> logger = loggerFactory.CreateLogger<GameController>();
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<GameDto>>> SearchGames([FromQuery] PaggingDto pagging)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await gameService.GetGames(pagging));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while search games");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<GameDto>> GetGameById(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await gameService.GetGameById(id));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while get game with id {id}", id);
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("Create")]
|
||||
public async Task<ActionResult<GameDto>> CreateGame([FromBody] GameDto game)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Created("/Create", await gameService.CreateGame(game));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while create game");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut("Update")]
|
||||
public async Task<ActionResult<GameDto>> UpdateGame([FromBody] GameDto game)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Created($"/Update", await gameService.UpdateGame(game));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while update game");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("Delete/{id:int}")]
|
||||
public async Task<ActionResult<bool>> DeleteGame(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(await gameService.DeleteGame(id));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Internal error while delete game");
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user