From cf320b341b2313c97c24f563467b549b47e0a8b7 Mon Sep 17 00:00:00 2001 From: Emre Date: Sun, 26 Mar 2023 01:07:10 +0100 Subject: [PATCH] DbManager and MAUI Done :white_check_mark: --- .../Controllers/v1/ChampionsController.cs | 8 +- .../Sources/DbManager/DbManager.Champions.cs | 31 +++-- .../Sources/DbManager/DbManager.RunePages.cs | 107 ++++++++++-------- .../Sources/DbManager/DbManager.Runes.cs | 93 +++++++++------ .../Sources/DbManager/DbManager.Skins.cs | 86 ++++++++------ .../DbManager/Mapper/ChampionMapper.cs | 14 ++- .../DbManager/Mapper/CharacteristicMapper.cs | 27 +++++ .../Sources/DbManager/Mapper/RuneMapper.cs | 2 +- .../Sources/DbManager/Mapper/SkillMapper.cs | 20 ++-- .../Sources/DbManager/Mapper/SkinMapper.cs | 23 ++-- .../Sources/MyFlib/DataBase.db | Bin 131072 -> 131072 bytes .../Sources/MyFlib/LolDbContext.cs | 4 + ...=> 20230325231552_myMigration.Designer.cs} | 2 +- ...ation.cs => 20230325231552_myMigration.cs} | 0 14 files changed, 264 insertions(+), 153 deletions(-) create mode 100644 src/EntityFramework_LoL/Sources/DbManager/Mapper/CharacteristicMapper.cs rename src/EntityFramework_LoL/Sources/MyFlib/Migrations/{20230325153221_myMigration.Designer.cs => 20230325231552_myMigration.Designer.cs} (99%) rename src/EntityFramework_LoL/Sources/MyFlib/Migrations/{20230325153221_myMigration.cs => 20230325231552_myMigration.cs} (100%) diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs index f822be1..2125c70 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs @@ -61,9 +61,9 @@ namespace ApiLol.Controllers.v1 { _logger.LogInformation("method {Action} - CHAMPION - V1.0 call with {name} and {item}", nameof(Put), name, champion); - var dtos = (await _manager.ChampionsMgr.GetItemByName(name, 0, await _manager.ChampionsMgr.GetNbItems())); + var champs = (await _manager.ChampionsMgr.GetItemByName(name, 0, await _manager.ChampionsMgr.GetNbItems())); - return Ok((await _manager.ChampionsMgr.UpdateItem(dtos.First(), champion.ToModel())).ToDto()); + return Ok((await _manager.ChampionsMgr.UpdateItem(champs.First(), champion.ToModel())).ToDto()); } @@ -87,9 +87,9 @@ namespace ApiLol.Controllers.v1 { _logger.LogInformation("method {Action} - CHAMPION - V1.0 call with {name}", nameof(Delete), name); - var dtos = (await _manager.ChampionsMgr.GetItemByName(name, 0, await _manager.ChampionsMgr.GetNbItems())); + var champs = (await _manager.ChampionsMgr.GetItemByName(name, 0, await _manager.ChampionsMgr.GetNbItems())); - await _manager.ChampionsMgr.DeleteItem(dtos.First()); + await _manager.ChampionsMgr.DeleteItem(champs.First()); return NoContent(); } diff --git a/src/EntityFramework_LoL/Sources/DbManager/DbManager.Champions.cs b/src/EntityFramework_LoL/Sources/DbManager/DbManager.Champions.cs index 71e5bc7..005a45b 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/DbManager.Champions.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/DbManager.Champions.cs @@ -22,7 +22,7 @@ namespace DbLib public async Task DeleteItem(Champion? item) { - var toDelete = parent.DbContext.Champions.Find(item.Name); + var toDelete = parent.DbContext.Champions.Where(c => c.Name == item.Name).First(); if (toDelete != null) { parent.DbContext.Champions.Remove(toDelete); @@ -38,12 +38,12 @@ namespace DbLib private Func filterByName = (champ, substring) => champ.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.GetItemsWithFilterAndOrdering(champ => filterByName(champ.ToModel(), substring), index, count, orderingPropertyName, descending) + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering(champ => filterByName(champ.ToModel(), substring), index, count, orderingPropertyName, descending) .Select(c => c.ToModel()); public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.GetItemsWithFilterAndOrdering( + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering( c => true, index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -51,34 +51,34 @@ namespace DbLib private Func filterByCharacteristic = (champ, charName) => champ.Characteristics.Keys.Any(k => k.Contains(charName, StringComparison.InvariantCultureIgnoreCase)); public async Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.Include(c => c.Characteristics).GetItemsWithFilterAndOrdering( + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering( champ => filterByCharacteristic(champ.ToModel(), charName), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); private Func filterByClass = (champ, championClass) => champ.Class == championClass; public async Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.GetItemsWithFilterAndOrdering( + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering( champ => filterByClass(champ.ToModel(), championClass), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.GetItemsWithFilterAndOrdering(champ => filterByNameContains(champ.ToModel(), substring), index, count, orderingPropertyName, descending) + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering(champ => filterByNameContains(champ.ToModel(), substring), index, count, orderingPropertyName, descending) .Select(c => c.ToModel()); public async Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.Include("RunePages").GetItemsWithFilterAndOrdering( + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering( c => c.RunePages.Any(rp => rp.Equals(runePage.ToEntity(parent.DbContext))), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); public async Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.GetItemsWithFilterAndOrdering(champ => filterBySkill(champ.ToModel(), skill), index, count, orderingPropertyName, descending) + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering(champ => filterBySkill(champ.ToModel(), skill), index, count, orderingPropertyName, descending) .Select(c => c.ToModel()); public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) - => parent.DbContext.Champions.GetItemsWithFilterAndOrdering(champ => filterBySkillSubstring(champ.ToModel(), skill), index, count, orderingPropertyName, descending) + => parent.DbContext.Champions.Include(c => c.Skills).Include(c => c.Characteristics).Include(c => c.Skins).Include(c => c.Image).GetItemsWithFilterAndOrdering(champ => filterBySkillSubstring(champ.ToModel(), skill), index, count, orderingPropertyName, descending) .Select(c => c.ToModel()); public async Task GetNbItems() @@ -112,10 +112,17 @@ namespace DbLib public async Task UpdateItem(Champion? oldItem, Champion? newItem) { - var toUpdate = parent.DbContext.Champions.Find(oldItem.Name); - toUpdate = newItem.ToEntity(parent.DbContext); + var toUpdate = parent.DbContext.Champions.FirstOrDefault(champ => champ.Name == oldItem.Name); + var newEntity = newItem.ToEntity(parent.DbContext); + toUpdate.Bio = newEntity.Bio; + toUpdate.Class = newEntity.Class; + toUpdate.Icon = newEntity.Icon; + toUpdate.Image = newEntity.Image; + toUpdate.Skins = newEntity.Skins; + toUpdate.Skills = newEntity.Skills; + toUpdate.Characteristics = newEntity.Characteristics; parent.DbContext.SaveChanges(); - return newItem; + return toUpdate?.ToModel(); } } } diff --git a/src/EntityFramework_LoL/Sources/DbManager/DbManager.RunePages.cs b/src/EntityFramework_LoL/Sources/DbManager/DbManager.RunePages.cs index a05db45..efdfc20 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/DbManager.RunePages.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/DbManager.RunePages.cs @@ -1,9 +1,6 @@ -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using DbManager.Mapper; +using Microsoft.EntityFrameworkCore; +using Model; namespace DbLib { @@ -16,64 +13,80 @@ namespace DbLib public RunePagesManager(DbManager parent) => this.parent = parent; - public Task AddItem(RunePage? item) + public async Task AddItem(RunePage? item) { - throw new NotImplementedException(); + var RunePage = await parent.DbContext.RunePages.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); + return RunePage.Entity.ToModel(parent.DbContext); } - public Task DeleteItem(RunePage? item) + public async Task DeleteItem(RunePage? item) { - throw new NotImplementedException(); + var toDelete = parent.DbContext.RunePages.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.RunePages.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; + } + return false; } - public Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByName + = (rp, substring) => rp.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByNameContains + = (rp, substring) => rp.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); - public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByRune + = (rp, rune) => rune != null && rp.Runes.Values.Contains(rune!); - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering( + rp => filterByName(rp.ToModel(parent.DbContext), substring), + index, count, orderingPropertyName, descending).Select(c => c.ToModel(parent.DbContext)); - public Task> GetItemsByRune(Model.Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering( + rp => true, + index, count, orderingPropertyName, descending).Select(c => c.ToModel(parent.DbContext)); + + public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering( + rp => rp.Champions.Any(c => c.Name.Equals(champion.Name)), + index, count, + orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); + + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering( + rp => filterByNameContains(rp.ToModel(parent.DbContext), substring), + index, count, orderingPropertyName, descending).Select(c => c.ToModel(parent.DbContext)); + + public async Task> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.RunePages.Include(rp => rp.Champions).Include(rp => rp.DictionaryCategoryRunes).GetItemsWithFilterAndOrdering( + rp => filterByRune(rp.ToModel(parent.DbContext), rune), + index, count, orderingPropertyName, descending).Select(c => c.ToModel(parent.DbContext)); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => parent.DbContext.RunePages.GetNbItemsWithFilter( + rp => true); - public Task GetNbItemsByChampion(Champion? champion) - { - throw new NotImplementedException(); - } + public async Task GetNbItemsByChampion(Champion? champion) + => parent.DbContext.RunePages.Where(rp => rp.Champions.Any(c => c.Name.Equals(champion.Name))).Count(); - public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } - public Task GetNbItemsByRune(Model.Rune? rune) - { - throw new NotImplementedException(); - } + public async Task GetNbItemsByName(string substring) + => parent.DbContext.RunePages.Where(rp => rp.Name.Contains(substring)).Count(); + + public async Task GetNbItemsByRune(Rune? rune) + => parent.DbContext.RunePages.Where(rp => rp.DictionaryCategoryRunes.Any(r => r.RuneName.Equals(rune.Name))).Count(); - public Task UpdateItem(RunePage? oldItem, RunePage? newItem) + public async Task UpdateItem(RunePage? oldItem, RunePage? newItem) { - throw new NotImplementedException(); + var toUpdate = parent.DbContext.RunePages.Find(oldItem.Name); + toUpdate.DictionaryCategoryRunes = newItem.ToEntity(parent.DbContext).DictionaryCategoryRunes; + parent.DbContext.SaveChanges(); + return toUpdate.ToModel(parent.DbContext); } } } diff --git a/src/EntityFramework_LoL/Sources/DbManager/DbManager.Runes.cs b/src/EntityFramework_LoL/Sources/DbManager/DbManager.Runes.cs index 4fcb8fe..a40a901 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/DbManager.Runes.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/DbManager.Runes.cs @@ -1,9 +1,5 @@ -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using DbManager.Mapper; +using Model; namespace DbLib { @@ -15,54 +11,77 @@ namespace DbLib public RunesManager(DbManager parent) => this.parent = parent; - public Task AddItem(Model.Rune? item) + public async Task AddItem(Rune? item) { - throw new NotImplementedException(); + var rune = await parent.DbContext.Runes.AddAsync(item.ToEntity()); + parent.DbContext.SaveChanges(); + return rune.Entity.ToModel(); } - public Task DeleteItem(Model.Rune? item) + public async Task DeleteItem(Rune? item) { - throw new NotImplementedException(); + var toDelete = parent.DbContext.Runes.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.Runes.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; + } + return false; } - public Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByRuneFamily + = (rune, family) => rune.Family == family; - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByName + = (rune, substring) => rune.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); - public Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByNameContains + = (rune, substring) => rune.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Runes.GetItemsWithFilterAndOrdering( + rune => filterByName(rune.ToModel(), substring), + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Runes.GetItemsWithFilterAndOrdering( + r => true, + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); + + public async Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Runes.GetItemsWithFilterAndOrdering( + rune => filterByRuneFamily(rune.ToModel(), family), + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); + + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Runes.GetItemsWithFilterAndOrdering( + rune => filterByNameContains(rune.ToModel(), substring), + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => parent.DbContext.Runes.GetNbItemsWithFilter( + rune => true); public Task GetNbItemsByFamily(RuneFamily family) - { - throw new NotImplementedException(); - } + => parent.DbContext.Runes.GetNbItemsWithFilter( + rune => filterByRuneFamily(rune.ToModel(), family)); public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } + => parent.DbContext.Runes.GetNbItemsWithFilter( + rune => filterByName(rune.ToModel(), substring)); - public Task UpdateItem(Model.Rune? oldItem, Model.Rune? newItem) + public async Task UpdateItem(Rune? oldItem, Rune? newItem) { - throw new NotImplementedException(); + var toUpdate = parent.DbContext.Runes.Find(oldItem.Name); + var newEntity = newItem.ToEntity(); + toUpdate.Description = newEntity.Description; + toUpdate.Icon = newEntity.Icon; + toUpdate.Family = newEntity.Family; + toUpdate.Image = newEntity.Image; + + parent.DbContext.SaveChanges(); + return toUpdate.ToModel(); } } diff --git a/src/EntityFramework_LoL/Sources/DbManager/DbManager.Skins.cs b/src/EntityFramework_LoL/Sources/DbManager/DbManager.Skins.cs index 45c9bf3..301a7e7 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/DbManager.Skins.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/DbManager.Skins.cs @@ -1,4 +1,6 @@ -using Model; +using DbManager.Mapper; +using Microsoft.EntityFrameworkCore; +using Model; using System; using System.Collections.Generic; using System.Linq; @@ -16,54 +18,74 @@ namespace DbLib public SkinsManager(DbManager parent) => this.parent = parent; - public Task AddItem(Skin? item) + public async Task AddItem(Skin? item) { - throw new NotImplementedException(); + var skin = await parent.DbContext.Skins.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); + return skin.Entity.ToModel(); } - public Task DeleteItem(Skin? item) + public async Task DeleteItem(Skin? item) { - throw new NotImplementedException(); + var toDelete = parent.DbContext.Skins.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.Skins.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; + } + return false; } - public Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByChampion = (skin, champion) => champion != null && skin.Champion.Equals(champion!); - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByName = (skin, substring) => skin.Name.Equals(substring, StringComparison.InvariantCultureIgnoreCase); - public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + private static Func filterByNameContains = (skin, substring) => skin.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public async Task> GetItemByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Skins.Include(s => s.Champion).Include(s => s.Image).GetItemsWithFilterAndOrdering( + skin => filterByName(skin.ToModel(), substring), + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Skins.Include(s => s.Champion).Include(s => s.Image).GetItemsWithFilterAndOrdering( + skin => true, + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); + + public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Skins.Include(s => s.Champion).Include(s => s.Image).GetItemsWithFilterAndOrdering( + skin => filterByChampion(skin.ToModel(), champion), + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); + + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + => parent.DbContext.Skins.Include(s => s.Champion).Include(s => s.Image).GetItemsWithFilterAndOrdering( + skin => filterByNameContains(skin.ToModel(), substring), + index, count, orderingPropertyName, descending).Select(c => c.ToModel()); public Task GetNbItems() - { - throw new NotImplementedException(); - } + => parent.DbContext.Skins.GetNbItemsWithFilter( + c => true); public Task GetNbItemsByChampion(Champion? champion) - { - throw new NotImplementedException(); - } + => parent.DbContext.Skins.GetNbItemsWithFilter( + skin => filterByChampion(skin.ToModel(), champion)); public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } + => parent.DbContext.Skins.GetNbItemsWithFilter( + skin => filterByName(skin.ToModel(), substring)); - public Task UpdateItem(Skin? oldItem, Skin? newItem) + public async Task UpdateItem(Skin? oldItem, Skin? newItem) { - throw new NotImplementedException(); + var toUpdate = parent.DbContext.Skins.Find(oldItem.Name); + var newEntity = newItem.ToEntity(parent.DbContext); + toUpdate.Description = newEntity.Description; + toUpdate.Icon = newEntity.Icon; + toUpdate.Price = newEntity.Price; + toUpdate.Champion = newEntity.Champion; + toUpdate.Image = newEntity.Image; + parent.DbContext.SaveChanges(); + return toUpdate.ToModel(); } } diff --git a/src/EntityFramework_LoL/Sources/DbManager/Mapper/ChampionMapper.cs b/src/EntityFramework_LoL/Sources/DbManager/Mapper/ChampionMapper.cs index 66adf27..71755f5 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/Mapper/ChampionMapper.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/Mapper/ChampionMapper.cs @@ -8,14 +8,21 @@ namespace DbManager.Mapper { public static Champion ToModel(this ChampionEntity championEntity) { - Champion champion = new (championEntity.Name, championEntity.Class.ToModel(), championEntity.Icon, "", championEntity.Bio); + Champion champion = new (championEntity.Name, championEntity.Class.ToModel(), championEntity.Icon, championEntity.Image.Base64, championEntity.Bio); foreach (var skill in championEntity.Skills) { champion.AddSkill(skill.ToModel()); } foreach (var skin in championEntity.Skins) { - champion.AddSkin(skin.ToModel()); + champion.AddSkin(new Skin(skin.Name, champion, skin.Price, skin.Icon, skin.Image.Base64, skin.Description)); + } + if (championEntity.Characteristics != null) + { + foreach (var c in championEntity.Characteristics) + { + champion.AddCharacteristics(c.ToModel()); + } } return champion; } @@ -31,12 +38,13 @@ namespace DbManager.Mapper }; foreach (var skill in champion.Skills) { - champ.Skills.Add(skill.ToEntity(champ)); + champ.Skills.Add(skill.ToEntity(champ, context)); } foreach (var skin in champion.Skins) { champ.Skins.Add(skin.ToEntity(context)); } + champ.Characteristics = champion.Characteristics.Select(x => x.ToEntity(champ, context)).ToList(); return champ; } } diff --git a/src/EntityFramework_LoL/Sources/DbManager/Mapper/CharacteristicMapper.cs b/src/EntityFramework_LoL/Sources/DbManager/Mapper/CharacteristicMapper.cs new file mode 100644 index 0000000..b99ee71 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/DbManager/Mapper/CharacteristicMapper.cs @@ -0,0 +1,27 @@ +using MyFlib.Entities; +using MyFlib; + +namespace DbManager.Mapper +{ + public static class CharacteristicMapper + { + public static CharacteristicEntity ToEntity(this KeyValuePair item, ChampionEntity champion, LolDbContext context) + { + var characteristicEntity = context.Characteristic.Find(item.Key, champion.Id); + if (characteristicEntity == null) + { + return new() + { + Name = item.Key, + Value = item.Value, + ChampionForeignKey = champion.Id + }; + } + return characteristicEntity; + } + + + public static Tuple ToModel(this CharacteristicEntity entity) + => new(entity.Name, entity.Value); + } +} diff --git a/src/EntityFramework_LoL/Sources/DbManager/Mapper/RuneMapper.cs b/src/EntityFramework_LoL/Sources/DbManager/Mapper/RuneMapper.cs index 788ce1e..744c73f 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/Mapper/RuneMapper.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/Mapper/RuneMapper.cs @@ -6,7 +6,7 @@ namespace DbManager.Mapper { public static class RuneMapper { - public static Rune ToModel(this RuneEntity rune) => new(rune.Name, rune.Family.ToModel(), rune.Icon, rune.Image.Base64, rune.Description); + public static Rune ToModel(this RuneEntity rune) => new(rune.Name, rune.Family.ToModel(), rune.Icon, "", rune.Description); public static RuneEntity ToEntity(this Rune rune) => new() { diff --git a/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkillMapper.cs b/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkillMapper.cs index 9e8b636..f2c185c 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkillMapper.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkillMapper.cs @@ -8,16 +8,20 @@ namespace DbManager.Mapper { public static Skill ToModel(this SkillEntity skillEntity) => new(skillEntity.Name, skillEntity.Type.ToModel(), skillEntity.Description); - - public static SkillEntity ToEntity(this Skill skill, ChampionEntity championEntity) + public static SkillEntity ToEntity(this Skill skill, ChampionEntity championEntity, LolDbContext context) { - return new() + var skillSearch = context.Skills.Find(skill.Name); + if (skillSearch == null) { - Name = skill.Name, - Description = skill.Description, - Type = skill.Type.ToEntity(), - Champion = championEntity - }; + return new() + { + Name = skill.Name, + Description = skill.Description, + Type = skill.Type.ToEntity(), + Champion = championEntity + }; + } + throw new Exception("Skill was already exist"); } } diff --git a/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkinMapper.cs b/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkinMapper.cs index 391891f..5edde49 100644 --- a/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkinMapper.cs +++ b/src/EntityFramework_LoL/Sources/DbManager/Mapper/SkinMapper.cs @@ -9,14 +9,21 @@ namespace DbManager.Mapper => new(skinEntity.Name, skinEntity.Champion.ToModel(), skinEntity.Price, skinEntity.Icon, skinEntity.Image.Base64, skinEntity.Description); public static SkinEntity ToEntity(this Skin skin, LolDbContext context) - => new() + { + var skinSearch = context.Skins.Find(skin.Name); + if(skinSearch == null) { - Name = skin.Name, - Description = skin.Description, - Icon = skin.Icon, - Price = skin.Price, - Champion = context.Champions.Find(skin.Champion.Name), - Image = skin.Image.ToEntity() - }; + return new() + { + Name = skin.Name, + Description = skin.Description, + Icon = skin.Icon, + Price = skin.Price, + Champion = context.Champions.FirstOrDefault(c => c.Name == skin.Champion.Name), + Image = skin.Image.ToEntity() + }; + } + throw new Exception("Skin was already exist"); + } } } diff --git a/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db b/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db index 4436f2fabccb9a22c34d8fa378e0b14f81948442..d3b2cb4df3b4923712556f2c8e0cc56361cfc791 100644 GIT binary patch delta 1285 zcmb7DU1$_n6rMA8|7T~;ZlXyiG21a%+|*2%>})bSt*Db-qu3_J#-)8Q{?G^6hiXg$ zO&%mcWaC32hN~U=5(JU>AS`&H3qjN;iC8H9(LS}-hrYD0Rz$Ep+s#U>7Sapfz2~0u zan8Bi^UcmiW@jVk+LH$)NiqnlOD-*R+vMyy+!iDk;EP(|orSTj@U#E%99Ro07ode7 z0v(#g6uc+H2ENGJAt{#GG(Z#22DbrHl0Pd6UK3>6T+$WOO2l#~Qf1q@DuI1c1in4eM(*Zszwil!o`iM3Pt~9 z9JYwg#V3#mp(52grx#>VA?7p9KX@0?e)@ap;s+~NVobXw;hgqTyG7A!+K%lkr0WsA zSg*KtU(cJx%NrqxFV+ z85t}8`~k`DU4pTSj@5G$oi!MQHbTQ+vo{c$2?n8dPZ4T48fYRrNiYa&`E-4?gkg1~ oU-<=!Z=VJly>!*S3HK88Yh?+`wRsVncTHi}dgq)~DgA!qZ#W`1Q~&?~ delta 1072 zcmb7DOH30{6z!d9J1y7e*yvA>hItghV&GGSP%U&{t?p3 z^Q;xh$!m5LpaQ!zr@=&GnneIJ)^g`TAYrbX0fb-bMKF?g`ZxfOI}G(uhQnMH88S=& z81OHn4RqMTPlKMM`D={68u9eYwMI4OOkNVPy)`VTYt?M6F@-ssmzd>5%Lek9}e8@ zw=--M;qj?Pvfk-5(QcC}^ciK)B<+?!b~_t|Qkp5tTX18LzDS3zqd1XAA+Di&8$~sR zNMR|8bI<1L*8G~QRZ*Dox#M&-7wr*^1v*{_QZgBc%*Kqw{AL^B@Aqsp*h2W*xLD5d zvnrHEmxWznLAb#G4rd_f^-EozHn-&R1ZBzFB|9Xy)8%pb z0&T%yYv_og;(IAvT-9>XN}h)(ez4z28m4wY;Dnt#t*BerA*Pv+LUWGVQikB>)*ym! zK3~!?!hs4s77pm2{1C{&vKDj<+Ek&hXp@*@?~$@NO_lg%CH&&AT4m))3|7IS^z;>J zR++yA1<`6M^jBa5w`-dI5pFiz5C5m!Z2ArawZJKnI9Rf596m8%ReZxBD$j`wSJxlFW@YVn*>2bIY=v5Nb vw(??bkqa3pzh-&uh~H9n2KP=J1>-oePKRie}Z@uwVBFaQ_HS diff --git a/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs b/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs index 4cfd544..2a3ebb1 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs @@ -96,6 +96,10 @@ namespace MyFlib ); //CharacteristicEntity + modelBuilder.Entity() + .HasOne(m => m.Champion) + .WithMany(a => a.Characteristics) + .HasForeignKey("ChampionForeignKey"); modelBuilder.Entity().HasKey(c => new { c.Name, c.ChampionForeignKey }); modelBuilder.Entity().HasData( diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325153221_myMigration.Designer.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325231552_myMigration.Designer.cs similarity index 99% rename from src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325153221_myMigration.Designer.cs rename to src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325231552_myMigration.Designer.cs index 5845de8..3cd96b3 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325153221_myMigration.Designer.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325231552_myMigration.Designer.cs @@ -11,7 +11,7 @@ using MyFlib; namespace MyFlib.Migrations { [DbContext(typeof(LolDbContext))] - [Migration("20230325153221_myMigration")] + [Migration("20230325231552_myMigration")] partial class myMigration { /// diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325153221_myMigration.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325231552_myMigration.cs similarity index 100% rename from src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325153221_myMigration.cs rename to src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230325231552_myMigration.cs