From bc808d92640ca7639919b7f94e2a5f7803f01da2 Mon Sep 17 00:00:00 2001 From: dadalmeida1 Date: Fri, 3 Mar 2023 14:05:29 +0100 Subject: [PATCH] update --- .../Controllers/ChampionsController.cs | 38 ++++++++-- .../Controllers/Request/PageRequest.cs | 29 ++++++++ .../Controllers/Response/PageResponse.cs | 6 ++ .../Controllers/RuneController.cs | 73 +++++++++++++++---- .../Controllers/RunePageController.cs | 70 ++++++++++++++++++ .../Controllers/SkillController.cs | 70 ++++++++++++++++++ .../Controllers/SkinController.cs | 70 ++++++++++++++++++ .../Middleware/ExceptionMiddleware.cs | 6 ++ .../Middleware/HttpExeption.cs | 41 ++++++----- 9 files changed, 365 insertions(+), 38 deletions(-) create mode 100644 EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Request/PageRequest.cs create mode 100644 EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Response/PageResponse.cs create mode 100644 EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RunePageController.cs create mode 100644 EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkillController.cs create mode 100644 EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkinController.cs create mode 100644 EntityFramework_LoL/Sources/API_LoL_Project/Middleware/ExceptionMiddleware.cs diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs index 6cd0643..8618a15 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs @@ -1,8 +1,10 @@ using API_LoL_Project.Mapper; +using API_LoL_Project.Middleware; using DTO; using Microsoft.AspNetCore.Mvc; using Model; using StubLib; +using System.Text.Json; using System.Xml.Linq; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -13,27 +15,41 @@ namespace API_LoL_Project.Controllers [ApiController] public class ChampionsController : ControllerBase { - public IChampionsManager dataManager; + public IChampionsManager dataManager; + private readonly ILogger _logger; /* public ChampionsController(IChampionsManager dataManager) { this.dataManager = dataManager; }*/ - public ChampionsController(IDataManager dataManager) + public ChampionsController(IDataManager dataManager, ILogger logger) { - this.dataManager = dataManager.ChampionsMgr; + this.dataManager = dataManager.ChampionsMgr; + this._logger = logger; } // GET: api/ [HttpGet] - public async Task>> Get() + public async Task>> Get([FromQuery] Request.PageRequest request) { try { - var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); + var totalcount = await dataManager.GetNbItems(); + if (request.count + request.index > totalcount) + { + _logger.LogWarning("No chamions found with Id"); + return BadRequest("No chamions found with Id "); + } + _logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request);; + var champions = await dataManager.GetItems(request.PageNumber, totalcount, request.orderingPropertyName, (request.descending == null ? false : (bool)request.descending)); IEnumerable res = champions.Select(c => c.toDTO()); + if (res.Count() >= 0 || res == null) + { + _logger.LogWarning("No chamions found with Id"); + return BadRequest("No chamions found with Id "); + } return Ok(res); } catch (Exception e) @@ -49,16 +65,22 @@ namespace API_LoL_Project.Controllers // GET api//5 [HttpGet("{name}")] - public async Task> Get(string name) + public async Task> GetChampionsByName(string name) { - - try { + var champion = await dataManager .GetItemsByName(name, 0, await dataManager.GetNbItems()); + _logger.LogInformation("Executing {Action} with name : {championName}", nameof(GetChampionsByName), name); ChampionDTO res = champion.First().toDTO(); + if (res == null) + { + _logger.LogWarning("No chamions found with {name}", name); ; + return NotFound(); + } return Ok(res); + } catch (Exception e) { diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Request/PageRequest.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Request/PageRequest.cs new file mode 100644 index 0000000..1950fa3 --- /dev/null +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Request/PageRequest.cs @@ -0,0 +1,29 @@ +namespace API_LoL_Project.Controllers.Request +{ + public class PageRequest + { + //max leght + public string? orderingPropertyName { get; set; } = null; + public bool? descending { get; set; } = false; + const int maxPageSize = 50; + public int PageNumber { get; set; } = 1; + + public int index { get; set; } = 1; + public int count { get; set; } = 1; + + + //max lentght + private int _pageSize; + public int PageSize + { + get + { + return _pageSize; + } + set + { + _pageSize = (value > maxPageSize) ? maxPageSize : value; + } + } + } +} diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Response/PageResponse.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Response/PageResponse.cs new file mode 100644 index 0000000..f75a599 --- /dev/null +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/Response/PageResponse.cs @@ -0,0 +1,6 @@ +namespace API_LoL_Project.Controllers.Response +{ + public class PageResponse + { + } +} diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs index 62e1908..7b9a2f2 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs @@ -1,6 +1,8 @@ using DTO; using Microsoft.AspNetCore.Mvc; using Model; +using API_LoL_Project.Mapper; + // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -9,48 +11,91 @@ namespace API_LoL_Project.Controllers [Route("api/[controller]")] [ApiController] public class RuneController : ControllerBase - {/* + { public IRunesManager runesManager; // you should create a custom logger to be prety private readonly ILogger _logger; - public RuneController(IDataManager dataManager,ILogger _logger) + public RuneController(IDataManager dataManager, ILogger logger) { this.runesManager = dataManager.RunesMgr; + + this._logger = logger; } - // GET: api/ + + + /*// GET: api/ [HttpGet] public async Task> Get() { - try{ + try + { var runes = await runesManager.GetItems(0, await runesManager.GetNbItems()); IEnumerable res = runes.Select(c => c.toDTO()); return Ok(res); } - catch(Exception e){ - _logger.LogInformation("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); + catch (Exception e) + { + _logger.LogInformation("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); + return BadRequest(e.Message); + + } + }*/ + + // GET: api/ + [HttpGet] + public async Task>> Get([FromQuery] Request.PageRequest request) + { + try + { + var totalcount = await runesManager.GetNbItems(); + if (request.count + request.index > totalcount) + { + _logger.LogWarning("to many rows ask the max is {totalcount}", totalcount); + return BadRequest("to many rows ask the max is " + totalcount) ; + } + _logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request); + + + var runes = await runesManager.GetItems(request.PageNumber, totalcount, request.orderingPropertyName,(request.descending == null ? false : (bool)request.descending)); + IEnumerable res = runes.Select(c => c.toDTO()); + if (res.Count() >= 0 || res == null) + { + _logger.LogWarning("No runes found with Id"); + return BadRequest("No runes found with Id "); + } + return Ok(res); + } + catch (Exception e) + { + _logger.LogError("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); return BadRequest(e.Message); } + + } + // GET api//5 [HttpGet("{id}")] public string Get(int id) { - try{ + try + { var rune = await dataManager .GetItemsByName(name, 0, await dataManager.GetNbItems()); RuneDto result = champion.First().toDTO(); return Ok(result); } - catch(Exeption e){ + catch (Exeption e) + { new HttpException(400, 'Cannot get rune :' + e.message); } - + } @@ -58,14 +103,16 @@ namespace API_LoL_Project.Controllers [HttpPost] public void Post([FromBody] string value) { - try{ + try + { await dataManager.AddItem(value.toModel()); return Ok(); } - catch(){ + catch () + { new HttpException(400, 'Cannot create rune') } - + } @@ -80,6 +127,6 @@ namespace API_LoL_Project.Controllers [HttpDelete("{id}")] public void Delete(int id) { - }*/ + } } } diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RunePageController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RunePageController.cs new file mode 100644 index 0000000..2d0934b --- /dev/null +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RunePageController.cs @@ -0,0 +1,70 @@ +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace API_LoL_Project.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class RunePageController : ControllerBase + { + // GET: api/ + [HttpGet] + public async Task>> Get([FromQuery] Request.PageRequest request) + { + try + { + var totalcount = await runesManager.GetNbItems(); + if (request.count + request.index > totalcount) + { + _logger.LogWarning("to many rows ask the max is {totalcount}", totalcount); + return BadRequest("to many rows ask the max is " + totalcount); + } + _logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request); + + + var runes = await runesManager.GetItems(request.PageNumber, totalcount, request.orderingPropertyName, (request.descending == null ? false : (bool)request.descending)); + IEnumerable res = runes.Select(c => c.toDTO()); + if (res.Count() >= 0 || res == null) + { + _logger.LogWarning("No runes found with Id"); + return BadRequest("No runes found with Id "); + } + return Ok(res); + } + catch (Exception e) + { + _logger.LogError("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); + return BadRequest(e.Message); + + } + + + } + + // GET api//5 + [HttpGet("{id}")] + public string Get(int id) + { + return "value"; + } + + // POST api/ + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api//5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api//5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkillController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkillController.cs new file mode 100644 index 0000000..950897f --- /dev/null +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkillController.cs @@ -0,0 +1,70 @@ +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace API_LoL_Project.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class SkillController : ControllerBase + { + // GET: api/ + [HttpGet] + public async Task>> Get([FromQuery] Request.PageRequest request) + { + try + { + var totalcount = await runesManager.GetNbItems(); + if (request.count + request.index > totalcount) + { + _logger.LogWarning("to many rows ask the max is {totalcount}", totalcount); + return BadRequest("to many rows ask the max is " + totalcount); + } + _logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request); + + + var runes = await runesManager.GetItems(request.PageNumber, totalcount, request.orderingPropertyName, (request.descending == null ? false : (bool)request.descending)); + IEnumerable res = runes.Select(c => c.toDTO()); + if (res.Count() >= 0 || res == null) + { + _logger.LogWarning("No runes found with Id"); + return BadRequest("No runes found with Id "); + } + return Ok(res); + } + catch (Exception e) + { + _logger.LogError("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); + return BadRequest(e.Message); + + } + + + } + + // GET api//5 + [HttpGet("{id}")] + public string Get(int id) + { + return "value"; + } + + // POST api/ + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api//5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api//5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkinController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkinController.cs new file mode 100644 index 0000000..db11f8d --- /dev/null +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/SkinController.cs @@ -0,0 +1,70 @@ +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace API_LoL_Project.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class SkinController : ControllerBase + { + // GET: api/ + [HttpGet] + public async Task>> Get([FromQuery] Request.PageRequest request) + { + try + { + var totalcount = await runesManager.GetNbItems(); + if (request.count + request.index > totalcount) + { + _logger.LogWarning("to many rows ask the max is {totalcount}", totalcount); + return BadRequest("to many rows ask the max is " + totalcount); + } + _logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), request); + + + var runes = await runesManager.GetItems(request.PageNumber, totalcount, request.orderingPropertyName, (request.descending == null ? false : (bool)request.descending)); + IEnumerable res = runes.Select(c => c.toDTO()); + if (res.Count() >= 0 || res == null) + { + _logger.LogWarning("No runes found with Id"); + return BadRequest("No runes found with Id "); + } + return Ok(res); + } + catch (Exception e) + { + _logger.LogError("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); + return BadRequest(e.Message); + + } + + + } + + // GET api//5 + [HttpGet("{id}")] + public string Get(int id) + { + return "value"; + } + + // POST api/ + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api//5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api//5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/ExceptionMiddleware.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/ExceptionMiddleware.cs new file mode 100644 index 0000000..9ffb49d --- /dev/null +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/ExceptionMiddleware.cs @@ -0,0 +1,6 @@ +namespace API_LoL_Project.Middleware +{ + public class ExceptionMiddleware + { + } +} diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs index 3073028..294e01d 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs @@ -1,26 +1,33 @@ using Microsoft.AspNetCore.Http.Headers; +using System.Net; namespace API_LoL_Project.Middleware { - public class HttpExeption - {/* - public HttpExeption() + public class HttpExeption : Exception + + { + + public HttpStatusCode StatusCode { get; set; } + public String Message { get; set; } + + + public HttpExeption(HttpStatusCode statusCode, string message ="") : base(message) { - - + this.StatusCode = statusCode; + this.Message = message; } - void AddHeaders(this HttpRequestMessage message, RequestHeaders headers) - { - var headerParameters = headers.Parameters.Where(x => !KnownHeaders.IsContentHeader(x.Name!)); + /*void AddHeaders(this HttpRequestMessage message, RequestHeaders headers) + { + var headerParameters = headers.Parameters.Where(x => !KnownHeaders.IsContentHeader(x.Name!)); - headerParameters.ForEach(x => AddHeader(x, message.Headers)); - } - void AddHeader(Parameter parameter, HttpHeaders httpHeaders) - { - var parameterStringValue = parameter.Value!.ToString(); + headerParameters.ForEach(x => AddHeader(x, message.Headers)); + } + void AddHeader(Parameter parameter, HttpHeaders httpHeaders) + { + var parameterStringValue = parameter.Value!.ToString(); - httpHeaders.Remove(parameter.Name!); - httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue); - }*/ -} + httpHeaders.Remove(parameter.Name!); + httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue); + }*/ + } }