pull/3/head
David D'ALMEIDA 2 years ago
parent c876900f53
commit f34951672d

@ -29,10 +29,19 @@ namespace API_LoL_Project.Controllers
// GET: api/<ChampionController> // GET: api/<ChampionController>
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<ChampionDTO>>> Get() public async Task<ActionResult<IEnumerable<ChampionDTO>>> Get()
{
try
{ {
var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); var champions = await dataManager.GetItems(0, await dataManager.GetNbItems());
IEnumerable<ChampionDTO> res = champions.Select(c => c.toDTO()); IEnumerable<ChampionDTO> res = champions.Select(c => c.toDTO());
return Ok(res); return Ok(res);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
} }
@ -43,10 +52,19 @@ namespace API_LoL_Project.Controllers
public async Task<ActionResult<ChampionDTO>> Get(string name) public async Task<ActionResult<ChampionDTO>> Get(string name)
{ {
try
{
var champion = await dataManager var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems()); .GetItemsByName(name, 0, await dataManager.GetNbItems());
ChampionDTO res = champion.First().toDTO();
return Ok(res);
}
catch (Exception e)
{
return BadRequest(e.Message);
return Ok(new { result = champion.First().toDTO() }); }
} }
@ -54,23 +72,44 @@ namespace API_LoL_Project.Controllers
[HttpPost] [HttpPost]
public async Task<IActionResult> Post([FromBody] ChampionDTO value) public async Task<IActionResult> Post([FromBody] ChampionDTO value)
{ {
await dataManager.AddItem(value.toModel()); try
return Ok(); {
var newChampion = value.toModel();
await dataManager.AddItem(newChampion);
return CreatedAtAction(nameof(Get), newChampion) ;
}
catch (Exception e)
{
return BadRequest(e.Message);
}
} }
// PUT api/<ChampionController>/5 // PUT api/<ChampionController>/5
[HttpPut("{name}")] [HttpPut("{name}")]
public async Task<IActionResult> Put(string name, [FromBody] ChampionDTO value) public async Task<IActionResult> Put(string name, [FromBody] ChampionDTO value)
{
try
{ {
var champion = await dataManager var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems()); .GetItemsByName(name, 0, await dataManager.GetNbItems());
await dataManager.UpdateItem(champion.First(), value.toModel()); await dataManager.UpdateItem(champion.First(), value.toModel());
return Ok(); return Ok();
} }
catch(Exception e)
{
return BadRequest(e.Message);
}
}
// DELETE api/<ChampionController>/5 // DELETE api/<ChampionController>/5
[HttpDelete("{name}")] [HttpDelete("{name}")]
public async Task<IActionResult> Delete(string name) public async Task<IActionResult> Delete(string name)
{
try
{ {
var champion = await dataManager var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems()); .GetItemsByName(name, 0, await dataManager.GetNbItems());
@ -81,6 +120,12 @@ namespace API_LoL_Project.Controllers
} }
return Ok(); return Ok();
} }
catch (Exception e)
{
return BadRequest(e.Message);
}
}
/* [HttpGet] /* [HttpGet]
public async Task<IActionResult> NbChampions() public async Task<IActionResult> NbChampions()

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using DTO;
using Microsoft.AspNetCore.Mvc;
using Model; using Model;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 // 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]")] [Route("api/[controller]")]
[ApiController] [ApiController]
public class RuneController : ControllerBase public class RuneController : ControllerBase
{ {/*
public IRunesManager runesManager; public IRunesManager runesManager;
// you should create a custom logger to be prety // you should create a custom logger to be prety
private readonly ILogger<RuneController> _logger; private readonly ILogger<RuneController> _logger;
@ -21,16 +22,17 @@ namespace API_LoL_Project.Controllers
// GET: api/<RuneController> // GET: api/<RuneController>
[HttpGet] [HttpGet]
public IEnumerable<string> Get() public async Task<IEnumerable<RuneDTO>> Get()
{ {
try{ try{
var runes = await dataManager.GetItems(0, await dataManager.GetNbItems()); var runes = await runesManager.GetItems(0, await runesManager.GetNbItems());
IEnumerable<RuneDTO> res = runes.Select(c => c.toDTO()); IEnumerable<RuneDTO> res = runes.Select(c => c.toDTO());
return Ok(res); return Ok(res);
} }
catch(Exeption e){ catch(Exception e){
_logger.LogInformation("About page visited at {e.message}", DateTime.UtcNow.ToLongTimeString()); _logger.LogInformation("About get at {e.message}", DateTime.UtcNow.ToLongTimeString());
new HttpException(400, 'Cannot get runes') return BadRequest(e.Message);
} }
} }
@ -78,6 +80,6 @@ namespace API_LoL_Project.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int id) public void Delete(int id)
{ {
} }*/
} }
} }

@ -3,7 +3,7 @@ using Model;
namespace API_LoL_Project.Mapper namespace API_LoL_Project.Mapper
{ {
public class RunesMappeur public static class RunesMappeur
{ {
public static RuneDTO toDTO(this Rune item) public static RuneDTO toDTO(this Rune item)
{ {
@ -11,7 +11,7 @@ namespace API_LoL_Project.Mapper
return new RuneDTO() return new RuneDTO()
{ {
Name = item.Name, 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); *//* var message = string.Format("Champion with name = {} not found", dto.Name);
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));*//* throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));*//*
}*/ }*/
return new Rune(dto.Name); return new Rune(dto.Name, dto.Family);
} }
} }

@ -1,21 +1,26 @@
namespace API_LoL_Project.Middleware using Microsoft.AspNetCore.Http.Headers;
namespace API_LoL_Project.Middleware
{ {
public class HttpExeption public class HttpExeption
{ {/*
public HttpExeption() public HttpExeption()
{ {
public void AddHeaders(this HttpRequestMessage message, RequestHeaders headers) {
}
void AddHeaders(this HttpRequestMessage message, RequestHeaders headers)
{
var headerParameters = headers.Parameters.Where(x => !KnownHeaders.IsContentHeader(x.Name!)); var headerParameters = headers.Parameters.Where(x => !KnownHeaders.IsContentHeader(x.Name!));
headerParameters.ForEach(x => AddHeader(x, message.Headers)); headerParameters.ForEach(x => AddHeader(x, message.Headers));
}
void AddHeader(Parameter parameter, HttpHeaders httpHeaders) { void AddHeader(Parameter parameter, HttpHeaders httpHeaders)
{
var parameterStringValue = parameter.Value!.ToString(); var parameterStringValue = parameter.Value!.ToString();
httpHeaders.Remove(parameter.Name!); httpHeaders.Remove(parameter.Name!);
httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue); httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue);
} }*/
}
}
} }
} }

@ -1,4 +1,5 @@
using System; using Model;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,7 +7,7 @@ using System.Threading.Tasks;
namespace DTO namespace DTO
{ {
internal class RuneDTO public class RuneDTO
{ {
public string Name { get; set; } public string Name { get; set; }

Loading…
Cancel
Save