pull/10/head
Louwar 2 years ago
parent a5552e0926
commit af180eb528

@ -9,6 +9,13 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EFlib\EFlib.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>
</Project>

@ -1,94 +1,31 @@
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
{
// GET: ChampionController
public ActionResult Index()
{
return null;
}
// GET: ChampionController/Details/5
public ActionResult Details(int id)
{
return null;
}
// GET: ChampionController/Create
public ActionResult Create()
{
return null;
}
// POST: ChampionController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return null;
}
}
// GET: ChampionController/Edit/5
public ActionResult Edit(int id)
{
return null;
}
// POST: ChampionController/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return null;
}
}
// GET: ChampionController/Delete/5
public ActionResult Delete(int id)
{
return null;
}
private readonly ILogger<ChampionController> _logger;
private readonly HttpClient _client;
private const string Apichampion = "api/champion";
private readonly IDataManager dataManager;
// POST: ChampionController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
public ChampionController(IDataManager datamanager, ILogger<ChampionController> logger)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return null;
}
_logger = logger;
this.dataManager = datamanager;
}
[HttpGet]
[Route("{id}")]
public ActionResult<IEnumerable<ChampionDto>> GetById()
@ -100,12 +37,13 @@ namespace API.Controllers
Id = index,
Name = "Stark",
Bio = "Trop fort",
Icon = "nothing"
})
.ToArray());
}
static async Task GetJsonData()
{
@ -120,12 +58,12 @@ namespace API.Controllers
var responseBody = await response.Content.ReadAsStringAsync();
try
{
var songs = JsonConvert.DeserializeObject<List<ChampionDto>>(responseBody);
if (songs != null)
var champions = JsonConvert.DeserializeObject<List<ChampionDto>>(responseBody);
if (champions != null)
{
foreach (var song in songs)
foreach (var champion in champions)
{
Console.WriteLine($"Name: {song.Name},Year: {song.Yearreleased}");
Console.WriteLine($"Name: {champion.Name}, Bio: {champion.Bio}");
}
}
Console.ReadLine();
@ -137,28 +75,27 @@ namespace API.Controllers
}
}
/*
[HttpPost]
/*[HttpPost]
public async Task<IActionResult> post([FromBody] ChampionDto champion)
{
return CreatedAtAction(nameof(GetById), new { id = 1 },
await dataManager.ChampionMgr.AddItem(champion.ToModel()));
await dataManager.ChampionsMgr.AddItem(champion.ToModel));
}
private readonly httpclient _client;
private const string Apichampion = "api/champion";
public championHttpManager(HttpClient client)
{
_client = client;
client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet");
}
public async task<IEnumerable<ChampionDto>> getChampion()
public async Task<IEnumerable<ChampionDto>> getChampion()
{
var champions = await _client.getFromJsonAsync<IEnumerable<ChampionDto>>();
//var reponse = await _cleint.GetAscyn("api/champion") ;
var champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>();
var reponse = await _cleint.GetAscyn("api/champion") ;
return champions;
}

@ -1,34 +0,0 @@
using API.Dto;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

@ -6,7 +6,6 @@
public int Id { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public string Icon { get; set; }
/**** Méthodes ****/

@ -1,13 +0,0 @@
namespace API.Dto
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

@ -0,0 +1,39 @@
using API.Dto;
using EFlib;
namespace API.Mapping
{
public static class ChampionMapper
{
public static ChampionDto ToDto(this Champion champion)
{
if(champion == null)
{
throw new ArgumentNullException("champion null");
}
return new ChampionDto
{
Id = champion.Id,
Name = champion.Name, // je peux décider de mettre le nom en minuscule pour que le json est des noms en minuscule
Bio = champion.Bio,
};
}
public static Champion ToChampion(this ChampionDto championDto)
{
if (championDto == null)
{
throw new ArgumentNullException("Dto null");
}
return new Champion
{
Id = championDto.Id,
Name = championDto.Name,
Bio = championDto.Bio,
Icon = null
};
}
}
}

@ -1,3 +1,6 @@
using Model;
using StubLib;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@ -7,7 +10,7 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//builder.services.AddScoped<IDataManager, StubData>();
builder.Services.AddScoped<IDataManager, StubData>();
//builder.services.AddTransient<IDataManager, StubData>();
//builder.services.AddSingleton<IDataManager, StubData>();

@ -1,2 +1,8 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using (var client = new HttpClient())
{
var response = await client.GetAsync("https://www.example.com");
Console.WriteLine(await response.Content.ReadAsStringAsync());
}

@ -12,12 +12,7 @@ namespace TestProject1
{
}
/*private readonly IDataManager datamanager;
public DemoController(IDataManager datamanager)
{
this.datamanager = datamanager;
}*/
[Test]
public void TestGET()
{

Loading…
Cancel
Save