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.
161 lines
4.3 KiB
161 lines
4.3 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;
|
|
private readonly ILogger _logger;
|
|
|
|
public ControllerChampions(IDataManager manager, ILogger<ControllerChampions> log)
|
|
{
|
|
_data = manager;
|
|
_logger = log;
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(ChampionPageDTO), 200)]
|
|
public async Task<IActionResult> Get([FromQuery] int index = 0, int count = 10, string? name = "")
|
|
{
|
|
|
|
_logger.LogInformation($"methode Get de ControllerChampions appelée index:{index}, count: {count} et name:{name}");
|
|
int nbChampions = await _data.ChampionsMgr.GetNbItems();
|
|
_logger.LogInformation($"Nombre de champions : {nbChampions}");
|
|
|
|
var champions = await _data.ChampionsMgr.GetItems(index, nbChampions);
|
|
var filteredChampions = champions.Where(Model => Model.Name.Contains(name)).Skip(index * count).Take(count);
|
|
var championDTO = filteredChampions.Select(Model => Model.ToDTO());
|
|
|
|
var page = new ChampionPageDTO
|
|
{
|
|
Data = championDTO,
|
|
Index = index,
|
|
Count = count,
|
|
TotalCount = nbChampions
|
|
};
|
|
return Ok(page);
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
[Route("{name}")]
|
|
[ProducesResponseType(typeof(ChampionDTO), 200)]
|
|
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 (Exception ex)
|
|
{
|
|
_logger.LogError($"erreur methode Get de ControllerChampions: {ex}");
|
|
return BadRequest("erreur de nom de champion");
|
|
}
|
|
}
|
|
|
|
|
|
[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 (Exception ex)
|
|
{
|
|
_logger.LogError($"erreur methode Post de ControllerChampions: {ex}");
|
|
return BadRequest("le champion existe deja");
|
|
}
|
|
}
|
|
|
|
[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 (Exception ex)
|
|
{
|
|
_logger.LogError($"erreur methode Put de ControllerChampions: {ex}");
|
|
return BadRequest("erreur de nom de champion");
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{name}")]
|
|
public async Task<IActionResult> Delete(string name)
|
|
{
|
|
_logger.LogInformation($"methode Delete de ControllerChampions appelée avec le paramètre name: {name}");
|
|
|
|
try
|
|
{
|
|
var champ = (await _data.ChampionsMgr.GetItemsByName(name, 0, 1)).First();
|
|
await _data.ChampionsMgr.DeleteItem(champ);
|
|
return Ok(champ.ToDTO());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"erreur methode Delete de ControllerChampions: {ex}");
|
|
return BadRequest("erreur de nom de champion");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
[ApiController]
|
|
[Route("api/v2/[controller]")]
|
|
[ApiVersion("2.0")]
|
|
|
|
public class ControllerChampionsSecondVersion : Controller
|
|
{
|
|
private readonly ILogger _logger;
|
|
|
|
public ControllerChampionsSecondVersion(ILogger<ControllerChampions> log)
|
|
{
|
|
_logger = log;
|
|
}
|
|
|
|
|
|
[HttpGet()]
|
|
public string Get(int id)
|
|
{
|
|
return "Version 2 of GET";
|
|
}
|
|
|
|
[HttpPost]
|
|
public void Post([FromBody] string value)
|
|
{
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public void Put(int id, [FromBody] string value)
|
|
{
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public void Delete(int id)
|
|
{
|
|
}
|
|
|
|
}
|
|
} |