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.
129 lines
4.3 KiB
129 lines
4.3 KiB
using System;
|
|
using StubLib;
|
|
using EFLib;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
using Microsoft.Extensions.Logging;
|
|
using DataManagers;
|
|
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
namespace ApiLol.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
public class ChampionController : ControllerBase
|
|
{
|
|
private readonly ILogger<ChampionController> _logger;
|
|
|
|
private IDataManager dataManager;
|
|
|
|
public ChampionController(ILogger<ChampionController> logger, IDataManager manager)
|
|
{
|
|
_logger = logger;
|
|
dataManager = manager;
|
|
}
|
|
|
|
|
|
[HttpGet("GetChampions")]
|
|
public async Task<ActionResult<IEnumerable<ChampionDTO>>> GetChampions(int nb = 3)
|
|
{
|
|
var champions = await dataManager.ChampionsMgr.GetItems(0, nb);
|
|
_logger.Log(LogLevel.Information, $"Les champions ont bien été envoyées");
|
|
return Ok(champions.ToDtos());
|
|
|
|
}
|
|
|
|
// GET api/champion/name
|
|
[HttpGet("getChampions/{name}")]
|
|
public async Task<ActionResult<ChampionDTO?>> GetChampionsByName(string name, int nb =3, int index = 0)
|
|
{
|
|
IEnumerable<Champion?> champion = await dataManager.ChampionsMgr.GetItemsByName(name, index, nb);
|
|
if (champion != null)
|
|
{
|
|
return Ok(champion.ToDtos());
|
|
}
|
|
return BadRequest();
|
|
}
|
|
|
|
|
|
[HttpPost("PostChampion")]
|
|
public async Task<ActionResult<ChampionDTO>> PostChampion(ChampionDTO champ)
|
|
{
|
|
var c = await dataManager.ChampionsMgr.AddItem(ChampionMapper.ToPoco(champ));
|
|
if (c != null)
|
|
{
|
|
_logger.Log(LogLevel.Information, "Le champion a été ajouté à la base");
|
|
return Ok(c);
|
|
}
|
|
return BadRequest();
|
|
|
|
}
|
|
|
|
[HttpPost("PostChampions")]
|
|
public async Task<ActionResult<IEnumerable<ChampionDTO>>> PostChampions(List<ChampionDTO> champs)
|
|
{
|
|
var list = new List<ChampionDTO>();
|
|
foreach (ChampionDTO champ in champs)
|
|
{
|
|
var c = await dataManager.ChampionsMgr.AddItem(ChampionMapper.ToPoco(champ));
|
|
if ( c != null)
|
|
{
|
|
list.Add(c.ToDto());
|
|
_logger.Log(LogLevel.Information, $"Le champion {champ.Name} a été ajouté à la base");
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Information, $"Le champion {champ.Name} n'a pas été ajouté à la base");
|
|
}
|
|
}
|
|
return Ok(list);
|
|
}
|
|
|
|
[HttpPut("PutChampion")]
|
|
public async Task<ActionResult<ChampionDTO>> Put(ChampionDTO oldChamp, ChampionDTO champ)
|
|
{
|
|
var c = await dataManager.ChampionsMgr.UpdateItem(oldChamp.ToPoco(), champ.ToPoco());
|
|
if (c!=null)
|
|
{
|
|
_logger.Log(LogLevel.Information, $"Le champion {champ.Name} a été modifié ");
|
|
return Ok(c);
|
|
}
|
|
_logger.Log(LogLevel.Information, $"Le champion {champ.Name} n'a pas été modifié");
|
|
return BadRequest();
|
|
}
|
|
|
|
// DELETE api/values
|
|
[HttpDelete("ChampionDto")]
|
|
public async Task<ActionResult> DeleteChampion([FromBody] ChampionDTO champion)
|
|
{
|
|
if (await dataManager.ChampionsMgr.DeleteItem(champion.ToPoco()))
|
|
{
|
|
return Ok();
|
|
}
|
|
return BadRequest();
|
|
}
|
|
|
|
[HttpDelete("DeleteChampions")]
|
|
public async Task<ActionResult<IEnumerable<ChampionDTO>>> DeleteChampions(List<ChampionDTO> champions, int nb = 10)
|
|
{
|
|
foreach (ChampionDTO champ in champions)
|
|
{
|
|
if (await dataManager.ChampionsMgr.DeleteItem(champ.ToPoco()))
|
|
{
|
|
_logger.Log(LogLevel.Information, $"Le champion {champ.Name} a été supprimé");
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Information, $"Le champion {champ.Name} n'a pas été supprimé");
|
|
}
|
|
}
|
|
return Ok();
|
|
}
|
|
|
|
}
|
|
}
|
|
|