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>
[HttpGet]
public async Task<ActionResult<IEnumerable<ChampionDTO>>> Get()
{
try
{
var champions = await dataManager.GetItems(0, await dataManager.GetNbItems());
IEnumerable<ChampionDTO> 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<ActionResult<ChampionDTO>> Get(string name)
{
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);
return Ok(new { result = champion.First().toDTO() });
}
}
@ -54,23 +72,44 @@ namespace API_LoL_Project.Controllers
[HttpPost]
public async Task<IActionResult> 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/<ChampionController>/5
[HttpPut("{name}")]
public async Task<IActionResult> Put(string name, [FromBody] ChampionDTO value)
{
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/<ChampionController>/5
[HttpDelete("{name}")]
public async Task<IActionResult> Delete(string name)
{
try
{
var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems());
@ -81,6 +120,12 @@ namespace API_LoL_Project.Controllers
}
return Ok();
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
/* [HttpGet]
public async Task<IActionResult> NbChampions()

@ -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<RuneController> _logger;
@ -21,16 +22,17 @@ namespace API_LoL_Project.Controllers
// GET: api/<RuneController>
[HttpGet]
public IEnumerable<string> Get()
public async Task<IEnumerable<RuneDTO>> Get()
{
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());
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)
{
}
}*/
}
}

@ -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);
}
}

@ -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) {
}
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) {
}
void AddHeader(Parameter parameter, HttpHeaders httpHeaders)
{
var parameterStringValue = parameter.Value!.ToString();
httpHeaders.Remove(parameter.Name!);
httpHeaders.TryAddWithoutValidation(parameter.Name!, parameterStringValue);
}
}
}
}*/
}
}

@ -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; }

Loading…
Cancel
Save