|
|
|
@ -1,62 +1,146 @@
|
|
|
|
|
using APILOL.Mapper;
|
|
|
|
|
using APILOL.Controllers.Request;
|
|
|
|
|
using APILOL.Mapper;
|
|
|
|
|
using DTO;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Model;
|
|
|
|
|
using StubLib;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
|
|
|
|
|
|
namespace APILOL.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
|
|
|
[ApiVersion("1.0")]
|
|
|
|
|
|
|
|
|
|
public class ChampionsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
IChampionsManager dataManager;
|
|
|
|
|
|
|
|
|
|
public ChampionsController(IDataManager dataManager)
|
|
|
|
|
private readonly ILogger<ChampionsController> _logger;
|
|
|
|
|
|
|
|
|
|
public ChampionsController(IDataManager dataManager, ILogger<ChampionsController> logger)
|
|
|
|
|
{
|
|
|
|
|
this.dataManager = dataManager.ChampionsMgr;
|
|
|
|
|
this._logger = logger;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GET: api/<ChampionController>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Get(int index, int count)
|
|
|
|
|
public async Task<IActionResult> Get([FromQuery] PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
var champions = await dataManager.GetItems(index, count);
|
|
|
|
|
_logger.LogInformation("API call - [GET] - CHAMPION");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var total = await dataManager.GetNbItems();
|
|
|
|
|
var champions = await dataManager.GetItems(request.offset, request.limit, request.orderingPropertyName, request.isDesc);
|
|
|
|
|
IEnumerable<ChampionDTO> items = champions.Select(c => c.ToDto());
|
|
|
|
|
if (items.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("No champion found.");
|
|
|
|
|
return NotFound("No champion found.");
|
|
|
|
|
}
|
|
|
|
|
return Ok(items);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception error)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(error.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET api/<ChampionController>/5
|
|
|
|
|
[HttpGet("{name}")]
|
|
|
|
|
public async Task<IActionResult> Get(string name)
|
|
|
|
|
public async Task<IActionResult> Get([FromQuery] PageRequest request,string name)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("API call - [GET / NAME] - CHAMPION");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (dataManager.GetNbItemsByName(name) != null)
|
|
|
|
|
{
|
|
|
|
|
return Ok(dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems()));
|
|
|
|
|
var champions = await dataManager.GetItemsByName(name, request.offset, request.limit, request.orderingPropertyName, request.isDesc);
|
|
|
|
|
IEnumerable <ChampionDTO> items = champions.Select(c => c.ToDto());
|
|
|
|
|
if (items.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("No champion found.");
|
|
|
|
|
return NotFound("No champion found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(items);
|
|
|
|
|
}
|
|
|
|
|
return NotFound("No champion matching with this name.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(error.Message);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST api/<ChampionController>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> Post([FromBody] ChampionDTO championDTO)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("API call - [POST] - CHAMPION");
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction(nameof(Get),(await dataManager.AddItem(championDTO.ToModel())).ToDto);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(await dataManager.GetNbItemsByName(championDTO.Name) == 0)
|
|
|
|
|
{
|
|
|
|
|
await dataManager.AddItem(championDTO.ToModel());
|
|
|
|
|
return CreatedAtAction(nameof(Get), championDTO);
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("A champion already exist with this Name. ( Unique Name )");
|
|
|
|
|
return BadRequest("A champion already exist with this Name. ( Unique Name )");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Error in the request");
|
|
|
|
|
return BadRequest(error.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT api/<ChampionController>/5
|
|
|
|
|
[HttpPut("{name}")]
|
|
|
|
|
public async Task<IActionResult> PutAsync(string name, [FromBody] ChampionDTO championDTO)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("API call - [PUT / NAME] - CHAMPION");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var champion = await dataManager
|
|
|
|
|
.GetItemsByName(name, 0, await dataManager.GetNbItems());
|
|
|
|
|
Console.WriteLine(champion.First().Name) ;
|
|
|
|
|
var champion2 = await dataManager
|
|
|
|
|
.GetItemsByName(championDTO.Name, 0, await dataManager.GetNbItems());
|
|
|
|
|
if (champion != null)
|
|
|
|
|
{
|
|
|
|
|
if(champion2.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
await dataManager.UpdateItem(champion.First(), championDTO.ToModel());
|
|
|
|
|
return Ok();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("champion already exist with this unique name.");
|
|
|
|
|
return BadRequest("champion already exist with this unique name.");
|
|
|
|
|
|
|
|
|
|
var dtos = (await dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems()));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("champion not found.");
|
|
|
|
|
return NotFound("champion not found.");
|
|
|
|
|
}
|
|
|
|
|
await dataManager.UpdateItem(champion.First(), championDTO.ToModel());
|
|
|
|
|
return Ok();
|
|
|
|
|
|
|
|
|
|
return Ok(dataManager.UpdateItem(dtos.First(), championDTO.ToModel()));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -64,13 +148,28 @@ namespace APILOL.Controllers
|
|
|
|
|
[HttpDelete("{name}")]
|
|
|
|
|
public async Task<IActionResult> Delete(string name)
|
|
|
|
|
{
|
|
|
|
|
var dtos = (await dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems()));
|
|
|
|
|
return Ok(dataManager.DeleteItem(dtos.First()));
|
|
|
|
|
_logger.LogInformation("API call - [DELETE / NAME] - CHAMPION");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var champion = await (dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems()));
|
|
|
|
|
|
|
|
|
|
if (champion.Count() != 0)
|
|
|
|
|
{
|
|
|
|
|
var championDto = champion.First().ToDto();
|
|
|
|
|
await dataManager.DeleteItem(champion.First());
|
|
|
|
|
return Ok(championDto);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("No matching Champion with this name");
|
|
|
|
|
return NotFound("No matching Champion with this name");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
var champion = new Champion("");
|
|
|
|
|
var dto = ChampionMapper.ToDto(champion);
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
catch(Exception error)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|