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.

185 lines
5.9 KiB

using API.Dto;
using API.Mapping;
using EFManager;
using Microsoft.AspNetCore.Mvc;
using Model;
using StubLib;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class ChampionController : ControllerBase
{
// private readonly ManagerData data;
private readonly StubData data;
private readonly ILogger<ChampionController> _logger;
public ChampionController(StubData manager, ILogger<ChampionController> logger)
{
data = manager;
_logger = logger;
}
/*
private const string Apichampion = "api/champion";
private readonly HttpClient _client;
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;
}
public async void addchampion(ChampionDto champion)
{
_clientLpostAsJsonAscync<Champion>(ApiChampion, champion);
}
*/
/**** 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(Champ => DtoChamps.Add(Champ.ToDto()));
return Ok(DtoChamps);
}
[HttpGet("count")]
public async Task<IActionResult> GetCount()
{
try
{
// Renvoie le nombre de champion
return Ok(data.ChampionsMgr.GetNbItems());
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("{name}")]
public async Task<ActionResult<ChampionDto>> GetChampByName(string name)
{
try
{
// Récupération de la liste des champions
IEnumerable<Champion?> champion = await data.ChampionsMgr.GetItemsByName(name, 0, data.ChampionsMgr.GetNbItems().Result);
// Enregistrement des log
_logger.LogInformation("Executing {Action} with name : {championName}", nameof(GetChampByName), name);
// Création du champion Dto
ChampionDto resultat = champion.First().ToDto();
// Vérification de sa véraciter
if (resultat == null)
{
_logger.LogWarning("No chamions found with {name}", name); ;
return NotFound();
}
return Ok(resultat);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
/**** Méthodes POST ****/
[HttpPost("Ajouter/{nom}")]
public async Task<ActionResult> PostChampName(string nom)
{
// Création d'un champion avec son nom
Champion champion = new Champion(nom);
// Ajout du champion dans la BD
await data.ChampionsMgr.AddItem(champion);
return CreatedAtAction(nameof(GetChampByName), new { Id = data.ChampionsMgr.GetNbItemsByName(nom) }, champion.ToDto());
}
[HttpPost("Ajouter")]
public async Task<ActionResult> PostChamp([FromBody] ChampionDto championDto)
{
// Convertie le championDto en model (a était ajouté via l'API)
Champion champion = championDto.ToModel();
// Ajout du champion en BD
await data.ChampionsMgr.AddItem(champion);
return CreatedAtAction(nameof(data.ChampionsMgr.GetItemsByName), new { Name = championDto.Name }, championDto);
}
[HttpPost]
public async Task<ActionResult> post([FromBody] ChampionDto championDto)
{
return CreatedAtAction(nameof(GetChampByName), new { id = 1 }, await data.ChampionsMgr.AddItem(championDto.ToModel()));
}
/**** 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);
}
*/
}
}