From 8aaadc0c8db91eccaa2fd97135fbf3613b0f25e7 Mon Sep 17 00:00:00 2001 From: Louwar Date: Thu, 23 Mar 2023 11:14:10 +0100 Subject: [PATCH] Update Manager --- Sources/EFManager/ManagerChampion.cs | 64 +++++++------- Sources/EFManager/ManagerRune.cs | 91 +++++++++---------- Sources/EFManager/ManagerRunePage.cs | 89 ++++++++++--------- Sources/EFManager/ManagerSkin.cs | 127 +++++++++++++++++++-------- 4 files changed, 214 insertions(+), 157 deletions(-) diff --git a/Sources/EFManager/ManagerChampion.cs b/Sources/EFManager/ManagerChampion.cs index 6cb1aa7..9c96722 100644 --- a/Sources/EFManager/ManagerChampion.cs +++ b/Sources/EFManager/ManagerChampion.cs @@ -33,7 +33,7 @@ namespace EFManager var toDelete = parent.DbContext.Champions.Find(item.Name); if (toDelete != null) { - parent.DbContext.Champions.Remove(item.toEF(parent.DbContext)); + parent.DbContext.Champions.Remove(toDelete); parent.DbContext.SaveChangesAsync(); return true; } @@ -51,30 +51,16 @@ namespace EFManager { if (descending) { - return await Task.FromResult(parent.DbContext.Champions.OrderByDescending( - c => typeof(EFChampion).GetProperty(orderingPropertyName)) - .Skip(index * count) - .Take(count) - .Select(ce => ce.toModel()) - ); + return await Task.FromResult(parent.DbContext.Champions.OrderByDescending(champ => typeof(EFChampion).GetProperty(orderingPropertyName)).Skip(index * count).Take(count).Select(efChampion => efChampion.toModel())); } else { - return await Task.FromResult(parent.DbContext.Champions.OrderBy( - c => typeof(EFChampion).GetProperty(orderingPropertyName)) - .Skip(index * count) - .Take(count) - .Select(ce => ce.toModel()) - ); + return await Task.FromResult(parent.DbContext.Champions.OrderBy(champ => typeof(EFChampion).GetProperty(orderingPropertyName)).Skip(index * count).Take(count).Select(efChampion => efChampion.toModel())); } } else { - return await Task.FromResult(parent.DbContext.Champions - .Skip(index * count) - .Take(count) - .Select(ce => ce.toModel()) - ); + return await Task.FromResult(parent.DbContext.Champions.Skip(index * count).Take(count).Select(efChampion => efChampion.toModel())); } } @@ -90,10 +76,25 @@ namespace EFManager } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - + public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + var EfChampion = from champ in parent.DbContext.Champions where champ.Name.Contains(substring) select champ; + if (orderingPropertyName != null) + { + if (descending) + { + return await Task.FromResult(EfChampion.OrderByDescending(champ => typeof(EFChampion).GetProperty(orderingPropertyName)).Skip(index * count).Take(count).Select(efChampion => efChampion.toModel())); + } + else + { + return await Task.FromResult(EfChampion.OrderBy(champ => typeof(EFChampion).GetProperty(orderingPropertyName)).Skip(index * count).Take(count).Select(efChampion => efChampion.toModel())); + + } + } + else + { + return await Task.FromResult(EfChampion.Skip(index * count).Take(count).Select(efChampion => efChampion.toModel())); + } } public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -122,12 +123,12 @@ namespace EFManager throw new NotImplementedException(); } - public Task GetNbItemsByClass(ChampionClass championClass) + public async Task GetNbItemsByClass(ChampionClass championClass) { throw new NotImplementedException(); } - public Task GetNbItemsByName(string substring) + public async Task GetNbItemsByName(string substring) { throw new NotImplementedException(); } @@ -137,24 +138,25 @@ namespace EFManager throw new NotImplementedException(); } - public Task GetNbItemsBySkill(Skill? skill) + public async Task GetNbItemsBySkill(Skill? skill) { throw new NotImplementedException(); } public async Task GetNbItemsBySkill(string skill) { - return parent.DbContext.Champions.Where(champ => skill != null && champ.Skills.Any(Skill => Skill.Name.Equals(skill))) - .Count(); + throw new NotImplementedException(); } public async Task UpdateItem(Champion? oldItem, Champion? newItem) { var toUpdate = parent.DbContext.Champions.Find(oldItem.Name); - - toUpdate = newItem.toEF(); - parent.DbContext.SaveChanges(); - + try + { + toUpdate = newItem.toEF(parent.DbContext); + parent.DbContext.SaveChanges(); + } + catch(DbUpdateException) { } return newItem; } } diff --git a/Sources/EFManager/ManagerRune.cs b/Sources/EFManager/ManagerRune.cs index a9185da..6808340 100644 --- a/Sources/EFManager/ManagerRune.cs +++ b/Sources/EFManager/ManagerRune.cs @@ -7,51 +7,54 @@ using System.Threading.Tasks; namespace EFManager { - public class ManagerRune : IRunesManager + public partial class ManagerData { - public Task AddItem(Model.Rune? item) - { - throw new NotImplementedException(); - } - - public Task DeleteItem(Model.Rune? item) - { - throw new NotImplementedException(); - } - - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } - - public Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } - - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItems() - { - throw new NotImplementedException(); - } - - public Task GetNbItemsByFamily(RuneFamily family) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } - - public Task UpdateItem(Model.Rune? oldItem, Model.Rune? newItem) - { - throw new NotImplementedException(); + public class ManagerRune : IRunesManager + { + public Task AddItem(Model.Rune? item) + { + throw new NotImplementedException(); + } + + public Task DeleteItem(Model.Rune? item) + { + throw new NotImplementedException(); + } + + public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItems() + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByFamily(RuneFamily family) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } + + public Task UpdateItem(Model.Rune? oldItem, Model.Rune? newItem) + { + throw new NotImplementedException(); + } } } } diff --git a/Sources/EFManager/ManagerRunePage.cs b/Sources/EFManager/ManagerRunePage.cs index c6ae9b1..b06a636 100644 --- a/Sources/EFManager/ManagerRunePage.cs +++ b/Sources/EFManager/ManagerRunePage.cs @@ -7,61 +7,64 @@ using System.Threading.Tasks; namespace EFManager { - public class ManagerRunePage : IRunePagesManager + public partial class ManagerData { - public Task AddItem(RunePage? item) + public class ManagerRunePage : IRunePagesManager { - throw new NotImplementedException(); - } + public Task AddItem(RunePage? item) + { + throw new NotImplementedException(); + } - public Task DeleteItem(RunePage? item) - { - throw new NotImplementedException(); - } + public Task DeleteItem(RunePage? item) + { + throw new NotImplementedException(); + } - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } - public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } - public Task> GetItemsByRune(Model.Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public Task> GetItemsByRune(Model.Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } - public Task GetNbItems() - { - throw new NotImplementedException(); - } + public Task GetNbItems() + { + throw new NotImplementedException(); + } - public Task GetNbItemsByChampion(Champion? champion) - { - throw new NotImplementedException(); - } + public Task GetNbItemsByChampion(Champion? champion) + { + throw new NotImplementedException(); + } - public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } - public Task GetNbItemsByRune(Model.Rune? rune) - { - throw new NotImplementedException(); - } + public Task GetNbItemsByRune(Model.Rune? rune) + { + throw new NotImplementedException(); + } - public Task UpdateItem(RunePage? oldItem, RunePage? newItem) - { - throw new NotImplementedException(); + public Task UpdateItem(RunePage? oldItem, RunePage? newItem) + { + throw new NotImplementedException(); + } } } } diff --git a/Sources/EFManager/ManagerSkin.cs b/Sources/EFManager/ManagerSkin.cs index dde4c6d..a1fc3e4 100644 --- a/Sources/EFManager/ManagerSkin.cs +++ b/Sources/EFManager/ManagerSkin.cs @@ -1,4 +1,7 @@ -using Model; +using EFlib; +using EFMapping; +using Microsoft.EntityFrameworkCore; +using Model; using System; using System.Collections.Generic; using System.Linq; @@ -7,54 +10,100 @@ using System.Threading.Tasks; namespace EFManager { - public class ManagerSkin : ISkinsManager + public partial class ManagerData { - private readonly ManagerData parent; - public ManagerSkin(ManagerData parent) - => this.parent = parent; - public Task AddItem(Skin? item) + public class ManagerSkin : ISkinsManager { - throw new NotImplementedException(); - } + private readonly ManagerData parent; + public ManagerSkin(ManagerData parent) + => this.parent = parent; + public async Task AddItem(Skin? item) + { + try + { + await parent.DbContext.Skins.AddAsync(item.toEF(parent.DbContext)); + parent.DbContext.SaveChangesAsync(); + } + catch (OperationCanceledException) { } + catch (DbUpdateException) { } + return item; + } - public Task DeleteItem(Skin? item) - { - throw new NotImplementedException(); - } + public async Task DeleteItem(Skin? item) + { + try + { + var toDelete = parent.DbContext.Skins.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.Skins.Remove(toDelete); + parent.DbContext.SaveChangesAsync(); + return true; + } + return false; + } + catch (DbUpdateException) + { + return false; + } + } - public Task> GetItems(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) + { + if (orderingPropertyName != null) + { + if (descending) + { + return await Task.FromResult(parent.DbContext.Skins.OrderByDescending(skin => typeof(EFSkin).GetProperty(orderingPropertyName)).Skip(index * count).Take(count).Select(efSkin => efSkin.toModel())); + } + else + { + return await Task.FromResult(parent.DbContext.Skins.OrderBy(skin => typeof(EFSkin).GetProperty(orderingPropertyName)).Skip(index * count).Take(count).Select(efSkin => efSkin.toModel())); + } + } + else + { + return await Task.FromResult(parent.DbContext.Skins.Skip(index * count).Take(count).Select(efSkin => efSkin.toModel())); + } + } - public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) - { - throw new NotImplementedException(); - } + public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + throw new NotImplementedException(); + } - public Task GetNbItems() - { - throw new NotImplementedException(); - } + public async Task GetNbItems() + { + return parent.DbContext.Skins.Count(); + } - public Task GetNbItemsByChampion(Champion? champion) - { - throw new NotImplementedException(); - } + public Task GetNbItemsByChampion(Champion? champion) + { + throw new NotImplementedException(); + } - public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } - public Task UpdateItem(Skin? oldItem, Skin? newItem) - { - throw new NotImplementedException(); + 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); + parent.DbContext.SaveChanges(); + } + catch (DbUpdateException) { } + return newItem; + + } } } }