From f57d5af38461432ccb89565ecd7117a11e89e1e1 Mon Sep 17 00:00:00 2001 From: baollier1 Date: Thu, 2 Feb 2023 10:49:02 +0100 Subject: [PATCH] add httpGet --- Sources/apiLOL/ChampionMapper.cs | 6 +- .../apiLOL/Controllers/ControllerChampions.cs | 101 ++++++++++-------- Sources/apiLOL/Program.cs | 5 + 3 files changed, 66 insertions(+), 46 deletions(-) diff --git a/Sources/apiLOL/ChampionMapper.cs b/Sources/apiLOL/ChampionMapper.cs index 20caa24..6875ca4 100644 --- a/Sources/apiLOL/ChampionMapper.cs +++ b/Sources/apiLOL/ChampionMapper.cs @@ -2,13 +2,15 @@ namespace apiLOL { - public class ChampionMapper + public static class ChampionMapper { - public static ChampionDTO ToDTO(Champion champion) + public static ChampionDTO ToDTO(this Champion champion) { ChampionDTO championDTO = new ChampionDTO(); championDTO.Name = champion.Name; championDTO.Bio = champion.Bio; + + Console.WriteLine(championDTO.Name); return championDTO; } } diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 9e9230e..fa13e23 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -1,49 +1,62 @@ -using Microsoft.AspNetCore.Mvc; -using StubLib; -// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 - -namespace apiLOL.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class ControllerChampions : ControllerBase - { - public StubData.ChampionsManager ChampionsManager { get; set; } - - // GET: api/ - [HttpGet] - public IEnumerable Get() +using Microsoft.AspNetCore.Mvc; +using Model; +using StubLib; +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace apiLOL.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class ControllerChampions : Controller + { + private readonly IDataManager data; + //public StubData.ChampionsManager ChampionsManager { get; set; } + + public ControllerChampions(IDataManager manager) + { + + data = manager; + } + + + // GET: api/ + [HttpGet] + public async Task Get() { - return new string[] { "value1", "value2" }; + var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ToDTO()); + return Ok(champs); } // GET api//5 - [HttpGet("{id}")] - public string Get(int id) - { - ChampionsManager.GetNbItemsByCharacteristic("Akali"); - ChampionMapper.ToDTO(ChampionsManager.GetNbItemsByCharacteristic("Akali")); - //return new ChampionMapper.ToDTO(ChampionsManager.GetNbItemsByCharacteristic("Akali")); - return "value"; - } - - // POST api/ - [HttpPost] - public void Post([FromBody] string value) - { - } - - // PUT api//5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE api//5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} + [HttpGet("{id}")] + public async Task Get(int id) + { + //ChampionsManager.GetNbItemsByCharacteristic("Akali"); + //ChampionMapper.ToDTO(ChampionsManager.GetNbItemsByCharacteristic("Akali")); + //return new ChampionMapper.ToDTO(ChampionsManager.GetNbItemsByCharacteristic("Akali")); + //return "value"; + var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ToDTO()); + return Ok(champs); + + } + + // POST api/ + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT api//5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api//5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Sources/apiLOL/Program.cs b/Sources/apiLOL/Program.cs index 15eacee..1a8abfa 100644 --- a/Sources/apiLOL/Program.cs +++ b/Sources/apiLOL/Program.cs @@ -1,3 +1,6 @@ +using Model; +using StubLib; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +10,8 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); + var app = builder.Build(); // Configure the HTTP request pipeline.