From e6b686772ff9a5fcdeeaa225ff03c4e19a04de9f Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Tue, 7 Mar 2023 19:25:32 +0100 Subject: [PATCH] Adding getChampionByName and Get skills by champion name --- .../Controllers/ChampionsController.cs | 50 +++++++++++++++---- Sources/API_LoL/Mapper/SkinMapper.cs | 18 +++++++ Sources/DTO/SkinDTO.cs | 23 +++++++++ 3 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 Sources/API_LoL/Mapper/SkinMapper.cs create mode 100644 Sources/DTO/SkinDTO.cs diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 9aefc31..7752f0c 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -4,6 +4,9 @@ using StubLib; using DTO; using DTO.Mapper; using System.CodeDom.Compiler; +using System.Drawing; +using System; +using API_LoL.Mapper; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -15,29 +18,23 @@ namespace API_LoL.Controllers { public ChampionsController(IDataManager Manager) { - this.ChampionsManager= Manager.ChampionsMgr; + this.ChampionsManager = Manager.ChampionsMgr; + this.SkinsManager = Manager.SkinsMgr; } private IChampionsManager ChampionsManager; + private ISkinsManager SkinsManager; // GET api//5 [HttpGet] - public async Task Get(String? name= null,String? skill = null, String? characteristic = null,int index = 0,int size =10) + public async Task Get(String? skill = null, String? characteristic = null,int index = 0,int size =10) { if (size - index > 10) { return BadRequest(); } - if (!string.IsNullOrEmpty(name)) - { - var list = await ChampionsManager.GetItemsByName(name, index,size); - if (list.Count() != 0) - { - return Ok(list.Select(champion => champion?.ToDTO())); - } - else { return NoContent(); } - }else if(!string.IsNullOrEmpty(skill)) { + if(!string.IsNullOrEmpty(skill)) { var list = await ChampionsManager.GetItemsBySkill(skill, index, size); if (list.Count() != 0) { @@ -63,6 +60,37 @@ namespace API_LoL.Controllers } } + [HttpGet("name")] + public async Task GetByName(String name) + { + if (string.IsNullOrEmpty(name)) return BadRequest(); + var list = await ChampionsManager.GetItemsByName(name, 0, 1); + if (list.Count() == 1) + { + return Ok(list.Select(champion => champion?.ToDTO()).First()); + } + else { return NoContent(); } + + } + + [HttpGet("name/skills")] + public async Task GetSkillsByName(String name) + { + if (string.IsNullOrEmpty(name)) return BadRequest(); + var list = await ChampionsManager.GetItemsByName(name, 0, 1); + if (list.Count() == 1) + { + var skins = await SkinsManager.GetItemsByChampion(list.First(), 0, await SkinsManager.GetNbItemsByChampion(list.First())); + if(skins.Count() != 0) + { + return Ok(skins.Select(skin => skin?.ToDTO())); + } + else { return NoContent(); } + } + else { return NoContent(); } + + } + // POST api/ [HttpPost] public async Task Post(ChampionDTO champion) diff --git a/Sources/API_LoL/Mapper/SkinMapper.cs b/Sources/API_LoL/Mapper/SkinMapper.cs new file mode 100644 index 0000000..25ac8f6 --- /dev/null +++ b/Sources/API_LoL/Mapper/SkinMapper.cs @@ -0,0 +1,18 @@ +using DTO; +using Model; + +namespace API_LoL.Mapper +{ + public static class SkinMapper + { + public static SkinDTO ToDTO(this Skin skin) + { + return new SkinDTO(skin.Name, skin.Description, skin.Icon); + } + + public static Skin ToSkin(this SkinDTO skin) + { + return new Skin(skin.Name, null, icon:skin.Icon) ; + } + } +} diff --git a/Sources/DTO/SkinDTO.cs b/Sources/DTO/SkinDTO.cs new file mode 100644 index 0000000..32696d6 --- /dev/null +++ b/Sources/DTO/SkinDTO.cs @@ -0,0 +1,23 @@ +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public class SkinDTO + { + public string Name { get; set; } + public string Description { get; set; } + public string Icon { get; set; } + + public SkinDTO(string name,string description,string icon) { + this.Name = name; + this.Description = description; + this.Icon = icon; + + } + } +}