From 56636621b6402d6024bc9cf4cde0bd60d782d11c Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Wed, 22 Feb 2023 11:21:46 +0100 Subject: [PATCH] Fin du manager pour champion --- .../Sources/ApiLol/ApiLol.csproj | 19 ++++ .../ApiLol/Controllers/ChampionController.cs | 58 +++++----- .../Sources/ApiLol/{ => DTO}/ChampionDTO.cs | 3 + .../Sources/ApiLol/Mapping/ChampionMapper.cs | 41 +++++++ .../Sources/ConsoleApi/Program.cs | 2 +- .../Sources/ConsoleDb/Program.cs | 12 ++- .../Sources/ConsoleDb/oiseaux.db | Bin 20480 -> 20480 bytes .../Sources/ConsoleDb/oiseaux.db-shm | Bin 0 -> 32768 bytes .../Sources/ConsoleDb/oiseaux.db-wal | 0 .../Sources/DataManagers/ChampChanger.cs | 33 ++++-- .../Sources/DataManagers/DbChampionManager.cs | 101 ++++++++++++++---- .../Sources/DataManagers/DbDataManager.cs | 20 +--- .../Sources/EFLib/ChampionEntity.cs | 4 +- 13 files changed, 205 insertions(+), 88 deletions(-) rename EntityFramework_LoL/Sources/ApiLol/{ => DTO}/ChampionDTO.cs (77%) create mode 100644 EntityFramework_LoL/Sources/ApiLol/Mapping/ChampionMapper.cs create mode 100644 EntityFramework_LoL/Sources/ConsoleDb/oiseaux.db-shm create mode 100644 EntityFramework_LoL/Sources/ConsoleDb/oiseaux.db-wal diff --git a/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj b/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj index 8cb33cf..f4484e6 100644 --- a/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj +++ b/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj @@ -10,4 +10,23 @@ + + + + + + + + + + + + + + + + + + + diff --git a/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionController.cs b/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionController.cs index ae2d93c..ff8f407 100644 --- a/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionController.cs +++ b/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionController.cs @@ -1,4 +1,5 @@ using System; +using EFLib; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -12,61 +13,52 @@ namespace ApiLol.Controllers public class ChampionController : ControllerBase { private readonly ILogger _logger; + private List champions = new List(); public ChampionController(ILogger logger) { _logger = logger; - } - - - // GET: api/champion - [HttpGet] - public ActionResult> Get() - { - List champions = new List(); - champions.Add(new ChampionDTO + champions.Add(new ChampionEntity { Id = 1, Name = "Aurel", Bio = "Trop joli", - Icon = "Moi" + Icon = "Moi", + champClass = Model.ChampionClass.Tank }); - champions.Add(new ChampionDTO + champions.Add(new ChampionEntity { Id = 2, Name = "Mathilde", Bio = "Elle est reloue", - Icon = "Macheval" + Icon = "Macheval", + champClass = Model.ChampionClass.Assassin }); - return Ok(champions); + } + + + // GET: api/champion + [HttpGet] + public ActionResult> Get() + { + IEnumerable champs = new List(); + foreach(ChampionEntity arg in champions) + { + champs.Append(arg.ToDto()); + } + return Ok(champs); } // GET api/values/5 [HttpGet("{id}")] public ActionResult Get(int id) { - List champions = new List(); - champions.Add(new ChampionDTO - { - Id = 1, - Name = "Aurel", - Bio = "Trop joli", - Icon = "Moi" - }); - champions.Add(new ChampionDTO - { - Id = 2, - Name = "Mathilde", - Bio = "Elle est reloue", - Icon = "Macheval" - }); - - ChampionDTO? champion = champions.SingleOrDefault((ChampionDTO arg) => arg.Id == id); - if (champion == null) + ChampionDTO? champion = champions.SingleOrDefault((ChampionEntity arg) => arg.Id == id)?.ToDto(); + if (champion != null) { - return BadRequest(); + return Ok(champion); } - return Ok(champion); + return BadRequest(); } // POST api/values diff --git a/EntityFramework_LoL/Sources/ApiLol/ChampionDTO.cs b/EntityFramework_LoL/Sources/ApiLol/DTO/ChampionDTO.cs similarity index 77% rename from EntityFramework_LoL/Sources/ApiLol/ChampionDTO.cs rename to EntityFramework_LoL/Sources/ApiLol/DTO/ChampionDTO.cs index 0038870..28fd292 100644 --- a/EntityFramework_LoL/Sources/ApiLol/ChampionDTO.cs +++ b/EntityFramework_LoL/Sources/ApiLol/DTO/ChampionDTO.cs @@ -1,4 +1,5 @@ using System; +using Model; namespace ApiLol { public class ChampionDTO @@ -10,6 +11,8 @@ namespace ApiLol public string Icon { get; set; } public string Bio { get; set; } + + public ChampionClass champClass { get; set; } } } diff --git a/EntityFramework_LoL/Sources/ApiLol/Mapping/ChampionMapper.cs b/EntityFramework_LoL/Sources/ApiLol/Mapping/ChampionMapper.cs new file mode 100644 index 0000000..83f9382 --- /dev/null +++ b/EntityFramework_LoL/Sources/ApiLol/Mapping/ChampionMapper.cs @@ -0,0 +1,41 @@ +using System; +using EFLib; + +namespace ApiLol +{ + public static class ChampionMapper + { + public static ChampionDTO ToDto(this ChampionEntity champion) + { + if (champion == null) + { + throw new NullReferenceException(); + } + return new ChampionDTO + { + Name = champion.Name, + Bio = champion.Bio, + Icon = champion.Icon, + Id = champion.Id, + champClass = champion.champClass + }; + } + + public static ChampionEntity ToEntity(this ChampionDTO champion) + { + if (champion == null) + { + throw new NullReferenceException(); + } + return new ChampionEntity + { + Name = champion.Name, + Bio = champion.Bio, + Icon = champion.Icon, + Id = champion.Id, + champClass = champion.champClass + }; + } + } +} + diff --git a/EntityFramework_LoL/Sources/ConsoleApi/Program.cs b/EntityFramework_LoL/Sources/ConsoleApi/Program.cs index 341d42d..f56e562 100644 --- a/EntityFramework_LoL/Sources/ConsoleApi/Program.cs +++ b/EntityFramework_LoL/Sources/ConsoleApi/Program.cs @@ -5,7 +5,7 @@ using static System.Net.WebRequestMethods; HttpClient http = new HttpClient(); //http.BaseAddress = new Uri("https://localhost:7006/"); -//Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion")); +Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion")); Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion/1")); diff --git a/EntityFramework_LoL/Sources/ConsoleDb/Program.cs b/EntityFramework_LoL/Sources/ConsoleDb/Program.cs index 87658d6..0fc628b 100644 --- a/EntityFramework_LoL/Sources/ConsoleDb/Program.cs +++ b/EntityFramework_LoL/Sources/ConsoleDb/Program.cs @@ -4,8 +4,14 @@ using Model; Console.WriteLine("Hello, World!"); + using (var db = new DbDataManager(new EFLib.LolContext())) { - Champion champ = await db.Add(new Model.Champion("marche", Model.ChampionClass.Assassin, "ouii", "yes", "bio")); - Console.WriteLine(champ.Name); -} \ No newline at end of file + IEnumerable champions = await db.ChampionsMgr.GetItems(0, 10); + + foreach (Champion c in champions) + { + Console.WriteLine(c.Name); + } +} + diff --git a/EntityFramework_LoL/Sources/ConsoleDb/oiseaux.db b/EntityFramework_LoL/Sources/ConsoleDb/oiseaux.db index d0c9a2e3fd7b14c0e86d27f647d28464f48017d7..2665bcbb95e0b284fd7a022be2d3e55d8ebd36ad 100644 GIT binary patch delta 218 zcmZozz}T>WaRZ})ApbQ6R{lnI{&)Nb`5XDy^H1L_C=kJ4&p(|(Tvk+=(H-NsNdu8N`?Fks2~FagD6N4q8%)#n3)e06<}mz5a$==EKbZRElEz! o&(GlniZb$lWZ?g}SWaRZ|PCj$cmBmZj#{@0rY9Uky2FbFfUF-VFEbL1u#C1<4Omu6-rW#;pO eRefUM{{&VwF@S@Qhml#H(>WtCw;(e=uNVMVS diff --git a/EntityFramework_LoL/Sources/ConsoleDb/oiseaux.db-shm b/EntityFramework_LoL/Sources/ConsoleDb/oiseaux.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3 new ChampionEntity { - return new ChampionEntity + Name = champion.Name, + Bio = champion.Bio, + Icon = champion.Icon, + ChampClass = champion.Class, + //Characteristics = champion.Characteristics.Select(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value) + }; + + public static IEnumerable ToPocos(this IEnumerable champs) + { + List champions = new List(); + foreach (ChampionEntity c in champs) + { + champions.Add(c.ToPoco()); + } + return champions; + } + + public static IEnumerable toEntities(this IEnumerable champs) + { + List champions = new List(); + foreach (Champion c in champs) { - Name = champion.Name, - Bio = champion.Bio, - Icon = champion.Icon, - }; + champions.Add(c.ToEntity()); + } + return champions; } } } diff --git a/EntityFramework_LoL/Sources/DataManagers/DbChampionManager.cs b/EntityFramework_LoL/Sources/DataManagers/DbChampionManager.cs index 68e32f1..9b631af 100644 --- a/EntityFramework_LoL/Sources/DataManagers/DbChampionManager.cs +++ b/EntityFramework_LoL/Sources/DataManagers/DbChampionManager.cs @@ -14,19 +14,67 @@ namespace DataManagers Context = context; } - public Task AddItem(Champion? item) - { - throw new NotImplementedException(); + private IEnumerable SortChampions(IEnumerable champs, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + switch (orderingPropertyName?.ToLower()) + { + case "name": + champs = champs.OrderBy(arg => arg.Name).ToList(); + break; + case "bio": + champs = champs.OrderBy(arg => arg.Bio).ToList(); + break; + case "class": + champs = champs.OrderBy(arg => arg.Class).ToList(); + break; + default: + break; + + } + if (descending) + { + champs = champs.Reverse().ToList(); + } + if (index + count > champs.ToList().Count) return champs.ToList().GetRange(index, champs.ToList().Count - index); + return champs.ToList().GetRange(index, count); + } + + public async Task AddItem(Champion? item) + { + if (item != null) + { + await Context.Champions.AddAsync(item.ToEntity()); + await Context.SaveChangesAsync(); + return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Name == item.Name)?.ToPoco()); + } + return null; + } + + public async Task DeleteItem(Champion? item) + { + if (item != null) + { + ChampionEntity? entity = await Context.Champions.SingleOrDefaultAsync((ChampionEntity arg) => arg.Name == item.Name); + if (entity != null) + { + Context.Champions.Remove(entity); + int test = await Context.SaveChangesAsync(); + return test==1; + } + } + return false; + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + IEnumerable champs = await Task.Run(() => Context.Champions.ToList().ToPocos()); + return SortChampions(champs, index, count,orderingPropertyName, descending); } - public Task DeleteItem(Champion? item) + public async Task GetNbItems() { - throw new NotImplementedException(); - } + return await Task.FromResult(Context.Champions.Count()); - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); } public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -34,14 +82,16 @@ namespace DataManagers throw new NotImplementedException(); } - public Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IEnumerable champs = await Task.Run(() => Context.Champions.ToList().ToPocos().Where(arg => arg.Class == championClass)); + return SortChampions(champs, index, count, orderingPropertyName, descending); } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IEnumerable champs = await Task.Run(() => Context.Champions.ToList().ToPocos().Where(arg => arg.Name.Contains(substring))); + return SortChampions(champs, index, count, orderingPropertyName, descending); } public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -59,12 +109,6 @@ namespace DataManagers throw new NotImplementedException(); } - public async Task GetNbItems() - { - return await Task.Run(() => Context.Champions.Count()); - - } - public Task GetNbItemsByCharacteristic(string charName) { throw new NotImplementedException(); @@ -75,9 +119,9 @@ namespace DataManagers throw new NotImplementedException(); } - public Task GetNbItemsByName(string substring) + public async Task GetNbItemsByName(string substring) { - throw new NotImplementedException(); + return await Task.FromResult(Context.Champions.Where((arg) => arg.Name.Contains(substring)).Count()); } public Task GetNbItemsByRunePage(RunePage? runePage) @@ -95,9 +139,20 @@ namespace DataManagers throw new NotImplementedException(); } - public Task UpdateItem(Champion? oldItem, Champion? newItem) + public async Task UpdateItem(Champion? oldItem, Champion? newItem) { - throw new NotImplementedException(); + ChampionEntity? champ = await Context.Champions.SingleOrDefaultAsync((arg) => arg.Name == oldItem.Name); + if (champ != null) + { + champ.Bio = newItem.Bio; + champ.Name = newItem.Name; + champ.ChampClass = newItem.Class; + champ.Icon = newItem.Icon; + Context.Champions.Update(champ); + await Context.SaveChangesAsync(); + return champ.ToPoco(); + } + return null; } } } diff --git a/EntityFramework_LoL/Sources/DataManagers/DbDataManager.cs b/EntityFramework_LoL/Sources/DataManagers/DbDataManager.cs index 0b8ba8d..3534d9c 100644 --- a/EntityFramework_LoL/Sources/DataManagers/DbDataManager.cs +++ b/EntityFramework_LoL/Sources/DataManagers/DbDataManager.cs @@ -9,7 +9,7 @@ namespace DataManagers private LolContext Context { get; set; } - public IChampionsManager ChampionsMgr => throw new NotImplementedException(); + public IChampionsManager ChampionsMgr => new DbChampionManager(Context); public ISkinsManager SkinsMgr => throw new NotImplementedException(); @@ -23,24 +23,6 @@ namespace DataManagers Context = context; } - public async Task GetNumbersOfElement() - { - return await Task.Run(() => Context.Champions.Count()); - } - - public async Task GetById(int id) - { - return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Id == id).ToPoco()); - } - - public async Task Add(Champion champ) - { - ChampionEntity entity = champ.ToEntity(); - await Context.Champions.AddAsync(entity); - Context.SaveChanges(); - return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Name == champ.Name && arg.Bio == champ.Bio).ToPoco()); - } - public void Dispose() { Context.Dispose(); diff --git a/EntityFramework_LoL/Sources/EFLib/ChampionEntity.cs b/EntityFramework_LoL/Sources/EFLib/ChampionEntity.cs index 8b3b00d..829cc2e 100644 --- a/EntityFramework_LoL/Sources/EFLib/ChampionEntity.cs +++ b/EntityFramework_LoL/Sources/EFLib/ChampionEntity.cs @@ -14,8 +14,8 @@ namespace EFLib public string Bio { get; set; } - public ChampionClass champClass { get; set;} + public ChampionClass ChampClass { get; set;} - } + } }