From 61c5cbff4e2c8eacfd33621ccfcb47533f132e5b Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Fri, 24 Feb 2023 15:23:25 +0100 Subject: [PATCH] avacencement filtarage panigation --- Sources/Api-lol/Controllers/Champions.cs | 69 +++++++++++++++++-- Sources/Api-lol/Controllers/Skins.cs | 27 +++++++- Sources/Api-lol/Factories/FactoChampions.cs | 9 ++- Sources/Api-lol/Factories/FactoSkins.cs | 9 ++- Sources/DTO/DtoChampions.cs | 8 +-- .../DtoControlleur/Champions/GetChampions.cs | 23 +++++++ Sources/DTO/DtoControlleur/Skins/GetSkins.cs | 23 +++++++ Sources/DTO/DtoSkins.cs | 11 +-- 8 files changed, 160 insertions(+), 19 deletions(-) create mode 100644 Sources/DTO/DtoControlleur/Champions/GetChampions.cs create mode 100644 Sources/DTO/DtoControlleur/Skins/GetSkins.cs diff --git a/Sources/Api-lol/Controllers/Champions.cs b/Sources/Api-lol/Controllers/Champions.cs index 0a982b8..168d1f1 100644 --- a/Sources/Api-lol/Controllers/Champions.cs +++ b/Sources/Api-lol/Controllers/Champions.cs @@ -1,9 +1,11 @@ using Api_lol.Factories; using DTO; +using DTO.DtoControlleur.Champions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Model; using StubLib; +using System; using System.Xml.Linq; namespace Api_lol.Controllers @@ -24,12 +26,32 @@ namespace Api_lol.Controllers [HttpGet] - public async Task Get() + public async Task Get(int index = 0,int count = 0,string classe = "All") { - var champs = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ModelToDto()); - //comm + IEnumerable champs = new List(); - return Ok(champs); + try + { + if (count == 0) + { + count = await data.ChampionsMgr.GetNbItems(); + } + if (classe != "All") + { + champs = (await data.ChampionsMgr.GetItems(index, count)).Where(e => e.Class.ToString() == classe).Select(Model => Model.ModelToDto()); + } + else + { + champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ModelToDto()); + } + } + catch(Exception ex) + { + return BadRequest(ex.Message); + } + GetChampions dto = new GetChampions(champs.ToList(), index, count); + + return Ok(dto); } @@ -37,7 +59,16 @@ namespace Api_lol.Controllers public async Task Post(DtoChampions champDTO) { Champion tmp = champDTO.DtoToModel(); - Champion champion = await data.ChampionsMgr.AddItem(tmp); + Champion champion; + + try + { + champion = await data.ChampionsMgr.AddItem(tmp); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } DtoChampions dtoChamp = champion.ModelToDto(); return CreatedAtAction(nameof(GetChampion),new { name = dtoChamp.name},dtoChamp); } @@ -48,7 +79,16 @@ namespace Api_lol.Controllers [Route("{name}")] public async Task GetChampion(string name) { - Champion champion = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name); + Champion champion; + + try + { + champion = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } if ( champion == null) { return BadRequest(); @@ -57,5 +97,22 @@ namespace Api_lol.Controllers return Ok(result); } + [HttpDelete] + public async Task DeleteChampion(string name = "") + { + Champion champion; + + try + { + champion = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(e => e.Name == name); + } catch (Exception ex) + { + return BadRequest(ex.Message); + } + await data.ChampionsMgr.DeleteItem(champion); + + return Ok(champion.ModelToDto()); + } + } } diff --git a/Sources/Api-lol/Controllers/Skins.cs b/Sources/Api-lol/Controllers/Skins.cs index 55152ce..c5d025e 100644 --- a/Sources/Api-lol/Controllers/Skins.cs +++ b/Sources/Api-lol/Controllers/Skins.cs @@ -1,5 +1,6 @@ using Api_lol.Factories; using DTO; +using DTO.DtoControlleur.Skins; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Model; @@ -22,11 +23,31 @@ namespace Api_lol.Controllers } [HttpGet] - public async Task Get() + public async Task Get(int index = 0,int count = 0,float price = 0) { - var skins = (await data.SkinsMgr.GetItems(0, await data.SkinsMgr.GetNbItems())).Select(Model => Model.ModelToDto()); + IEnumerable skins = new List(); + try + { + if (count == 0) + { + count = await data.SkinsMgr.GetNbItems(); + } + if ( price != 0 ) + { + skins = (await data.SkinsMgr.GetItems(index, count)).Where(e => e.Price == price).Select(Model => Model.ModelToDto()); + } + else + { + skins = (await data.SkinsMgr.GetItems(index, count)).Select(Model => Model.ModelToDto()); + } + } + catch(Exception ex) + { + return BadRequest(ex.Message); + } + GetSkins dto = new GetSkins(skins.ToList(),index,count); - return Ok(skins); + return Ok(dto); } [HttpGet] diff --git a/Sources/Api-lol/Factories/FactoChampions.cs b/Sources/Api-lol/Factories/FactoChampions.cs index 8d61f11..6a997f9 100644 --- a/Sources/Api-lol/Factories/FactoChampions.cs +++ b/Sources/Api-lol/Factories/FactoChampions.cs @@ -13,7 +13,14 @@ namespace Api_lol.Factories public static DtoChampions ModelToDto(this Champion champ) { - return new DtoChampions(champ.Name); + DtoChampions dto = new DtoChampions(); + + dto.name = champ.Name; + dto.classe = champ.Class.ToString(); + dto.bio = champ.Bio; + dto.icon = champ.Icon; + + return dto; } } } diff --git a/Sources/Api-lol/Factories/FactoSkins.cs b/Sources/Api-lol/Factories/FactoSkins.cs index 6b592bc..122326c 100644 --- a/Sources/Api-lol/Factories/FactoSkins.cs +++ b/Sources/Api-lol/Factories/FactoSkins.cs @@ -7,7 +7,14 @@ namespace Api_lol.Factories { public static DtoSkins ModelToDto(this Skin skin) { - return new DtoSkins(skin.Name); + DtoSkins dto = new DtoSkins(); + + dto.name = skin.Name; + dto.description = skin.Description; + dto.price = skin.Price; + dto.icon = skin.Icon; + + return dto; } public static Skin DtoToModel(this DtoSkins skinDto,Champion champ) diff --git a/Sources/DTO/DtoChampions.cs b/Sources/DTO/DtoChampions.cs index ae2bbb0..7c3f0ab 100644 --- a/Sources/DTO/DtoChampions.cs +++ b/Sources/DTO/DtoChampions.cs @@ -10,9 +10,9 @@ namespace DTO { public string name { get; set; } - public DtoChampions(string name) - { - this.name = name; - } + public string classe { get; set; } + + public string bio { get; set; } + public string icon { get; set; } } } diff --git a/Sources/DTO/DtoControlleur/Champions/GetChampions.cs b/Sources/DTO/DtoControlleur/Champions/GetChampions.cs new file mode 100644 index 0000000..7ebe5cb --- /dev/null +++ b/Sources/DTO/DtoControlleur/Champions/GetChampions.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO.DtoControlleur.Champions +{ + public class GetChampions + { + public List listeChampions { get; set; } + + public int index { get; set; } + public int count { get; set; } + + public GetChampions(List liste, int indexDto,int countDto) + { + index = indexDto; + count = countDto; + listeChampions = liste; + } + } +} diff --git a/Sources/DTO/DtoControlleur/Skins/GetSkins.cs b/Sources/DTO/DtoControlleur/Skins/GetSkins.cs new file mode 100644 index 0000000..6949f31 --- /dev/null +++ b/Sources/DTO/DtoControlleur/Skins/GetSkins.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO.DtoControlleur.Skins +{ + public class GetSkins + { + public List listeSkins { get; set; } + + public int index { get; set; } + public int count { get; set; } + + public GetSkins(List liste, int indexDto, int countDto) + { + index = indexDto; + count = countDto; + listeSkins = liste; + } + } +} diff --git a/Sources/DTO/DtoSkins.cs b/Sources/DTO/DtoSkins.cs index d1a002e..0054ac9 100644 --- a/Sources/DTO/DtoSkins.cs +++ b/Sources/DTO/DtoSkins.cs @@ -10,9 +10,12 @@ namespace DTO { public string name { get; set; } - public DtoSkins(string name) - { - this.name = name; - } + public float price { get; set; } + public string description { get; set; } + + public string icon { get; set; } + + + } } \ No newline at end of file