From 01de760381d2a6faf40828196f70314082c4667e Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Sun, 26 Feb 2023 16:10:41 +0100 Subject: [PATCH 1/2] Ajout pagination et filtre --- .../Controllers/ChampionsController.cs | 54 ++++++++---- Sources/Api_UT/ChampionControllerTest.cs | 85 +++++++++++++++++++ Sources/Api_UT/UnitTest1.cs | 45 ---------- 3 files changed, 124 insertions(+), 60 deletions(-) create mode 100644 Sources/Api_UT/ChampionControllerTest.cs 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 1d4efad..4494b94 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -3,6 +3,7 @@ using Model; using StubLib; using DTO; using DTO.Mapper; +using System.CodeDom.Compiler; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -19,26 +20,49 @@ namespace API_LoL.Controllers private IChampionsManager ChampionsManager; - // GET: api/ + // GET api//5 + [HttpGet] - public async Task Get() + public async Task Get(String? name= null,String? skill = null, String? characteristic = null,int index = 0,int size =10) { - var list = await ChampionsManager.GetItems(0,await ChampionsManager.GetNbItems()); - if (list.Count() != 0) { - return Ok(list.Select(champion => champion?.toDTO())); - }else { - return NoContent(); + 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)) { + var list = await ChampionsManager.GetItemsBySkill(skill, index, size); + if (list.Count() != 0) + { + return Ok(list.Select(champion => champion?.toDTO())); + } + else { return NoContent(); } + } + else if(!string.IsNullOrEmpty (characteristic)) { + var list = await ChampionsManager.GetItems(index, size); + if (list.Count() != 0) + { + return Ok(list.Select(champion => champion?.toDTO())); + } + else { return NoContent(); } + } + else { + var list = await ChampionsManager.GetItems(index, size); + if (list.Count() != 0) + { + return Ok(list.Select(champion => champion?.toDTO())); + } + else { return NoContent(); } } } - // GET api//5 - /* - [HttpGet("{id}")] - public string Get(String name) - { - return "value"; - }*/ - // POST api/ [HttpPost] public async Task Post(ChampionDTO champion) diff --git a/Sources/Api_UT/ChampionControllerTest.cs b/Sources/Api_UT/ChampionControllerTest.cs new file mode 100644 index 0000000..16fccaa --- /dev/null +++ b/Sources/Api_UT/ChampionControllerTest.cs @@ -0,0 +1,85 @@ +using API_LoL.Controllers; +using DTO; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore.Query; +using Model; +using StubLib; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; + +namespace Api_UT +{ + [TestClass] + public class ChampionControllerTest + { + + ChampionsController api = new ChampionsController(new StubData()); + + [TestMethod] + 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", "", "") }; + IActionResult a = await api.Get(); + a.Should().NotBeNull(); + var aObject = a as OkObjectResult; + aObject.Should().NotBeNull(); + var championresult = aObject.Value as IEnumerable; + list.Should().BeEquivalentTo(championresult); + } + + [TestMethod] + public async Task Get_MoreThanLimit_BadRequest() + { + IActionResult a = await api.Get(index :0,size :11); + + a.Should().NotBeNull(); + a.Should().BeOfType(); + } + + [TestMethod] + public async Task Get_2First_OkListOf2() + { + List list = new List { new ChampionDTO("Akali", "", ""), new ChampionDTO("Aatrox", "", "") }; + + IActionResult a = await api.Get(index: 0,size: 2); + + a.Should().NotBeNull(); + a.Should().BeOfType(); + var aObject = a as OkObjectResult; + aObject.Should().NotBeNull(); + var championresult = aObject.Value as IEnumerable; + list.Should().BeEquivalentTo(championresult); + } + + [TestMethod] + public async Task Get_FilterAName_OkListOf5() + { + List list = new List { new ChampionDTO("Akali", "", ""), new ChampionDTO("Akshan", "", "") }; + + IActionResult a = await api.Get(name: "Ak"); + + a.Should().NotBeNull(); + a.Should().BeOfType(); + var aObject = a as OkObjectResult; + aObject.Should().NotBeNull(); + var championresult = aObject.Value as IEnumerable; + list.Should().BeEquivalentTo(championresult); + } + + + + [TestMethod] + public async Task Post_ValidChampion_Created() + { + 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.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); + } + + } +} \ No newline at end of file diff --git a/Sources/Api_UT/UnitTest1.cs b/Sources/Api_UT/UnitTest1.cs deleted file mode 100644 index 4c934b2..0000000 --- a/Sources/Api_UT/UnitTest1.cs +++ /dev/null @@ -1,45 +0,0 @@ -using API_LoL.Controllers; -using DTO; -using FluentAssertions; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore.Query; -using Model; -using StubLib; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; - -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.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); - } - - } -} \ No newline at end of file From 3321d55c73a95553b6d2eb82c0dac1947814ecd2 Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Sun, 26 Feb 2023 16:19:45 +0100 Subject: [PATCH 2/2] =?UTF-8?q?R=C3=A9solution=20des=20probl=C3=A8mes=20de?= =?UTF-8?q?=20skills?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/EntityFramework/ChampionEntity.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index 2f859e1..856100a 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -28,8 +28,8 @@ namespace EntityFramework [Required] public string Icon { get; set; } - public ImmutableHashSet Skills => skills.ToImmutableHashSet(); - private HashSet skills = new HashSet(); + //public ImmutableHashSet Skills => skills.ToImmutableHashSet(); + //private HashSet skills = new HashSet(); public ChampionEntity(string name,string bio,string icon) { this.Name = name; @@ -43,11 +43,11 @@ namespace EntityFramework } - +/* public bool AddSkill(Skill skill) => skills.Add(skill); public bool RemoveSkill(Skill skill) - => skills.Remove(skill); + => skills.Remove(skill);*/ } }