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.v2 { [ApiController] [Route("api/v{version:apiVersion}/[controller]")] [ApiVersion("2")] public class ChampionsController : ControllerBase { IChampionsManager dataManager; private readonly ILogger _logger; public ChampionsController(IDataManager dataManager, ILogger logger) { this.dataManager = dataManager.ChampionsMgr; this._logger = logger; } // GET: api/ [MapToApiVersion("2")] [HttpGet] public async Task Get([FromQuery] PageRequest request) { _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 items = champions.Select(c => c.ToDto()); if (items.Count() == 0) { _logger.LogInformation("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) { return BadRequest(error.Message); } } // GET api//5 [MapToApiVersion("2")] [HttpGet("{name}")] public async Task Get([FromQuery] PageRequest request,string name) { _logger.LogInformation("API call - [GET / NAME] - CHAMPION"); try { if (dataManager.GetNbItemsByName(name) != null) { var champions = await dataManager.GetItemsByName(name, request.Offset, request.Limit, request.OrderingPropertyName, request.IsDesc); IEnumerable 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); } } // POST api/ [MapToApiVersion("2")] [HttpPost] public async Task Post([FromBody] ChampionDTO championDTO) { _logger.LogInformation("API call - [POST] - CHAMPION"); 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//5 [MapToApiVersion("2")] [HttpPut("{name}")] public async Task 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."); } else { _logger.LogInformation("champion not found."); return NotFound("champion not found."); } await dataManager.UpdateItem(champion.First(), championDTO.ToModel()); return Ok(); } catch (Exception e) { return BadRequest(e.Message); } } // DELETE api//5 [MapToApiVersion("2")] [HttpDelete("{name}")] public async Task Delete(string name) { _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"); } } catch(Exception error) { return BadRequest(error); } } } }