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.

110 lines
3.3 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

using API.Dto;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;
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 HttpClient _client;
private const string Apichampion = "api/champion";
private readonly IDataManager dataManager;
public ChampionController(IDataManager datamanager, ILogger<ChampionController> logger)
{
_logger = logger;
this.dataManager = datamanager;
}
[HttpGet]
[Route("{id}")]
public ActionResult<IEnumerable<ChampionDto>> 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<List<ChampionDto>>(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.");
}
}
}
/*[HttpPost]
public async Task<IActionResult> post([FromBody] ChampionDto champion)
{
return CreatedAtAction(nameof(GetById), new { id = 1 },
await dataManager.ChampionsMgr.AddItem(champion.ToModel));
}
public championHttpManager(HttpClient client)
{
_client = client;
client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet");
}
public async Task<IEnumerable<ChampionDto>> getChampion()
{
var champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>();
var reponse = await _cleint.GetAscyn("api/champion") ;
return champions;
}
public async void addchampion(ChampionDto champion)
{
_clientLpostAsJsonAscync<Champion>(ApiChampion, champion);
}
*/
}
}