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 1/3] 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; + + } + } +} -- 2.36.3 From f4b77f66e7cdbd1effd254f9cd503a8743c09c66 Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Tue, 7 Mar 2023 22:07:52 +0100 Subject: [PATCH 2/3] Adding ChampionClass to ChampionDTO --- .../Controllers/ChampionsController.cs | 27 +++++++++++---- Sources/API_LoL/Mapper/ChampionClassMapper.cs | 34 +++++++++++++++++++ Sources/API_LoL/Mapper/ChampionMapper.cs | 7 ++-- Sources/DTO/ChampionClassDTO.cs | 16 +++++++++ Sources/DTO/ChampionDTO.cs | 15 +++----- 5 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 Sources/API_LoL/Mapper/ChampionClassMapper.cs create mode 100644 Sources/DTO/ChampionClassDTO.cs diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 7752f0c..8ef8ca7 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -73,26 +73,41 @@ namespace API_LoL.Controllers } - [HttpGet("name/skills")] - public async Task GetSkillsByName(String name) + [HttpGet("name/skins")] + public async Task GetSkinsByName(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) + if (skins.Count() != 0) { return Ok(skins.Select(skin => skin?.ToDTO())); } else { return NoContent(); } } else { return NoContent(); } - } - // POST api/ - [HttpPost] + [HttpGet("name/largeImage")] + public async Task GetLargeImageByName(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) { if (champion == null) diff --git a/Sources/API_LoL/Mapper/ChampionClassMapper.cs b/Sources/API_LoL/Mapper/ChampionClassMapper.cs new file mode 100644 index 0000000..638f500 --- /dev/null +++ b/Sources/API_LoL/Mapper/ChampionClassMapper.cs @@ -0,0 +1,34 @@ +using DTO; +using Model; + +namespace API_LoL.Mapper +{ + public static class ChampionClassMapper + { + public static string ToDTO(this ChampionClass championClass) + { + return championClass.ToString(); + } + + public static ChampionClass ToChampionClass(this String championClass) + { + switch (championClass) + { + case "Assassin": + return ChampionClass.Assassin; + case "Fighter": + return ChampionClass.Fighter; + case "Mage": + return ChampionClass.Mage; + case "Marksman": + return ChampionClass.Marksman; + case "Support": + return ChampionClass.Support; + case "Tank": + return ChampionClass.Tank; + default: + return ChampionClass.Unknown; + } + } + } +} diff --git a/Sources/API_LoL/Mapper/ChampionMapper.cs b/Sources/API_LoL/Mapper/ChampionMapper.cs index c2e8d0b..3dd6a11 100644 --- a/Sources/API_LoL/Mapper/ChampionMapper.cs +++ b/Sources/API_LoL/Mapper/ChampionMapper.cs @@ -1,4 +1,5 @@ -using Model; +using API_LoL.Mapper; +using Model; using System; using System.Collections.Generic; using System.Linq; @@ -11,13 +12,13 @@ namespace DTO.Mapper { public static ChampionDTO ToDTO(this Champion champion) { - return new ChampionDTO(champion.Name, champion.Bio, champion.Icon); + return new ChampionDTO(champion.Name, champion.Bio, champion.Icon,champion.Class.ToDTO().ToString()); //return new ChampionDTO(champion.Name, champion.Bio, champion.Icon, champion.Skills); } public static Champion ToChampion(this ChampionDTO champion) { - Champion champ = new Champion(champion.Name, ChampionClass.Unknown, champion.Icon, "", champion.Bio); + Champion champ = new Champion(champion.Name, champion.Class.ToChampionClass(), champion.Icon, "", champion.Bio); //foreach (Skill skill in champion.Skills) //{ diff --git a/Sources/DTO/ChampionClassDTO.cs b/Sources/DTO/ChampionClassDTO.cs new file mode 100644 index 0000000..c85525f --- /dev/null +++ b/Sources/DTO/ChampionClassDTO.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public class ChampionClassDTO + { + public string Name; + public ChampionClassDTO(string name) { + this.Name = name; + } + } +} diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index 922d56e..723d7b4 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -5,26 +5,19 @@ namespace DTO { public class ChampionDTO { - public ChampionDTO(string name, string bio, string icon) + public ChampionDTO(string name, string bio, string icon, string championClassDTO) { Name = name; Bio = bio; Icon = icon; + Class = championClassDTO; } - - //public ChampionDTO(string name, string bio, string icon, ICollection skills) - //{ - // Name = name; - // Bio = bio; - // Icon = icon; - // Skills = skills; - //} - public string Name { get; set; } public string Bio { get; set; } - //public ChampionClass Class { get; set; } public string Icon { get; set; } + public string Class { get; set; } + public bool equals(ChampionDTO other) { return other.Name==this.Name && other.Bio==this.Bio && other.Icon==this.Icon; -- 2.36.3 From dc64edf23a5c4c77fce476070f9be3af6906a114 Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Tue, 7 Mar 2023 22:19:27 +0100 Subject: [PATCH 3/3] bug solving :bug: --- .../Controllers/ChampionsController.cs | 30 ++++++-------- Sources/Api_UT/ChampionControllerTest.cs | 10 ++--- Sources/Api_UT/UnitTest1.cs | 41 ------------------- 3 files changed, 17 insertions(+), 64 deletions(-) delete mode 100644 Sources/Api_UT/UnitTest1.cs diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 8ef8ca7..ce8b87e 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -7,6 +7,7 @@ using System.CodeDom.Compiler; using System.Drawing; using System; using API_LoL.Mapper; +using System.Xml.Linq; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -28,13 +29,22 @@ namespace API_LoL.Controllers // GET api//5 [HttpGet] - public async Task Get(String? skill = null, String? characteristic = null,int index = 0,int size =10) + public async Task Get(string? name = null,String? skill = null, String? characteristic = null,int index = 0,int size =10) { if (size - index > 10) { return BadRequest(); } - if(!string.IsNullOrEmpty(skill)) { + 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)) { var list = await ChampionsManager.GetItemsBySkill(skill, index, size); if (list.Count() != 0) { @@ -90,22 +100,6 @@ namespace API_LoL.Controllers else { return NoContent(); } } - [HttpGet("name/largeImage")] - public async Task GetLargeImageByName(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_UT/ChampionControllerTest.cs b/Sources/Api_UT/ChampionControllerTest.cs index 16fccaa..a3ca675 100644 --- a/Sources/Api_UT/ChampionControllerTest.cs +++ b/Sources/Api_UT/ChampionControllerTest.cs @@ -21,7 +21,7 @@ namespace Api_UT public async Task Get_Default_OkList() { - List list = new List {new ChampionDTO("Akali","",""), new ChampionDTO("Aatrox", "", ""), new ChampionDTO("Ahri", "", ""), new ChampionDTO("Akshan", "", ""), new ChampionDTO("Bard", "", ""), new ChampionDTO("Alistar", "", "") }; + List list = new List {new ChampionDTO("Akali","","","Assassin"), new ChampionDTO("Aatrox", "", "", "Fighter"), new ChampionDTO("Ahri", "", "", "Mage"), new ChampionDTO("Akshan", "", "", "Marksman"), new ChampionDTO("Bard", "", "","Support"), new ChampionDTO("Alistar", "", "","Tank") }; IActionResult a = await api.Get(); a.Should().NotBeNull(); var aObject = a as OkObjectResult; @@ -42,7 +42,7 @@ namespace Api_UT [TestMethod] public async Task Get_2First_OkListOf2() { - List list = new List { new ChampionDTO("Akali", "", ""), new ChampionDTO("Aatrox", "", "") }; + List list = new List { new ChampionDTO("Akali", "", "", "Assassin"), new ChampionDTO("Aatrox", "", "", "Fighter") }; IActionResult a = await api.Get(index: 0,size: 2); @@ -57,7 +57,7 @@ namespace Api_UT [TestMethod] public async Task Get_FilterAName_OkListOf5() { - List list = new List { new ChampionDTO("Akali", "", ""), new ChampionDTO("Akshan", "", "") }; + List list = new List { new ChampionDTO("Akali", "", "", "Assassin"), new ChampionDTO("Akshan", "", "", "Marksman") }; IActionResult a = await api.Get(name: "Ak"); @@ -75,9 +75,9 @@ namespace Api_UT public async Task Post_ValidChampion_Created() { ChampionsController api = new ChampionsController(new StubData()); - IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon")); + IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon", "Assassin")); Assert.IsNotNull(a); - ChampionDTO champ = new ChampionDTO("nom", "bio", "icon"); + ChampionDTO champ = new ChampionDTO("nom", "bio", "icon","Assassin"); Assert.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); } diff --git a/Sources/Api_UT/UnitTest1.cs b/Sources/Api_UT/UnitTest1.cs deleted file mode 100644 index de78d2b..0000000 --- a/Sources/Api_UT/UnitTest1.cs +++ /dev/null @@ -1,41 +0,0 @@ -using API_LoL.Controllers; -using DTO; -using FluentAssertions; -using Microsoft.AspNetCore.Mvc; -using Model; -using StubLib; - -namespace Api_UT -{ - [TestClass] - public class UnitTest1 - { - [TestMethod] - public async Task TestGet() - { - List list = new List {new ChampionDTO("Akali","",""), new ChampionDTO("Aatrox", "", ""), new ChampionDTO("Ahri", "", ""), new ChampionDTO("Akshan", "", ""), new ChampionDTO("Bard", "", ""), new ChampionDTO("Alistar", "", "") }; - ChampionsController api = new ChampionsController(new StubData()); - IActionResult a = await api.Get(); - - /// utilisation du nuggets fluentAssertion - //Assert.IsNotNull(a); - a.Should().NotBeNull(); - //Assert.AreEqual(list,((OkObjectResult)a).Value); - var aObject = a as OkObjectResult; - aObject.Should().NotBeNull(); - var championresult = aObject.Value as IEnumerable; - list.Should().BeEquivalentTo(championresult); - } - - [TestMethod] - public async Task TestPostValid() - { - ChampionsController api = new ChampionsController(new StubData()); - IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon")); - Assert.IsNotNull(a); - ChampionDTO champ = new ChampionDTO("nom", "bio", "icon"); - //Assert.AreEqual(champ,((CreatedAtActionResult)a).Value); - } - - } -} \ No newline at end of file -- 2.36.3