diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index 02bc885..296d45d 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -17,39 +17,27 @@ 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; } public async Task DeleteItem(Champion? item) { - try - { - var toDelete = parent.DbContext.champions.Find(item.Name); - if (toDelete!=null) - { - parent.DbContext.champions.Remove(toDelete); - parent.DbContext.SaveChanges(); - return true; - } - return false; - } - catch (DbUpdateException) + var toDelete = parent.DbContext.champions.Find(item.Name); + if (toDelete!=null) { - 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) { 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 +46,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 +54,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 +62,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 +71,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 +80,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 +88,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 +137,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..2175f00 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) @@ -58,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() @@ -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/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??""); + } } }