From 0dc5acc2f0cb4897638bab3f85f13731b16b493c Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Wed, 22 Mar 2023 16:06:33 +0100 Subject: [PATCH 1/3] Adding includes --- .../Sources/Business/DbData.Champions.cs | 35 ++++++------------ .../Sources/Business/DbData.RunePages.cs | 37 +++++-------------- .../Sources/Business/DbData.Runes.cs | 32 ++++------------ .../Sources/Business/DbData.Skins.cs | 28 ++++---------- .../Sources/EntityMappers/SkinEntity.cs | 37 +++++++++++++++++++ 5 files changed, 73 insertions(+), 96 deletions(-) create mode 100644 EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index 02bc885..ff05005 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -17,13 +17,8 @@ namespace Business public async Task AddItem(Champion? item) { - try - { - await parent.DbContext.champions.AddAsync(item.ToEntity(parent.DbContext)); - parent.DbContext.SaveChanges(); - } - catch(OperationCanceledException){} - catch(DbUpdateException) {} + await parent.DbContext.champions.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } @@ -40,16 +35,12 @@ namespace Business } return false; } - catch (DbUpdateException) - { - return false; - } } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { Console.WriteLine("GET"); - return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").GetItemsWithFilterAndOrdering( c => true, index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -58,7 +49,7 @@ namespace Business public async Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Include("Characteristics").GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").GetItemsWithFilterAndOrdering( c => c.Characteristics.Any(ch => ch.Name.Equals(charName)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -66,7 +57,7 @@ namespace Business public async Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").GetItemsWithFilterAndOrdering( c => c.Class.Equals(championClass), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -74,7 +65,7 @@ namespace Business public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").GetItemsWithFilterAndOrdering( c => c.Name.Contains(substring), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -83,7 +74,7 @@ namespace Business public async Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Include("runepages").GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").Include("runepages").GetItemsWithFilterAndOrdering( c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity(parent.DbContext))), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -92,7 +83,7 @@ namespace Business public async Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").GetItemsWithFilterAndOrdering( c => skill != null && c.Skills.Any(s => s.Name.Equals(skill.Name)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -100,7 +91,7 @@ namespace Business public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Include("Skills").GetItemsWithFilterAndOrdering( + return parent.DbContext.champions.Include("Skills").Include("Characteristics").Include("Skills").GetItemsWithFilterAndOrdering( c => skill != null && c.Skills.Any(s => s.Name.Equals(skill)), index, count, orderingPropertyName, descending).Select(c => c.ToModel()); @@ -149,12 +140,8 @@ namespace Business public async Task UpdateItem(Champion? oldItem, Champion? newItem) { var toUpdate = parent.DbContext.champions.Find(oldItem.Name); - try - { - toUpdate = newItem.ToEntity(parent.DbContext); - parent.DbContext.SaveChanges(); - - }catch (DbUpdateException){} + toUpdate = newItem.ToEntity(parent.DbContext); + parent.DbContext.SaveChanges(); return newItem; } } diff --git a/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs b/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs index 012df0c..acd067f 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs @@ -16,39 +16,26 @@ namespace Business public async Task AddItem(RunePage? item) { - try - { - await parent.DbContext.runepages.AddAsync(item.ToEntity(parent.DbContext)); - parent.DbContext.SaveChanges(); - } - catch (OperationCanceledException) { } - catch (DbUpdateException) { } + await parent.DbContext.runepages.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(RunePage? item) { - - try - { - var toDelete = parent.DbContext.runepages.Find(item.Name); - if (toDelete != null) - { - parent.DbContext.runepages.Remove(toDelete); - parent.DbContext.SaveChanges(); - return true; - } - return false; - } - catch (DbUpdateException) + var toDelete = parent.DbContext.runepages.Find(item.Name); + if (toDelete != null) { - return false; + parent.DbContext.runepages.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runepages.GetItemsWithFilterAndOrdering( + return parent.DbContext.runepages.Include("entries").GetItemsWithFilterAndOrdering( rp => true, index, count, orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); @@ -64,7 +51,7 @@ namespace Business public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runepages.GetItemsWithFilterAndOrdering( + return parent.DbContext.runepages.Include("entries").GetItemsWithFilterAndOrdering( rp => rp.Name.Contains(substring), index, count, orderingPropertyName, descending).Select(rp => rp.ToModel(parent.DbContext)); @@ -104,8 +91,6 @@ namespace Business { var toUpdate = parent.DbContext.runepages.Include("entries") .Where(x => x.Name.Equals(newItem.Name)).First(); - try - { toUpdate.entries = newItem.Runes.Select(r => new RunePageRuneEntity() { category = r.Key, @@ -114,8 +99,6 @@ namespace Business parent.DbContext.SaveChanges(); - } - catch (DbUpdateException) { } return newItem; } diff --git a/EntityFramework_LoL/Sources/Business/DbData.Runes.cs b/EntityFramework_LoL/Sources/Business/DbData.Runes.cs index 118c9cf..332ff1a 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Runes.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Runes.cs @@ -15,33 +15,21 @@ namespace Business => this.parent = parent; public async Task AddItem(Rune? item) { - try - { - await parent.DbContext.runes.AddAsync(item.ToEntity(parent.DbContext)); - parent.DbContext.SaveChanges(); - } - catch (OperationCanceledException) {} - catch (DbUpdateException) { } + await parent.DbContext.runes.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(Rune? item) { - try - { - var toDelete = parent.DbContext.runes.Find(item?.Name); - if (toDelete != null) - { - parent.DbContext.runes.Remove(item?.ToEntity(parent.DbContext)); - parent.DbContext.SaveChanges(); - return true; - } - return false; - } - catch (DbUpdateException) + var toDelete = parent.DbContext.runes.Find(item?.Name); + if (toDelete != null) { - return false; + parent.DbContext.runes.Remove(item?.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); + return true; } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -86,14 +74,10 @@ namespace Business public async Task UpdateItem(Rune? oldItem, Rune? newItem) { var toUpdate = parent.DbContext.runes.Find(oldItem.Name); - try - { toUpdate.Description = newItem.Description; toUpdate.RuneFamily = newItem.Family; parent.DbContext.SaveChanges(); - } - catch (DbUpdateException) {} return newItem; } } diff --git a/EntityFramework_LoL/Sources/Business/DbData.Skins.cs b/EntityFramework_LoL/Sources/Business/DbData.Skins.cs index ad51b74..dafee84 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Skins.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Skins.cs @@ -15,26 +15,17 @@ namespace Business public async Task AddItem(Skin? item) { - try { - await parent.DbContext.skins.AddAsync(item.ToEntity(parent.DbContext)); - parent.DbContext.SaveChanges(); - } - catch(OperationCanceledException){} - catch(DbUpdateException) {} + await parent.DbContext.skins.AddAsync(item.ToEntity(parent.DbContext)); + parent.DbContext.SaveChanges(); return item; } public async Task DeleteItem(Skin? item) { - try { - var toDelete = parent.DbContext.skins.Find(item.Name); - parent.DbContext.skins.Remove(toDelete); - parent.DbContext.SaveChanges(); - return true; - } - catch(DbUpdateException) { - return false; - } + var toDelete = parent.DbContext.skins.Find(item.Name); + parent.DbContext.skins.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -85,12 +76,7 @@ namespace Business public async Task UpdateItem(Skin? oldItem, Skin? newItem) { var toUpdate = parent.DbContext.skins.Find(oldItem.Name); - try - { - toUpdate.Champion = parent.DbContext.champions.Find(newItem.Champion.Name); - - } - catch (DbUpdateException) { } + toUpdate.Champion = parent.DbContext.champions.Find(newItem.Champion.Name); return newItem; } } diff --git a/EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs b/EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs new file mode 100644 index 0000000..927dca3 --- /dev/null +++ b/EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs @@ -0,0 +1,37 @@ +using Shared; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Entities +{ + public class SkinEntity + { + [Key] + [MaxLength(256)] + public string Name { get; set; } + [Required] + [MaxLength(500)] + public string Description { get; set; } + [Required] + public string Icon { get; set; } + [Required] + public float Price { get; set; } + + [Required] + public string ChampionForeignKey { get; set; } + + [ForeignKey("ChampionForeignKey")] + public ChampionEntity Champion { get; set; } + + public Guid? ImageId { get; set; } + + [ForeignKey("ImageId")] + public LargeImageEntity? Image { get; set; } + + } +} -- 2.36.3 From 361a7fe9d55f0b5b92ac18530eff88535af65d82 Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Wed, 22 Mar 2023 19:58:02 +0100 Subject: [PATCH 2/3] Correcting bug --- .../Sources/Business/DbData.Champions.cs | 15 +++----- .../Sources/Business/DbData.Skins.cs | 2 +- .../Sources/EntityMappers/SkinEntity.cs | 37 ------------------- 3 files changed, 7 insertions(+), 47 deletions(-) delete mode 100644 EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index ff05005..296d45d 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -24,17 +24,14 @@ namespace Business public async Task DeleteItem(Champion? item) { - try + var toDelete = parent.DbContext.champions.Find(item.Name); + if (toDelete!=null) { - var toDelete = parent.DbContext.champions.Find(item.Name); - if (toDelete!=null) - { - parent.DbContext.champions.Remove(toDelete); - parent.DbContext.SaveChanges(); - return true; - } - return false; + parent.DbContext.champions.Remove(toDelete); + parent.DbContext.SaveChanges(); + return true; } + return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) diff --git a/EntityFramework_LoL/Sources/Business/DbData.Skins.cs b/EntityFramework_LoL/Sources/Business/DbData.Skins.cs index dafee84..2175f00 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Skins.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Skins.cs @@ -49,7 +49,7 @@ namespace Business return parent.DbContext.skins.Include("Champion").GetItemsWithFilterAndOrdering( s => s.Name.Contains(substring), index, count, - orderingPropertyName, descending).Select(s => s?.ToModel()); + orderingPropertyName, descending).Select(s => s.ToModel()); } public async Task GetNbItems() diff --git a/EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs b/EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs deleted file mode 100644 index 927dca3..0000000 --- a/EntityFramework_LoL/Sources/EntityMappers/SkinEntity.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Shared; -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Entities -{ - public class SkinEntity - { - [Key] - [MaxLength(256)] - public string Name { get; set; } - [Required] - [MaxLength(500)] - public string Description { get; set; } - [Required] - public string Icon { get; set; } - [Required] - public float Price { get; set; } - - [Required] - public string ChampionForeignKey { get; set; } - - [ForeignKey("ChampionForeignKey")] - public ChampionEntity Champion { get; set; } - - public Guid? ImageId { get; set; } - - [ForeignKey("ImageId")] - public LargeImageEntity? Image { get; set; } - - } -} -- 2.36.3 From a91154ff3a0c9bdcad1b947e7cc25b252adf5f6a Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Sat, 25 Mar 2023 11:52:52 +0100 Subject: [PATCH 3/3] Adding large image to entity mappers --- EntityFramework_LoL/Sources/Entities/ChampionEntity.cs | 3 ++- EntityFramework_LoL/Sources/Entities/RuneEntity.cs | 2 ++ .../Sources/EntityMappers/ChampionMapper.cs | 3 ++- EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs | 7 +++++-- EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs | 7 +++++-- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/EntityFramework_LoL/Sources/Entities/ChampionEntity.cs b/EntityFramework_LoL/Sources/Entities/ChampionEntity.cs index 42c060c..4480243 100644 --- a/EntityFramework_LoL/Sources/Entities/ChampionEntity.cs +++ b/EntityFramework_LoL/Sources/Entities/ChampionEntity.cs @@ -13,7 +13,8 @@ namespace Entities [Required] [MaxLength(500)] public string Bio { get; set; } - public string? Icon { get; set; } + public string Icon { get; set; } + [Required] public ChampionClass Class { get; set;} public virtual ICollection Skills { get; set; } diff --git a/EntityFramework_LoL/Sources/Entities/RuneEntity.cs b/EntityFramework_LoL/Sources/Entities/RuneEntity.cs index a456235..9abbe02 100644 --- a/EntityFramework_LoL/Sources/Entities/RuneEntity.cs +++ b/EntityFramework_LoL/Sources/Entities/RuneEntity.cs @@ -19,6 +19,8 @@ namespace Entities [MaxLength(500)] public string Description { get; set; } + public string Icon { get; set; } + [Required] public RuneFamily RuneFamily { get; set; } public ICollection? runepages { get; set; } diff --git a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs index e00b0e3..43a754a 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/ChampionMapper.cs @@ -30,7 +30,8 @@ namespace EntityMapper public static Champion ToModel(this ChampionEntity entity) { - var champion = new Champion(entity.Name, entity.Class, entity.Icon, "", entity.Bio); + var image = entity?.Image?.Base64 ?? ""; + var champion = new Champion(entity?.Name ?? "", entity?.Class??Shared.ChampionClass.Unknown, entity?.Icon??"", image , 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/RuneMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs index 5dcf3d8..4cac4f4 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/RuneMapper.cs @@ -14,7 +14,9 @@ namespace EntityMapper { Name = item.Name, Description = item.Description, - RuneFamily = item.Family + RuneFamily = item.Family, + Icon = item.Icon, + Image = new() { Base64 = item.Image.Base64 }, }; } return runeEntity; @@ -23,7 +25,8 @@ namespace EntityMapper public static Rune ToModel(this RuneEntity entity) { - return new Rune(entity.Name, entity.RuneFamily, "", "", entity.Description); + var image = entity?.Image?.Base64 ?? ""; + return new Rune(entity?.Name ?? "", entity?.RuneFamily??Shared.RuneFamily.Unknown, entity?.Icon ?? "", image, entity?.Description??""); } } diff --git a/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs b/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs index c595ec2..4a172b7 100644 --- a/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs +++ b/EntityFramework_LoL/Sources/EntityMappers/SkinMapper.cs @@ -15,14 +15,17 @@ namespace EntityMapper ChampionForeignKey = item.Champion.Name, Description = item.Description, Icon = item.Icon, - Image = null, + Image = new() { Base64 = item.Image.Base64 }, Price = item.Price }; } public static Skin ToModel(this SkinEntity entity) - => new(entity.Name, entity.Champion.ToModel(), entity.Price, null, entity.Description); + { + var image = entity?.Image?.Base64 ?? ""; + return new(entity?.Name ?? "", entity?.Champion?.ToModel()??new(""), entity?.Price??-1, image, entity?.Description??""); + } } } -- 2.36.3