From dd0e2d98e8e8631c8434f4f2acd20aeb287c0717 Mon Sep 17 00:00:00 2001 From: Theo DUPIN Date: Tue, 7 Mar 2023 21:16:38 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20en=20commentaire=20d'interface=20non=20i?= =?UTF-8?q?mpl=C3=A9ment=C3=A9e=20pour=20tests=20Drone=20et=20Sonar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ConsoleAPI/Program.cs | 6 +- .../DBChampionManager.cs | 80 ++++++++++--------- Sources/DBDataManager/DBDataManager.csproj | 14 ++++ Sources/DBDataManager/DbDataManager.cs | 22 +++++ Sources/DBDataManager/Extensions.cs | 60 ++++++++++++++ Sources/EntityFrameworkLib/DbDataManager.cs | 21 ----- Sources/LeagueOfLegends.sln | 8 +- Sources/Model/Champion.cs | 6 ++ .../Controllers/ChampionsController.cs | 4 +- 9 files changed, 156 insertions(+), 65 deletions(-) rename Sources/{EntityFrameworkLib => DBDataManager}/DBChampionManager.cs (55%) create mode 100644 Sources/DBDataManager/DBDataManager.csproj create mode 100644 Sources/DBDataManager/DbDataManager.cs create mode 100644 Sources/DBDataManager/Extensions.cs delete mode 100644 Sources/EntityFrameworkLib/DbDataManager.cs diff --git a/Sources/ConsoleAPI/Program.cs b/Sources/ConsoleAPI/Program.cs index a0e0d57..86677b7 100644 --- a/Sources/ConsoleAPI/Program.cs +++ b/Sources/ConsoleAPI/Program.cs @@ -27,7 +27,7 @@ namespace ConsoleAPI } // exécute la requête HTTP GET pour obtenir un champion par nom - HttpResponseMessage response1 = await client.GetAsync("/Akali"); + HttpResponseMessage response1 = await client.GetAsync("/Bard"); if (response1.IsSuccessStatusCode) { @@ -46,8 +46,8 @@ namespace ConsoleAPI Name = "Ashe", Bio = "La Comtesse du froid", }; - /*response = await client.PostAsync("/addChampion", new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(champion), System.Text.Encoding.UTF8, "application/json")); - HttpResponseMessage response = await client.PostAsync("/addChampion", Content);*/ + /*response = await client.PostAsync("/addChampion", new StringContent("application/json")); + HttpResponseMessage responseMessage = await client.PostAsync("/addChampion", new StringContent("application/json"));*/ // vérifie si la réponse est valide if (response.IsSuccessStatusCode) diff --git a/Sources/EntityFrameworkLib/DBChampionManager.cs b/Sources/DBDataManager/DBChampionManager.cs similarity index 55% rename from Sources/EntityFrameworkLib/DBChampionManager.cs rename to Sources/DBDataManager/DBChampionManager.cs index f3f8f41..aeb3bc2 100644 --- a/Sources/EntityFrameworkLib/DBChampionManager.cs +++ b/Sources/DBDataManager/DBChampionManager.cs @@ -1,4 +1,5 @@ -using EntityFrameworkLib.Mappers; +using EntityFrameworkLib; +using EntityFrameworkLib.Mappers; using Model; using Shared; using System; @@ -7,7 +8,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace EntityFrameworkLib +namespace DBDataManager { public class DBChampionManager : IChampionsManager { @@ -53,55 +54,62 @@ namespace EntityFrameworkLib return champions; } + private Func filterByCharacteristic = (champ, charName) => champ.Characteristics.Keys.Any(k => k.Contains(charName, StringComparison.InvariantCultureIgnoreCase)); + Task> IChampionsManager.GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName, bool descending) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetItemsWithFilterAndOrdering( + champions => filterByCharacteristic(champions, charName), + index, count, orderingPropertyName, descending); + + private Func filterByClass = (champ, championClass) => champ.Class == championClass; Task> IChampionsManager.GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName, bool descending) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetItemsWithFilterAndOrdering( + champions => filterByClass(champions, championClass), + index, count, orderingPropertyName, descending); + + private Func filterByName = (champ, substring) => champ.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); Task> IGenericDataManager.GetItemsByName(string substring, int index, int count, string? orderingPropertyName, bool descending) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetItemsWithFilterAndOrdering( + champions => filterByName(champions, substring), + index, count, orderingPropertyName, descending); Task> IChampionsManager.GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName, bool descending) { throw new NotImplementedException(); } + + private Func filterBySkill = (champ, skill) => skill != null && champ.Skills.Contains(skill!); + Task> IChampionsManager.GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName, bool descending) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetItemsWithFilterAndOrdering( + champions => filterBySkill(champions, skill), + index, count, orderingPropertyName, descending); + + + private static Func filterBySkillSubstring = (champ, skill) => champ.Skills.Any(s => s.Name.Contains(skill, StringComparison.InvariantCultureIgnoreCase)); Task> IChampionsManager.GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName, bool descending) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetItemsWithFilterAndOrdering( + champions => filterBySkillSubstring(champions, skill), + index, count, orderingPropertyName, descending); Task IGenericDataManager.GetNbItems() - { - throw new NotImplementedException(); - } + => Task.FromResult(context.Champions.Count()); Task IChampionsManager.GetNbItemsByCharacteristic(string charName) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetNbItemsWithFilter( + champions => filterByCharacteristic(champions, charName)); Task IChampionsManager.GetNbItemsByClass(ChampionClass championClass) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetNbItemsWithFilter( + champions => filterByClass(champions, championClass)); Task IGenericDataManager.GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetNbItemsWithFilter( + champions => filterByName(champions, substring)); Task IChampionsManager.GetNbItemsByRunePage(RunePage? runePage) { @@ -109,18 +117,14 @@ namespace EntityFrameworkLib } Task IChampionsManager.GetNbItemsBySkill(Skill? skill) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetNbItemsWithFilter( + champions => filterBySkill(champions, skill)); Task IChampionsManager.GetNbItemsBySkill(string skill) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().GetNbItemsWithFilter( + champions => filterBySkillSubstring(champions, skill)); Task IGenericDataManager.UpdateItem(Champion? oldItem, Champion? newItem) - { - throw new NotImplementedException(); - } + => context.Champions.toPocos().UpdateItem(oldItem, newItem); } } diff --git a/Sources/DBDataManager/DBDataManager.csproj b/Sources/DBDataManager/DBDataManager.csproj new file mode 100644 index 0000000..47a7c34 --- /dev/null +++ b/Sources/DBDataManager/DBDataManager.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Sources/DBDataManager/DbDataManager.cs b/Sources/DBDataManager/DbDataManager.cs new file mode 100644 index 0000000..c2951d5 --- /dev/null +++ b/Sources/DBDataManager/DbDataManager.cs @@ -0,0 +1,22 @@ +using DBDataManager; +using Model; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DBDataManager +{ + public class DBDataManager //: IDataManager + { + //public IChampionsManager ChampionsManager => new DBChampionManager(); + + //public ISkinsManager SkinsManager => throw new NotImplementedException(); + + //public IRunesManager RunesManager=> throw new NotImplementedException(); + + //public IRunePagesManager RunePagesManager=> throw new NotImplementedException(); + } +} diff --git a/Sources/DBDataManager/Extensions.cs b/Sources/DBDataManager/Extensions.cs new file mode 100644 index 0000000..ef92821 --- /dev/null +++ b/Sources/DBDataManager/Extensions.cs @@ -0,0 +1,60 @@ +using EntityFrameworkLib; +using EntityFrameworkLib.Mappers; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DBDataManager +{ + static class Extensions + { + internal static Task> GetItemsWithFilterAndOrdering(this IEnumerable collection, + Func filter, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + IEnumerable temp = collection; + temp = temp.Where(item => filter(item)); + if (orderingPropertyName != null) + { + var prop = typeof(T).GetProperty(orderingPropertyName!); + if (prop != null) + { + temp = descending ? temp.OrderByDescending(item => prop.GetValue(item)) + : temp.OrderBy(item => prop.GetValue(item)); + } + } + return Task.FromResult>(temp.Skip(index * count).Take(count)); + } + + public static IEnumerable toPocos(this IEnumerable champions) + { + List result = new List(); + foreach(ChampionEntity champion in champions) + { + result.Add(champion.toModel()); + } + return result; + } + + internal static Task GetNbItemsWithFilter(this IEnumerable collection, Func filter) + { + return Task.FromResult(collection.Count(item => filter(item))); + } + + internal static Task UpdateItem(this IList collection, T? oldItem, T? newItem) + { + if (oldItem == null || newItem == null) return Task.FromResult(default(T)); + + if (!collection.Contains(oldItem)) + { + return Task.FromResult(default(T)); + } + + collection.Remove(oldItem!); + collection.Add(newItem!); + return Task.FromResult(newItem); + } + } +} diff --git a/Sources/EntityFrameworkLib/DbDataManager.cs b/Sources/EntityFrameworkLib/DbDataManager.cs deleted file mode 100644 index 4149a8b..0000000 --- a/Sources/EntityFrameworkLib/DbDataManager.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Model; -using Shared; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EntityFrameworkLib -{ - public class DBDataManager : IDataManager - { - public IChampionsManager ChampionsManager => new DBChampionManager(); - - public ISkinsManager SkinsManager => throw new NotImplementedException(); - - public IRunesManager RunesManager=> throw new NotImplementedException(); - - public IRunePagesManager RunePagesManager=> throw new NotImplementedException(); - } -} diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 98e1c84..fa67e38 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAPI", "ConsoleAPI\Co EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DBWithStub", "DBWithStub", "{F0711CE6-C48B-4E96-8DE0-79BDBB4635D6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBEntitiesWithStub", "DBEntitiesWithStub\DBEntitiesWithStub.csproj", "{C9D40527-6F42-4FA9-8063-554D2A0E9161}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBEntitiesWithStub", "DBEntitiesWithStub\DBEntitiesWithStub.csproj", "{C9D40527-6F42-4FA9-8063-554D2A0E9161}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBDataManager", "DBDataManager\DBDataManager.csproj", "{9596049A-FCBB-4F61-9E48-D3086EAEFF85}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -77,6 +79,10 @@ Global {C9D40527-6F42-4FA9-8063-554D2A0E9161}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9D40527-6F42-4FA9-8063-554D2A0E9161}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9D40527-6F42-4FA9-8063-554D2A0E9161}.Release|Any CPU.Build.0 = Release|Any CPU + {9596049A-FCBB-4F61-9E48-D3086EAEFF85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9596049A-FCBB-4F61-9E48-D3086EAEFF85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9596049A-FCBB-4F61-9E48-D3086EAEFF85}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9596049A-FCBB-4F61-9E48-D3086EAEFF85}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Sources/Model/Champion.cs b/Sources/Model/Champion.cs index 122f595..7151d1f 100644 --- a/Sources/Model/Champion.cs +++ b/Sources/Model/Champion.cs @@ -66,6 +66,12 @@ public class Champion : IEquatable Class = @class; } + public Champion(string name, string bio) + { + this.name = name; + this.bio = bio; + } + public ReadOnlyCollection Skins { get; private set; } private List skins = new (); diff --git a/Sources/Web_Api/Controllers/ChampionsController.cs b/Sources/Web_Api/Controllers/ChampionsController.cs index f911511..2cb8936 100644 --- a/Sources/Web_Api/Controllers/ChampionsController.cs +++ b/Sources/Web_Api/Controllers/ChampionsController.cs @@ -35,7 +35,7 @@ namespace Web_Api.Controllers } [HttpPost("addChampion")] - public async Task AddChampion(ChampionDTO champion) + public async Task AddChampion([FromBody] ChampionDTO champion) { var newChampion = champion.toModel(); await ChampionsManager.AddItem(newChampion); @@ -48,7 +48,7 @@ namespace Web_Api.Controllers } [HttpPut("updateChampion")] - public async Task UpdateChampion(string name, ChampionDTO champion) + public async Task UpdateChampion(string name, [FromBody] ChampionDTO champion) { var championSelected = await ChampionsManager.GetItemsByName(name, 0, await ChampionsManager.GetNbItemsByName(name), null); var existingChampion = championSelected.FirstOrDefault();