Major update
This commit is contained in:
72
games/Controllers/GamesController.cs
Normal file
72
games/Controllers/GamesController.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Games.Commands;
|
||||
using Games.Entities;
|
||||
using Games.Queries;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
||||
|
||||
namespace Games.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/games")]
|
||||
public class GamesController : ControllerBase
|
||||
{
|
||||
private readonly ISender _sender;
|
||||
|
||||
public GamesController(ISender sender) => _sender = sender;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetGames([FromQuery] int pageNumber, [FromQuery] int pageSize)
|
||||
{
|
||||
var games = await _sender.Send(new GetGamesQuery
|
||||
{
|
||||
PageNumber = pageNumber,
|
||||
PageSize = pageSize
|
||||
});
|
||||
|
||||
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(new
|
||||
{
|
||||
games.CurrentPage,
|
||||
games.PageSize,
|
||||
games.TotalCount,
|
||||
games.TotalPages
|
||||
}));
|
||||
|
||||
return Ok(games);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult> GetGamesById(int id)
|
||||
{
|
||||
var game = await _sender.Send(new GetGameByIdQuery
|
||||
{
|
||||
GameId = id
|
||||
});
|
||||
|
||||
return Ok(game);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> AddGame([FromBody] List<Game> games)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _sender.Send(new AddGamesCommand(games));
|
||||
|
||||
return StatusCode(201);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user