You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.4 KiB

using API.Dto;
using API.Mapping;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;
using StubLib;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class ChampionController : ControllerBase
{
private readonly ILogger<ChampionController> _logger;
private readonly StubData stub = new StubData();
private readonly IDataManager dataManager; // Pour plus tard pour le momment c'est avec le stub
// private const string Apichampion = "api/champion";
// private readonly HttpClient _client;
public ChampionController(ILogger<ChampionController> logger)
{
_logger = logger;
}
/* public championHttpManager(HttpClient client)
{
_client = client;
client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet");
}
public async Task<IEnumerable<ChampionDto>> getJson()
{
var champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>();
var reponse = await _client.GetAsync("api/champion");
return champions;
}
*/
/**** Méthodes GET ****
[HttpGet]
public async Task<ActionResult<ChampionDto>> GetChamps()
{
IEnumerable<Champion?> Champs = await stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result);
List<ChampionDto> DtoChamps = new List<ChampionDto>();
Champs.ToList().ForEach(c => DtoChamps.Add(c.ToDto()));
return Ok(DtoChamps);
}
[HttpGet]
[Route("{id}")]
public async Task<ActionResult<ChampionDto>> GetChampById(int id)
{
BadRequest("404");
IEnumerable<Champion?> Champs = await stub.ChampionsMgr.GetItems(id, 1);
if (id >= 0 && id < stub.ChampionsMgr.GetNbItems().Result)
{
return Ok(Champs.First().ToDto());
}
return BadRequest("404");
}
[HttpGet("{id}/Skins")]
public async Task<ActionResult<SkinDto>> GetSkinsChamp(int id)
{
IEnumerable<Champion?> lcha = await stub.ChampionsMgr.GetItems(id, 1);
if (id >= 0 && id < stub.ChampionsMgr.GetNbItems().Result)
{
Champion ca = lcha.First();
IEnumerable<Skin?> lsk = await stub.SkinsMgr.GetItemsByChampion(ca, 0, stub.SkinsMgr.GetNbItemsByChampion(ca).Result);
List<SkinDto> skins = new List<SkinDto>();
lsk.ToList().ForEach(s => skins.Add(s.ToSkin()));
return Ok(skins);
}
return BadRequest();
}
/**** Méthodes POST ****
[HttpPost("Ajouter/{nom}")]
public async Task<ActionResult> PostChampName(string nom)
{
Champion ca = new Champion(nom);
await stub.ChampionsMgr.AddItem(ca);
return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetNbItems().Result - 1 }, ca.ToDto());
}
[HttpPost("Ajouter")]
public async Task<IActionResult> PostChamp([FromBody] ChampionDto c)
{
Champion ca = c.ToModel();
await stub.ChampionsMgr.AddItem(ca);
return CreatedAtAction(nameof(stub.ChampionsMgr.GetItemsByName), new { Name = c.Name }, c);
}
[HttpPost]
public async Task<IActionResult> post([FromBody] ChampionDto champion)
{
return CreatedAtAction(nameof(GetChampById), new { id = 1 },
await dataManager.ChampionsMgr.AddItem(champion.ToModel));
}
public async void addchampion(ChampionDto champion)
{
_clientLpostAsJsonAscync<Champion>(ApiChampion, champion);
}
/**** Méthodes DELETE ****
[HttpDelete("Supprimer/{id}")]
public async Task<IActionResult> DeleteChamp(int id)
{
IEnumerable<Champion?> lcha = await stub.ChampionsMgr.GetItems(id, 1);
if (id >= 0 && id < stub.ChampionsMgr.GetNbItems().Result)
{
Champion ca = lcha.First();
stub.ChampionsMgr.DeleteItem(ca);
return Ok();
}
return BadRequest();
}
/**** Méthodes PUT ****
[HttpPut("Modifier/{nom}")]// CA C4EST PAS FINI
public async Task<ActionResult> PutChampName(string nom)
{
Champion ca = new Champion(nom);
await stub.ChampionsMgr.AddItem(ca);
return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetNbItems().Result - 1 }, ca.ToDto());
}
[HttpPut("Modifier")]
public async Task<IActionResult> PutChamp([FromBody] ChampionDto c, [FromBody] ChampionDto cNouv)
{
Champion ca = c.ToModel();
Champion caNouv = cNouv.ToModel();
await stub.ChampionsMgr.UpdateItem(ca, caNouv);
return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(ca) }, ca);
}*/
}
}