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

@ -9,6 +9,13 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <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> </ItemGroup>
</Project> </Project>

@ -1,94 +1,31 @@
using API.Dto; using API.Dto;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace API.Controllers namespace API.Controllers
{ {
[ApiController]
[Route("[controller]")]
public class ChampionController : ControllerBase public class ChampionController : ControllerBase
{ {
// GET: ChampionController private readonly ILogger<ChampionController> _logger;
public ActionResult Index() private readonly HttpClient _client;
{ private const string Apichampion = "api/champion";
return null; private readonly IDataManager dataManager;
}
// 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 ChampionController(IDataManager datamanager, ILogger<ChampionController> logger)
public ActionResult Delete(int id)
{ {
return null; _logger = logger;
this.dataManager = datamanager;
} }
// POST: ChampionController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return null;
}
}
[HttpGet] [HttpGet]
[Route("{id}")] [Route("{id}")]
public ActionResult<IEnumerable<ChampionDto>> GetById() public ActionResult<IEnumerable<ChampionDto>> GetById()
@ -100,13 +37,14 @@ namespace API.Controllers
Id = index, Id = index,
Name = "Stark", Name = "Stark",
Bio = "Trop fort", Bio = "Trop fort",
Icon = "nothing"
}) })
.ToArray()); .ToArray());
} }
static async Task GetJsonData() static async Task GetJsonData()
{ {
using var client = new HttpClient(); using var client = new HttpClient();
@ -120,12 +58,12 @@ namespace API.Controllers
var responseBody = await response.Content.ReadAsStringAsync(); var responseBody = await response.Content.ReadAsStringAsync();
try try
{ {
var songs = JsonConvert.DeserializeObject<List<ChampionDto>>(responseBody); var champions = JsonConvert.DeserializeObject<List<ChampionDto>>(responseBody);
if (songs != null) 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(); Console.ReadLine();
@ -137,28 +75,27 @@ namespace API.Controllers
} }
} }
/*
[HttpPost]
/*[HttpPost]
public async Task<IActionResult> post([FromBody] ChampionDto champion) public async Task<IActionResult> post([FromBody] ChampionDto champion)
{ {
return CreatedAtAction(nameof(GetById), new { id = 1 }, 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) public championHttpManager(HttpClient client)
{ {
_client = client; _client = client;
client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet"); 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 champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>();
//var reponse = await _cleint.GetAscyn("api/champion") ; var reponse = await _cleint.GetAscyn("api/champion") ;
return champions; 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 int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Bio { get; set; } public string Bio { get; set; }
public string Icon { get; set; }
/**** Méthodes ****/ /**** 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); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
@ -7,7 +10,7 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
//builder.services.AddScoped<IDataManager, StubData>(); builder.Services.AddScoped<IDataManager, StubData>();
//builder.services.AddTransient<IDataManager, StubData>(); //builder.services.AddTransient<IDataManager, StubData>();
//builder.services.AddSingleton<IDataManager, StubData>(); //builder.services.AddSingleton<IDataManager, StubData>();

@ -1,2 +1,8 @@
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!"); 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,11 +12,6 @@ namespace TestProject1
{ {
} }
/*private readonly IDataManager datamanager;
public DemoController(IDataManager datamanager)
{
this.datamanager = datamanager;
}*/
[Test] [Test]
public void TestGET() public void TestGET()

Loading…
Cancel
Save