From ac8f0dfff1401b6065b23e877745bf3356cbddec Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Tue, 14 Mar 2023 16:57:30 +0100 Subject: [PATCH] Adding Characteristics to Champion tests --- .../Sources/Business/DbData.Champions.cs | 2 +- .../Sources/EntityMappers/ChampionMapper.cs | 7 +++- .../EntityMappers/CharacteristicMapper.cs | 33 +++++++++++++++++++ .../Sources/TestEF/TestChampions.cs | 20 ++++++++++- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 EntityFramework_LoL/Sources/EntityMappers/CharacteristicMapper.cs diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index 7e54d83..9155269 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -58,7 +58,7 @@ namespace Business public async Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Characteristics").GetItemsWithFilterAndOrdering( c => c.Characteristics.Any(ch => ch.Name.Equals(charName)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); diff --git a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs index 9dabc8e..861d718 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs @@ -21,6 +21,8 @@ namespace EntityMapper Image = new() { Base64 = item.Image.Base64 }, }; championEntity.Skills = item.Skills.Select(x => x.ToEntity(championEntity, context)).ToList(); + championEntity.Characteristics = item.Characteristics.Select(x => x.ToEntity(championEntity, context)).ToList(); + } return championEntity; @@ -28,7 +30,10 @@ namespace EntityMapper public static Champion ToModel(this ChampionEntity entity) { - return new(entity.Name, entity.Class, entity.Icon, "", entity.Bio); + var champion = new Champion(entity.Name, entity.Class, entity.Icon, "", entity.Bio); + if(entity.Skills!=null) foreach(var s in entity.Skills){champion.AddSkill(s.ToModel());} + if (entity.Characteristics != null) foreach (var c in entity.Characteristics){champion.AddCharacteristics(c.ToModel()); } + return champion; } } diff --git a/EntityFramework_LoL/Sources/EntityMappers/CharacteristicMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/CharacteristicMapper.cs new file mode 100644 index 0000000..04dbc13 --- /dev/null +++ b/EntityFramework_LoL/Sources/EntityMappers/CharacteristicMapper.cs @@ -0,0 +1,33 @@ +using Entities; +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityMappers +{ + public static class CharacteristicMapper + { + public static CharacteristicEntity ToEntity(this KeyValuePair item, ChampionEntity champion, LolDbContext context) + { + var characteristicEntity = context.characteristics.Find(item.Key, champion.Name); + if (characteristicEntity == null) + { + return new() + { + Name = item.Key, + Value = item.Value, + ChampionForeignKey = champion.Name + }; + } + return characteristicEntity; + } + + + public static Tuple ToModel(this CharacteristicEntity entity) + => new(entity.Name, entity.Value); + } +} + diff --git a/EntityFramework_LoL/Sources/TestEF/TestChampions.cs b/EntityFramework_LoL/Sources/TestEF/TestChampions.cs index 3e73a07..cd19d1d 100644 --- a/EntityFramework_LoL/Sources/TestEF/TestChampions.cs +++ b/EntityFramework_LoL/Sources/TestEF/TestChampions.cs @@ -30,12 +30,25 @@ namespace TestEF Champion batman = new("Batman", ChampionClass.Assassin, "icon_1", "image_1", "L'ombre de la nuit"); batman.AddSkill(new("Bat-signal", SkillType.Basic, "Envoie le signal")); - + batman.AddCharacteristics(new[] { + Tuple.Create("Force", 150), + Tuple.Create("Agilité", 500) + }); Champion endeavor = new("Endeavor", ChampionClass.Tank, "icon_2", "image_2", "Feu brûlant énernel"); endeavor.AddSkill(new("Final flames", SkillType.Ultimate, "Dernière flamme d'un héro")); + endeavor.AddCharacteristics(new[] { + Tuple.Create("Force", 200), + Tuple.Create("Défense", 300), + Tuple.Create("Alter", 800) + }); + Champion escanor = new("Escanor", ChampionClass.Fighter, "icon_3", "image_3", "1, 2, 3, Soleil"); escanor.AddSkill(new("Croissance solaire", SkillType.Passive, "Le soleil rends plus fort !")); + escanor.AddCharacteristics(new[] { + Tuple.Create("Force", 500), + Tuple.Create("Défense", 500) + }); await manager.AddItem(batman); await manager.AddItem(endeavor); @@ -61,6 +74,11 @@ namespace TestEF items = await manager.GetItemsBySkill(new Skill("Final flames", SkillType.Ultimate, "Dernière flamme d'un héro"), 0, nbItems); Assert.Equal("Endeavor", items.First().Name); + + items = await manager.GetItemsByCharacteristic("Alter", 0, nbItems); + Assert.Equal("Endeavor", items.First().Name); + + Assert.Equal(2, await manager.GetNbItemsByCharacteristic("Défense")); } }