|
|
@ -1,18 +1,16 @@
|
|
|
|
using API.Dto;
|
|
|
|
using API.Dto;
|
|
|
|
using API.Mapping;
|
|
|
|
using API.Mapping;
|
|
|
|
using EFManager;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Model;
|
|
|
|
using Model;
|
|
|
|
using StubLib;
|
|
|
|
using StubLib;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace API.Controllers.version2
|
|
|
|
namespace API.Controllers
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[ApiController]
|
|
|
|
[Route("[controller]")]
|
|
|
|
[ApiVersion("2.0")]
|
|
|
|
|
|
|
|
[Route("api/v{version:ApiVersion}/[controller]")]
|
|
|
|
public class ChampionController : ControllerBase
|
|
|
|
public class ChampionController : ControllerBase
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// private readonly ManagerData data;
|
|
|
|
// private readonly ManagerData data;
|
|
|
|
private readonly StubData data;
|
|
|
|
private readonly StubData data;
|
|
|
|
private readonly ILogger<ChampionController> _logger;
|
|
|
|
private readonly ILogger<ChampionController> _logger;
|
|
|
@ -20,35 +18,9 @@ namespace API.Controllers
|
|
|
|
|
|
|
|
|
|
|
|
public ChampionController(StubData manager, ILogger<ChampionController> logger)
|
|
|
|
public ChampionController(StubData manager, ILogger<ChampionController> logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
data = manager;
|
|
|
|
data = manager;
|
|
|
|
_logger = logger;
|
|
|
|
_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 ****/
|
|
|
|
/**** Méthodes GET ****/
|
|
|
|
[HttpGet]
|
|
|
|
[HttpGet]
|
|
|
@ -56,6 +28,11 @@ namespace API.Controllers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Récupération de la liste des champions
|
|
|
|
// Récupération de la liste des champions
|
|
|
|
IEnumerable<Champion?> Champs = await data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result);
|
|
|
|
IEnumerable<Champion?> Champs = await data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result);
|
|
|
|
|
|
|
|
if (Champs == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogWarning("No chamions found");
|
|
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Création de la liste de champion Dto
|
|
|
|
// Création de la liste de champion Dto
|
|
|
|
List<ChampionDto> DtoChamps = new List<ChampionDto>();
|
|
|
|
List<ChampionDto> DtoChamps = new List<ChampionDto>();
|
|
|
@ -80,7 +57,6 @@ namespace API.Controllers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{name}")]
|
|
|
|
[HttpGet("{name}")]
|
|
|
|
public async Task<ActionResult<ChampionDto>> GetChampByName(string name)
|
|
|
|
public async Task<ActionResult<ChampionDto>> GetChampByName(string name)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -98,7 +74,7 @@ namespace API.Controllers
|
|
|
|
// Vérification de sa véraciter
|
|
|
|
// Vérification de sa véraciter
|
|
|
|
if (resultat == null)
|
|
|
|
if (resultat == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.LogWarning("No chamions found with {name}", name); ;
|
|
|
|
_logger.LogWarning("No chamions found with {name}", name);
|
|
|
|
return NotFound();
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(resultat);
|
|
|
|
return Ok(resultat);
|
|
|
@ -111,7 +87,6 @@ namespace API.Controllers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**** Méthodes POST ****/
|
|
|
|
/**** Méthodes POST ****/
|
|
|
|
[HttpPost("Ajouter/{nom}")]
|
|
|
|
[HttpPost("Ajouter/{nom}")]
|
|
|
|
public async Task<ActionResult> PostChampName(string nom)
|
|
|
|
public async Task<ActionResult> PostChampName(string nom)
|
|
|
@ -125,60 +100,77 @@ namespace API.Controllers
|
|
|
|
return CreatedAtAction(nameof(GetChampByName), new { Id = data.ChampionsMgr.GetNbItemsByName(nom) }, champion.ToDto());
|
|
|
|
return CreatedAtAction(nameof(GetChampByName), new { Id = data.ChampionsMgr.GetNbItemsByName(nom) }, champion.ToDto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("Ajouter")]
|
|
|
|
[HttpPost("Ajouter")]
|
|
|
|
public async Task<ActionResult> PostChamp([FromBody] ChampionDto championDto)
|
|
|
|
public async Task<ActionResult> PostChamp([FromBody] ChampionDto championDto)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Convertie le championDto en model (a était ajouté via l'API)
|
|
|
|
try
|
|
|
|
Champion champion = championDto.ToModel();
|
|
|
|
{
|
|
|
|
|
|
|
|
// Convertie le championDto en model (a était ajouté via l'API)
|
|
|
|
|
|
|
|
Champion champion = championDto.ToModel();
|
|
|
|
|
|
|
|
|
|
|
|
// Ajout du champion en BD
|
|
|
|
// Ajout du champion en BD
|
|
|
|
await data.ChampionsMgr.AddItem(champion);
|
|
|
|
await data.ChampionsMgr.AddItem(champion);
|
|
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction(nameof(data.ChampionsMgr.GetItemsByName), new { Name = championDto.Name }, championDto);
|
|
|
|
_logger.LogInformation("Sucessfully saved Champions : " + champion.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
return CreatedAtAction(nameof(data.ChampionsMgr.GetItemsByName), new { championDto.Name }, championDto);
|
|
|
|
public async Task<ActionResult> post([FromBody] ChampionDto championDto)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
catch (Exception e)
|
|
|
|
return CreatedAtAction(nameof(GetChampByName), new { id = 1 }, await data.ChampionsMgr.AddItem(championDto.ToModel()));
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError("Somthing goes wrong caching the Champions controller : " + e.Message);
|
|
|
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**** Méthodes DELETE ****/
|
|
|
|
/**** Méthodes DELETE ****/
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("Supprimer/{id}")]
|
|
|
|
[HttpDelete("Supprimer/{name}")]
|
|
|
|
public async Task<IActionResult> DeleteChamp(int id)
|
|
|
|
public async Task<IActionResult> DeleteChamp(string name)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
IEnumerable<Champion?> lcha = await data.ChampionsMgr.GetItems(id, 1);
|
|
|
|
try
|
|
|
|
|
|
|
|
|
|
|
|
if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Champion ca = lcha.First();
|
|
|
|
_logger.LogInformation("Executing {Action} with name : {championName}", nameof(DeleteChamp), name);
|
|
|
|
data.ChampionsMgr.DeleteItem(ca);
|
|
|
|
var champion = await data.ChampionsMgr.GetItemsByName(name, 0, await data.ChampionsMgr.GetNbItems());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (champion != null) await data.ChampionsMgr.DeleteItem(champion.First());
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError($"No chamions found with {name} cannot delete"); ;
|
|
|
|
|
|
|
|
return NotFound($"No chamions found with {name} cannot delete");
|
|
|
|
|
|
|
|
}
|
|
|
|
return Ok();
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BadRequest();
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError("Somthing goes wrong caching the Champions controller : " + e.Message);
|
|
|
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**** Méthodes PUT ****/
|
|
|
|
|
|
|
|
|
|
|
|
/**** Méthodes PUT ****
|
|
|
|
[HttpPut("Modifier/{name}")]
|
|
|
|
|
|
|
|
public async Task<ActionResult> PutChampName(string name, [FromBody] ChampionDto value)
|
|
|
|
[HttpPut("Modifier/{nom}")]
|
|
|
|
|
|
|
|
public async Task<ActionResult> PutChampName(string nom)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Champion champion = new Champion(nom);
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
await data.ChampionsMgr.AddItem(champion);
|
|
|
|
_logger.LogInformation("Executing {Action} with name : {championName}", nameof(PutChampName), name);
|
|
|
|
|
|
|
|
var champion = await data.ChampionsMgr.GetItemsByName(name, 0, await data.ChampionsMgr.GetNbItems());
|
|
|
|
return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetNbItems().Result - 1 }, champion.ToDto());
|
|
|
|
if (champion == null)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError("No chamions found with {name} in the dataBase", name); ;
|
|
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("Modifier")]
|
|
|
|
await data.ChampionsMgr.UpdateItem(champion.First(), value.ToModel());
|
|
|
|
public async Task<IActionResult> PutChamp([FromBody] ChampionDto championDto)
|
|
|
|
return Ok();
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Champion champion = championDto.ToModel();
|
|
|
|
catch (Exception e)
|
|
|
|
await data.ChampionsMgr.UpdateItem(champion);
|
|
|
|
{
|
|
|
|
return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(champion) }, champion);
|
|
|
|
_logger.LogError("Somthing goes wrong caching the Champions controller : " + e.Message);
|
|
|
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|