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 _logger; private readonly HttpClient _client; private readonly IDataManager dataManager; private const string Apichampion = "api/champion"; public ChampionController(IDataManager datamanager, ILogger logger) { _logger = logger; this.dataManager = datamanager; } /* public championHttpManager(HttpClient client) { _client = client; client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet"); } */ /**** Méthodes GET ****/ [HttpGet] public async Task> Get() { StubData stub = new StubData(); IEnumerable listeChamp = await stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result); List champs = new List(); listeChamp.ToList().ForEach(c => champs.Add(c.ToDto())); return Ok(champs); } [HttpGet] [Route("{id}")] public ActionResult> GetById() { BadRequest("404"); return Ok(Enumerable.Range(1, 5).Select(index => new ChampionDto { Id = index, Name = "Stark", Bio = "Trop fort", }) .ToArray()); } static async Task GetJsonData() { using var client = new HttpClient(); client.BaseAddress = new Uri("https://localhost:7175"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync("/api/ChampionDto"); if (response.IsSuccessStatusCode) { var responseBody = await response.Content.ReadAsStringAsync(); try { var champions = JsonConvert.DeserializeObject>(responseBody); if (champions != null) { foreach (var champion in champions) { Console.WriteLine($"Name: {champion.Name}, Bio: {champion.Bio}"); } } Console.ReadLine(); } catch (JsonReaderException) { Console.WriteLine("Something went wrong."); } } } /* public async Task> getJson() { var champions = await _client.GetFromJsonAsync>(); var reponse = await _cleint.GetAscyn("api/champion"); return champions; }*/ /**** Méthodes POST ****/ /* [HttpPost] public async Task post([FromBody] ChampionDto champion) { return CreatedAtAction(nameof(GetById), new { id = 1 }, await dataManager.ChampionsMgr.AddItem(champion.ToModel)); } public async void addchampion(ChampionDto champion) { _clientLpostAsJsonAscync(ApiChampion, champion); }*/ /**** Méthodes DELETE ****/ /**** Méthodes PUT ****/ } }