diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs index c2558a0..6cd0643 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/ChampionsController.cs @@ -30,10 +30,19 @@ namespace API_LoL_Project.Controllers [HttpGet] public async Task>> Get() { - var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); - IEnumerable res = champions.Select(c => c.toDTO()); - return Ok(res); - + try + { + var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); + IEnumerable res = champions.Select(c => c.toDTO()); + return Ok(res); + } + catch (Exception e) + { + return BadRequest(e.Message); + + } + + } @@ -43,10 +52,19 @@ namespace API_LoL_Project.Controllers public async Task> Get(string name) { - var champion = await dataManager - .GetItemsByName(name, 0, await dataManager.GetNbItems()); - return Ok(new { result = champion.First().toDTO() }); + try + { + var champion = await dataManager + .GetItemsByName(name, 0, await dataManager.GetNbItems()); + ChampionDTO res = champion.First().toDTO(); + return Ok(res); + } + catch (Exception e) + { + return BadRequest(e.Message); + + } } @@ -54,32 +72,59 @@ namespace API_LoL_Project.Controllers [HttpPost] public async Task Post([FromBody] ChampionDTO value) { - await dataManager.AddItem(value.toModel()); - return Ok(); + try + { + var newChampion = value.toModel(); + await dataManager.AddItem(newChampion); + return CreatedAtAction(nameof(Get), newChampion) ; + } + catch (Exception e) + { + return BadRequest(e.Message); + + } + } // PUT api//5 [HttpPut("{name}")] public async Task Put(string name, [FromBody] ChampionDTO value) { - var champion = await dataManager - .GetItemsByName(name, 0, await dataManager.GetNbItems()); - await dataManager.UpdateItem(champion.First(), value.toModel()); - return Ok(); + + + try + { + var champion = await dataManager + .GetItemsByName(name, 0, await dataManager.GetNbItems()); + await dataManager.UpdateItem(champion.First(), value.toModel()); + return Ok(); + } + catch(Exception e) + { + return BadRequest(e.Message); + } } // DELETE api//5 [HttpDelete("{name}")] public async Task Delete(string name) { - var champion = await dataManager - .GetItemsByName(name, 0, await dataManager.GetNbItems()); - if (champion != null) await dataManager.DeleteItem(champion.First()); - else - { - return NotFound(); + try + { + var champion = await dataManager + .GetItemsByName(name, 0, await dataManager.GetNbItems()); + if (champion != null) await dataManager.DeleteItem(champion.First()); + else + { + return NotFound(); + } + return Ok(); } - return Ok(); + catch (Exception e) + { + return BadRequest(e.Message); + } + } /* [HttpGet] diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs index f7a386e..62e1908 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Controllers/RuneController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using DTO; +using Microsoft.AspNetCore.Mvc; using Model; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -8,7 +9,7 @@ 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; @@ -21,16 +22,17 @@ namespace API_LoL_Project.Controllers // GET: api/ [HttpGet] - public IEnumerable Get() + public async Task> Get() { try{ - var runes = await dataManager.GetItems(0, await dataManager.GetNbItems()); + var runes = await runesManager.GetItems(0, await runesManager.GetNbItems()); IEnumerable res = runes.Select(c => c.toDTO()); return Ok(res); } - catch(Exeption e){ - _logger.LogInformation("About page visited at {e.message}", DateTime.UtcNow.ToLongTimeString()); - new HttpException(400, 'Cannot get runes') + catch(Exception e){ + _logger.LogInformation("About get at {e.message}", DateTime.UtcNow.ToLongTimeString()); + return BadRequest(e.Message); + } } @@ -78,6 +80,6 @@ namespace API_LoL_Project.Controllers [HttpDelete("{id}")] public void Delete(int id) { - } + }*/ } } diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Mapper/RunesMappeur.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Mapper/RunesMappeur.cs index ef3ede0..18e23d6 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Mapper/RunesMappeur.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Mapper/RunesMappeur.cs @@ -3,7 +3,7 @@ using Model; namespace API_LoL_Project.Mapper { - public class RunesMappeur + public static class RunesMappeur { public static RuneDTO toDTO(this Rune item) { @@ -11,7 +11,7 @@ namespace API_LoL_Project.Mapper return new RuneDTO() { Name = item.Name, - Bio = item.Bio + Family = item.Family, }; } @@ -22,7 +22,7 @@ namespace API_LoL_Project.Mapper *//* var message = string.Format("Champion with name = {} not found", dto.Name); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));*//* }*/ - return new Rune(dto.Name); + return new Rune(dto.Name, dto.Family); } } diff --git a/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs b/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs index 8b328ee..3073028 100644 --- a/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs +++ b/EntityFramework_LoL/Sources/API_LoL_Project/Middleware/HttpExeption.cs @@ -1,21 +1,26 @@ -namespace API_LoL_Project.Middleware +using Microsoft.AspNetCore.Http.Headers; + +namespace API_LoL_Project.Middleware { public class HttpExeption - { + {/* public HttpExeption() { - public 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(); - - httpHeaders.Remove(parameter.Name!); - httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue); - } - } + + } + 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(); + + httpHeaders.Remove(parameter.Name!); + httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue); + }*/ +} } diff --git a/EntityFramework_LoL/Sources/DTO/RuneDTO.cs b/EntityFramework_LoL/Sources/DTO/RuneDTO.cs index 17b7e3e..495f618 100644 --- a/EntityFramework_LoL/Sources/DTO/RuneDTO.cs +++ b/EntityFramework_LoL/Sources/DTO/RuneDTO.cs @@ -1,4 +1,5 @@ -using System; +using Model; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,7 @@ using System.Threading.Tasks; namespace DTO { - internal class RuneDTO + public class RuneDTO { public string Name { get; set; }