using Api_lol.Factories; using DTO; using DTO.DtoControlleur.Champions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Model; using StubLib; using System; using System.Xml.Linq; namespace Api_lol.Controllers { [ApiController] [Route("[controller]")] public class Champions : Controller { private readonly IDataManager data; private readonly ILogger logger; public Champions(ILogger logger,IDataManager manager) { this.logger = logger; data = manager; } [HttpGet] public async Task Get(int index = 0,int count = 0,string classe = "All") { IEnumerable champs = new List(); try { if (count == 0) { count = await data.ChampionsMgr.GetNbItems(); } if (classe != "All") { champs = (await data.ChampionsMgr.GetItems(index, count)).Where(e => e.Class.ToString() == classe).Select(Model => Model.ModelToDto()); } else { champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ModelToDto()); } } catch(Exception ex) { return BadRequest(ex.Message); } GetChampions dto = new GetChampions(champs.ToList(), index, count,await data.ChampionsMgr.GetNbItems()); return Ok(dto); } [HttpPost] public async Task Post(DtoChampions champDTO) { Champion tmp = champDTO.DtoToModel(); Champion champion; try { champion = await data.ChampionsMgr.AddItem(tmp); } catch (Exception ex) { return BadRequest(ex.Message); } DtoChampions dtoChamp = champion.ModelToDto(); return CreatedAtAction(nameof(GetChampion),new { name = dtoChamp.name},dtoChamp); } [HttpGet] [Route("{name}")] public async Task GetChampion(string name) { Champion champion; try { champion = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name); } catch (Exception ex) { return BadRequest(ex.Message); } if ( champion == null) { return BadRequest(); } DtoChampions result = champion.ModelToDto(); return Ok(result); } [HttpDelete] public async Task DeleteChampion(string name = "") { Champion champion; try { champion = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(e => e.Name == name); } catch (Exception ex) { return BadRequest(ex.Message); } await data.ChampionsMgr.DeleteItem(champion); return Ok(champion.ModelToDto()); } } }