You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LOL/Sources/apiLOL/Controllers/ControllerChampions.cs

85 lines
2.5 KiB

using Microsoft.AspNetCore.Mvc;
using Model;
using StubLib;
using System.Xml.Linq;
using static StubLib.StubData;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace apiLOL.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ControllerChampions : Controller
{
private readonly IDataManager data;
private readonly ILogger _logger;
public ControllerChampions(IDataManager manager, ILogger<ControllerChampions> log)
{
data = manager;
_logger = log;
}
// GET: api/<ControllerLol>
[HttpGet]
public async Task<IActionResult> Get([FromQuery] int index = 0, int count = 10, string name = "")
{
//FromQuery permet de filtrer dans la collection de champions en fonction du nom
// Possible de faire une classe PageRequest pour gérer les paramètres index et count
_logger.LogInformation($"methode Get de ControllerChampions appelée");
_logger.LogInformation($"Nombre de champions : {await data.ChampionsMgr.GetNbItems()}");
var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ToDTO());
var page = new ChampionPageDTO
{
Data = champs,
Index = index,
Count = count,
TotalCount = 100
};
return Ok(page);
}
// GET api/<ControllerLol>/Charle
[HttpGet]
[Route("{name}")]
public async Task<IActionResult> GetChampion(string name)
{
_logger.LogInformation($"methode GetChampion de ControllerChampions appelée avec le paramètre {name}");
var champs = (await data.ChampionsMgr.GetItemsByName(name,0,1)).First();
return Ok(champs.ToDTO());
}
// POST api/<ControllerLol>
[HttpPost]
public async Task<IActionResult> Post(ChampionDTO champDTO)
{
_logger.LogInformation($"methode Post de ControllerChampions appelée avec le paramètre {champDTO.Name}");
Champion tmp = champDTO.ToModel();
Champion champion = await data.ChampionsMgr.AddItem(tmp);
ChampionDTO dtoChamp = champion.ToDTO();
return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp);
}
// PUT api/<ControllerLol>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ControllerLol>/5
[HttpDelete("{name}")]
public async void Delete(String name)
{
var champ = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First();
data.ChampionsMgr.DeleteItem(champ);
}
}
}