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.
169 lines
5.7 KiB
169 lines
5.7 KiB
using API.Dto;
|
|
using API.Mapping;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
using StubLib;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class ChampionController : ControllerBase
|
|
{
|
|
private readonly ILogger<ChampionController> _logger;
|
|
private readonly StubData data = new StubData();
|
|
|
|
private readonly IDataManager dataManager; // Pour plus tard pour le momment c'est avec le stub
|
|
|
|
private const string Apichampion = "api/champion";
|
|
private readonly HttpClient _client;
|
|
|
|
public ChampionController(ILogger<ChampionController> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
/* public championHttpManager(HttpClient client)
|
|
{
|
|
_client = client;
|
|
client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet");
|
|
}
|
|
|
|
public async Task<IEnumerable<ChampionDto>> getJson()
|
|
{
|
|
var champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>();
|
|
var reponse = await _client.GetAsync("api/champion");
|
|
return champions;
|
|
}*/
|
|
|
|
|
|
/**** Méthodes GET ****/
|
|
[HttpGet]
|
|
public async Task<ActionResult<ChampionDto>> GetChamps()
|
|
{
|
|
// Récupération de la liste des champions
|
|
IEnumerable<Champion?> Champs = await data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result);
|
|
|
|
// Création de la liste de champion Dto
|
|
List<ChampionDto> DtoChamps = new List<ChampionDto>();
|
|
|
|
// Chargement de la liste des champions Dto à partir des champions
|
|
Champs.ToList().ForEach(c => DtoChamps.Add(c.ToDto()));
|
|
|
|
return Ok(DtoChamps);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("{id}")]
|
|
public async Task<ActionResult<ChampionDto>> GetChampById(int id)
|
|
{
|
|
BadRequest("404");
|
|
|
|
IEnumerable<Champion?> Champs = await data.ChampionsMgr.GetItems(id, 1);
|
|
|
|
if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result)
|
|
{
|
|
return Ok(Champs.First().ToDto());
|
|
}
|
|
return BadRequest("404");
|
|
}
|
|
|
|
[HttpGet("{id}/Skins")]
|
|
public async Task<ActionResult<SkinDto>> GetSkinsChamp(int id)
|
|
{
|
|
// Récupération du champion
|
|
IEnumerable<Champion?> ChampNum = await data.ChampionsMgr.GetItems(id, 1);
|
|
|
|
|
|
if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result)
|
|
{
|
|
// Converstion en Champion au lieu de champion IEnumerable
|
|
Champion champion = ChampNum.First();
|
|
|
|
// Récupération des skin du champion
|
|
IEnumerable<Skin?> ListeSkin = await data.SkinsMgr.GetItemsByChampion(champion, 0, data.SkinsMgr.GetNbItemsByChampion(champion).Result);
|
|
|
|
// Création de la liste de skin
|
|
List<SkinDto> skins = new List<SkinDto>();
|
|
|
|
// Ajout des skins dans la nouvelle liste
|
|
ListeSkin.ToList().ForEach(Skin => skins.Add(Skin.ToDto()));
|
|
|
|
return Ok(skins);
|
|
}
|
|
return BadRequest();
|
|
}
|
|
|
|
/**** Méthodes POST ****/
|
|
[HttpPost("Ajouter/{nom}")]
|
|
public async Task<ActionResult> PostChampName(string nom)
|
|
{
|
|
Champion champion = new Champion(nom);
|
|
|
|
await data.ChampionsMgr.AddItem(champion);
|
|
|
|
return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetNbItems().Result - 1 }, champion.ToDto());
|
|
}
|
|
|
|
[HttpPost("Ajouter")]
|
|
public async Task<IActionResult> PostChamp([FromBody] ChampionDto championDto)
|
|
{
|
|
Champion champion = championDto.ToModel();
|
|
await data.ChampionsMgr.AddItem(champion);
|
|
return CreatedAtAction(nameof(data.ChampionsMgr.GetItemsByName), new { Name = championDto.Name }, championDto);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> post([FromBody] ChampionDto championDto)
|
|
{
|
|
return CreatedAtAction(nameof(GetChampById), new { id = 1 },
|
|
await data.ChampionsMgr.AddItem(championDto.ToModel()));
|
|
}
|
|
|
|
/*public async void addchampion(ChampionDto champion)
|
|
{
|
|
_clientLpostAsJsonAscync<Champion>(ApiChampion, champion);
|
|
}
|
|
*/
|
|
|
|
/**** Méthodes DELETE ****/
|
|
|
|
[HttpDelete("Supprimer/{id}")]
|
|
public async Task<IActionResult> DeleteChamp(int id)
|
|
{
|
|
IEnumerable<Champion?> lcha = await data.ChampionsMgr.GetItems(id, 1);
|
|
|
|
if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result)
|
|
{
|
|
Champion ca = lcha.First();
|
|
data.ChampionsMgr.DeleteItem(ca);
|
|
return Ok();
|
|
}
|
|
return BadRequest();
|
|
}
|
|
|
|
|
|
|
|
/**** Méthodes PUT ****/
|
|
|
|
[HttpPut("Modifier/{nom}")]
|
|
public async Task<ActionResult> PutChampName(string nom)
|
|
{
|
|
Champion champion = new Champion(nom);
|
|
|
|
await data.ChampionsMgr.AddItem(champion);
|
|
|
|
return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetNbItems().Result - 1 }, champion.ToDto());
|
|
}
|
|
|
|
/*[HttpPut("Modifier")]
|
|
public async Task<IActionResult> PutChamp([FromBody] ChampionDto championDto)
|
|
{
|
|
Champion champion = championDto.ToModel();
|
|
await data.ChampionsMgr.UpdateItem(champion);
|
|
return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(champion) }, champion);
|
|
}*/
|
|
|
|
}
|
|
}
|