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.
123 lines
3.4 KiB
123 lines
3.4 KiB
using System.Security.Cryptography;
|
|
using apiLOL.DTO;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
namespace apiLOL.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
[ApiVersion("1.0")]
|
|
|
|
public class ControllerChampions : Controller
|
|
{
|
|
private readonly IDataManager data;
|
|
// EFdata manager qui implémente l'interface IDataManager
|
|
// coté client : Refaire un APIdata manager qui implémente l'interface IDataManager
|
|
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)
|
|
{
|
|
//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"); //Context dans les logs
|
|
int nbChampions = await data.ChampionsMgr.GetNbItems();
|
|
_logger.LogInformation($"Nombre de champions : {nbChampions}");
|
|
|
|
var champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO());
|
|
|
|
var page = new ChampionPageDTO
|
|
{
|
|
Data = champs,
|
|
Index = index,
|
|
Count = count,
|
|
TotalCount = nbChampions
|
|
};
|
|
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}");
|
|
try
|
|
{
|
|
var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1));
|
|
return Ok(champs.First().ToDTO());
|
|
}
|
|
catch
|
|
{
|
|
return BadRequest("erreur de nom de champion");
|
|
} //Attraper l'excpetion et la logger
|
|
}
|
|
|
|
|
|
// 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}");
|
|
|
|
|
|
try
|
|
{
|
|
Champion tmp = champDTO.ToModel();
|
|
Champion champion = await data.ChampionsMgr.AddItem(tmp);
|
|
ChampionDTO dtoChamp = champion.ToDTO();
|
|
return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp);
|
|
}
|
|
catch
|
|
{
|
|
return BadRequest("le champion existe deja");
|
|
}
|
|
}
|
|
|
|
// PUT api/<ControllerLol>/5
|
|
[HttpPut("{name}")]
|
|
public async Task<IActionResult> Put(string name, string bio)
|
|
{
|
|
_logger.LogInformation($"methode Put de ControllerChampions appelée avec le paramètre name: {name} et bio: {bio}");
|
|
|
|
try
|
|
{
|
|
var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First();
|
|
champs.Bio = bio;
|
|
return Ok(champs.ToDTO());
|
|
}
|
|
catch
|
|
{
|
|
return BadRequest("erreur de nom de champion");
|
|
}
|
|
}
|
|
|
|
// DELETE api/<ControllerLol>/5
|
|
[HttpDelete("{name}")]
|
|
public async Task<IActionResult> Delete(string name)
|
|
{
|
|
try
|
|
{
|
|
var champ = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First();
|
|
data.ChampionsMgr.DeleteItem(champ); // await
|
|
return Ok(champ.ToDTO());
|
|
}
|
|
catch
|
|
{
|
|
return BadRequest("erreur de nom de champion");
|
|
}
|
|
}
|
|
|
|
}
|
|
} |