|
|
|
@ -12,7 +12,7 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
|
|
|
[ApiVersion("2")]
|
|
|
|
|
[ApiVersion("2.0")]
|
|
|
|
|
|
|
|
|
|
public class ChampionsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
@ -24,18 +24,17 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
{
|
|
|
|
|
this.dataManager = dataManager.ChampionsMgr;
|
|
|
|
|
this._logger = logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GET: api/<ChampionController>
|
|
|
|
|
[MapToApiVersion("2")]
|
|
|
|
|
[MapToApiVersion("2.0")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> Get([FromQuery] PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("API call - [GET] - CHAMPION");
|
|
|
|
|
_logger.LogInformation("API call - [GET] - CHAMPION ");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
@ -44,48 +43,50 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
IEnumerable<ChampionDTO> items = champions.Select(c => c.ToDto());
|
|
|
|
|
if (items.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("No champion found.");
|
|
|
|
|
_logger.LogError("No champion found.");
|
|
|
|
|
return NotFound("No champion found.");
|
|
|
|
|
}
|
|
|
|
|
return Ok(new { data = items, count = await dataManager.GetNbItems(), offset = request.Offset, limit = request.Limit });
|
|
|
|
|
}
|
|
|
|
|
catch(Exception error)
|
|
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Error in the request");
|
|
|
|
|
return BadRequest(error.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET api/<ChampionController>/5
|
|
|
|
|
[MapToApiVersion("2")]
|
|
|
|
|
[MapToApiVersion("2.0")]
|
|
|
|
|
[HttpGet("{name}")]
|
|
|
|
|
public async Task<IActionResult> Get([FromQuery] PageRequest request,string name)
|
|
|
|
|
public async Task<IActionResult> Get([FromQuery] PageRequest request, string name)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("API call - [GET / NAME] - CHAMPION");
|
|
|
|
|
_logger.LogInformation("API call - [GET / NAME] - CHAMPION {name}", name);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (dataManager.GetNbItemsByName(name) != null)
|
|
|
|
|
{
|
|
|
|
|
var champions = await dataManager.GetItemsByName(name, request.Offset, request.Limit, request.OrderingPropertyName, request.IsDesc);
|
|
|
|
|
IEnumerable <ChampionDTO> items = champions.Select(c => c.ToDto());
|
|
|
|
|
IEnumerable<ChampionDTO> items = champions.Select(c => c.ToDto());
|
|
|
|
|
if (items.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("No champion found.");
|
|
|
|
|
_logger.LogError("No champion found.");
|
|
|
|
|
return NotFound("No champion found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(items);
|
|
|
|
|
return Ok(new { data = items, count = dataManager.GetNbItems() });
|
|
|
|
|
}
|
|
|
|
|
return NotFound("No champion matching with this name.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Error in the request");
|
|
|
|
|
return BadRequest(error.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST api/<ChampionController>
|
|
|
|
|
[MapToApiVersion("2")]
|
|
|
|
|
[MapToApiVersion("2.0")]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> Post([FromBody] ChampionDTO championDTO)
|
|
|
|
|
{
|
|
|
|
@ -93,12 +94,12 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(await dataManager.GetNbItemsByName(championDTO.Name) == 0)
|
|
|
|
|
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 )");
|
|
|
|
|
_logger.LogError("A champion already exist with this Name. ( Unique Name )");
|
|
|
|
|
return BadRequest("A champion already exist with this Name. ( Unique Name )");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error)
|
|
|
|
@ -109,7 +110,7 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT api/<ChampionController>/5
|
|
|
|
|
[MapToApiVersion("2")]
|
|
|
|
|
[MapToApiVersion("2.0")]
|
|
|
|
|
[HttpPut("{name}")]
|
|
|
|
|
public async Task<IActionResult> PutAsync(string name, [FromBody] ChampionDTO championDTO)
|
|
|
|
|
{
|
|
|
|
@ -118,24 +119,24 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
{
|
|
|
|
|
var champion = await dataManager
|
|
|
|
|
.GetItemsByName(name, 0, await dataManager.GetNbItems());
|
|
|
|
|
Console.WriteLine(champion.First().Name) ;
|
|
|
|
|
Console.WriteLine(champion.First().Name);
|
|
|
|
|
var champion2 = await dataManager
|
|
|
|
|
.GetItemsByName(championDTO.Name, 0, await dataManager.GetNbItems());
|
|
|
|
|
if (champion != null)
|
|
|
|
|
{
|
|
|
|
|
if(champion2.Count() == 0)
|
|
|
|
|
if (champion2.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
await dataManager.UpdateItem(champion.First(), championDTO.ToModel());
|
|
|
|
|
return Ok();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("champion already exist with this unique name.");
|
|
|
|
|
_logger.LogError("champion already exist with this unique name.");
|
|
|
|
|
return BadRequest("champion already exist with this unique name.");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("champion not found.");
|
|
|
|
|
_logger.LogError("champion not found.");
|
|
|
|
|
return NotFound("champion not found.");
|
|
|
|
|
}
|
|
|
|
|
await dataManager.UpdateItem(champion.First(), championDTO.ToModel());
|
|
|
|
@ -144,13 +145,14 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Error in the request");
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE api/<ChampionController>/5
|
|
|
|
|
[MapToApiVersion("2")]
|
|
|
|
|
[MapToApiVersion("2.0")]
|
|
|
|
|
[HttpDelete("{name}")]
|
|
|
|
|
public async Task<IActionResult> Delete(string name)
|
|
|
|
|
{
|
|
|
|
@ -158,7 +160,7 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var champion = await (dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (champion.Count() != 0)
|
|
|
|
|
{
|
|
|
|
|
var championDto = champion.First().ToDto();
|
|
|
|
@ -167,13 +169,14 @@ namespace APILOL.Controllers.v2
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("No matching Champion with this name");
|
|
|
|
|
_logger.LogError("No matching Champion with this name");
|
|
|
|
|
return NotFound("No matching Champion with this name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch(Exception error)
|
|
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Error in the request");
|
|
|
|
|
return BadRequest(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|