From af180eb528ffe1592becea007f3ef07777672fcc Mon Sep 17 00:00:00 2001 From: Louwar Date: Thu, 2 Feb 2023 19:31:13 +0100 Subject: [PATCH] Update API --- Sources/API/API.csproj | 7 ++ Sources/API/Controllers/ChampionController.cs | 119 +++++------------- .../Controllers/WeatherForecastController.cs | 34 ----- Sources/API/Dto/ChampionDto.cs | 1 - Sources/API/Dto/WeatherForecast.cs | 13 -- Sources/API/Mapping/ChampionMapper.cs | 39 ++++++ Sources/API/Program.cs | 5 +- Sources/ConsoleAPI/Program.cs | 6 + Sources/TestProject1/UnitTest1.cs | 7 +- 9 files changed, 85 insertions(+), 146 deletions(-) delete mode 100644 Sources/API/Controllers/WeatherForecastController.cs delete mode 100644 Sources/API/Dto/WeatherForecast.cs create mode 100644 Sources/API/Mapping/ChampionMapper.cs diff --git a/Sources/API/API.csproj b/Sources/API/API.csproj index f092a0c..f2eb669 100644 --- a/Sources/API/API.csproj +++ b/Sources/API/API.csproj @@ -9,6 +9,13 @@ + + + + + + + diff --git a/Sources/API/Controllers/ChampionController.cs b/Sources/API/Controllers/ChampionController.cs index f7bdc71..4e1760f 100644 --- a/Sources/API/Controllers/ChampionController.cs +++ b/Sources/API/Controllers/ChampionController.cs @@ -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 _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 logger) { - try - { - return RedirectToAction(nameof(Index)); - } - catch - { - return null; - } + _logger = logger; + this.dataManager = datamanager; } - - - [HttpGet] [Route("{id}")] public ActionResult> 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>(responseBody); - if (songs != null) + var champions = JsonConvert.DeserializeObject>(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 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> getChampion() + + + public async Task> getChampion() { - var champions = await _client.getFromJsonAsync>(); - //var reponse = await _cleint.GetAscyn("api/champion") ; + var champions = await _client.GetFromJsonAsync>(); + var reponse = await _cleint.GetAscyn("api/champion") ; return champions; } diff --git a/Sources/API/Controllers/WeatherForecastController.cs b/Sources/API/Controllers/WeatherForecastController.cs deleted file mode 100644 index 8270c8c..0000000 --- a/Sources/API/Controllers/WeatherForecastController.cs +++ /dev/null @@ -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 _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable 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(); - } - } -} \ No newline at end of file diff --git a/Sources/API/Dto/ChampionDto.cs b/Sources/API/Dto/ChampionDto.cs index ae2aa38..421f1ee 100644 --- a/Sources/API/Dto/ChampionDto.cs +++ b/Sources/API/Dto/ChampionDto.cs @@ -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 ****/ diff --git a/Sources/API/Dto/WeatherForecast.cs b/Sources/API/Dto/WeatherForecast.cs deleted file mode 100644 index 0e8427d..0000000 --- a/Sources/API/Dto/WeatherForecast.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/Sources/API/Mapping/ChampionMapper.cs b/Sources/API/Mapping/ChampionMapper.cs new file mode 100644 index 0000000..ddce066 --- /dev/null +++ b/Sources/API/Mapping/ChampionMapper.cs @@ -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 + }; + } + } +} diff --git a/Sources/API/Program.cs b/Sources/API/Program.cs index 02e7054..0fbdbae 100644 --- a/Sources/API/Program.cs +++ b/Sources/API/Program.cs @@ -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(); +builder.Services.AddScoped(); //builder.services.AddTransient(); //builder.services.AddSingleton(); diff --git a/Sources/ConsoleAPI/Program.cs b/Sources/ConsoleAPI/Program.cs index 3751555..b1a568d 100644 --- a/Sources/ConsoleAPI/Program.cs +++ b/Sources/ConsoleAPI/Program.cs @@ -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()); +} \ No newline at end of file diff --git a/Sources/TestProject1/UnitTest1.cs b/Sources/TestProject1/UnitTest1.cs index 39bc957..dc88d86 100644 --- a/Sources/TestProject1/UnitTest1.cs +++ b/Sources/TestProject1/UnitTest1.cs @@ -12,12 +12,7 @@ namespace TestProject1 { } - /*private readonly IDataManager datamanager; - public DemoController(IDataManager datamanager) - { - this.datamanager = datamanager; - }*/ - + [Test] public void TestGET() {