diff --git a/.vs/League-of-Legends_Project3/v17/.wsuo b/.vs/League-of-Legends_Project3/v17/.wsuo index 4fd9423..0700017 100644 Binary files a/.vs/League-of-Legends_Project3/v17/.wsuo and b/.vs/League-of-Legends_Project3/v17/.wsuo differ diff --git a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs index 52bfe7f..0c31b3f 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Champions.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Champions.cs @@ -28,50 +28,61 @@ namespace Business public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Select(c =>c.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => true, + index, count, + orderingPropertyName, descending).Select(c => c.ToModel()); } public async Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Where(c => c.Characteristics.Any(ch => ch.Name.Equals(charName))) - .Select(c => c.ToModel()) - .Skip(index * count).Take(count); + + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => c.Characteristics.Any(ch => ch.Name.Equals(charName)), + index, count, + orderingPropertyName, descending).Select(c => c.ToModel()); } public async Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Where(c => c.Class.Equals(championClass)) - .Select(c => c.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => c.Class.Equals(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) { - return parent.DbContext.champions.Where(c => c.Name.Contains(substring)) - .Select(c => c.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => c.Name.Contains(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) { - return parent.DbContext.champions.Where(c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity()))) - .Select(c => c.ToModel()) - .Skip(index * count).Take(count); + + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity())), + index, count, + orderingPropertyName, descending).Select(c => c.ToModel()); + } public async Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Where(c => skill!=null && c.Skills.Any(s => s.Name.Equals(skill.Name))) - .Select(c => c.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => skill != null && c.Skills.Any(s => s.Name.Equals(skill.Name)), + index, count, + orderingPropertyName, descending).Select(c => c.ToModel()); } public async Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.champions.Where(c => skill != null && c.Skills.Any(s => s.Name.Equals(skill))) - .Select(c => c.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.champions.GetItemsWithFilterAndOrdering( + c => skill != null && c.Skills.Any(s => s.Name.Equals(skill)), + index, count, + orderingPropertyName, descending).Select(c => c.ToModel()); } public async Task GetNbItems() diff --git a/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs b/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs index b7ea7fc..3de4937 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.RunePages.cs @@ -1,5 +1,7 @@ using API_LoL_Project.Mapper; using Model; +using System.Data.SqlTypes; +using System.Linq; namespace Business { @@ -26,29 +28,34 @@ namespace Business public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runepages.Select(rp => rp.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runepages.GetItemsWithFilterAndOrdering( + rp => true, + index, count, + orderingPropertyName, descending).Select(rp => rp.ToModel()); } public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runepages.Where(rp => rp.champions.Any(c => c.Name.Equals(champion.Name))) - .Select(rp => rp.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runepages.GetItemsWithFilterAndOrdering( + rp => rp.champions.Any(c => c.Name.Equals(champion.Name)), + index, count, + orderingPropertyName, descending).Select(rp => rp.ToModel()); } public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runepages.Where(rp => rp.Name.Contains(substring)) - .Select(rp => rp.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runepages.GetItemsWithFilterAndOrdering( + rp => rp.Name.Contains(substring), + index, count, + orderingPropertyName, descending).Select(rp => rp.ToModel()); } public async Task> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runepages.Where(rp => rp.runes.Any(r => r.Name.Equals(rune.Name))) - .Select(rp => rp.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runepages.GetItemsWithFilterAndOrdering( + rp => rp.runes.Any(r => r.Name.Equals(rune.Name)), + index, count, + orderingPropertyName, descending).Select(rp => rp.ToModel()); } public async Task GetNbItems() diff --git a/EntityFramework_LoL/Sources/Business/DbData.Runes.cs b/EntityFramework_LoL/Sources/Business/DbData.Runes.cs index bcd964f..9c97b23 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Runes.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Runes.cs @@ -27,23 +27,26 @@ namespace Business public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runes - .Select(r => r.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runes.GetItemsWithFilterAndOrdering( + r => true, + index, count, + orderingPropertyName, descending).Select(r => r.ToModel()); } public async Task> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runes.Where(r => r.RuneFamily.Equals(family)) - .Select(r => r.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runes.GetItemsWithFilterAndOrdering( + r => r.RuneFamily.Equals(family), + index, count, + orderingPropertyName, descending).Select(r => r.ToModel()); } public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.runes.Where(r => r.Name.Contains(substring)) - .Select(r => r.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.runes.GetItemsWithFilterAndOrdering( + r => r.Name.Contains(substring), + index, count, + orderingPropertyName, descending).Select(r => r.ToModel()); } public async Task GetNbItems() diff --git a/EntityFramework_LoL/Sources/Business/DbData.Skins.cs b/EntityFramework_LoL/Sources/Business/DbData.Skins.cs index 4a0a965..47b7ada 100644 --- a/EntityFramework_LoL/Sources/Business/DbData.Skins.cs +++ b/EntityFramework_LoL/Sources/Business/DbData.Skins.cs @@ -1,5 +1,7 @@ using API_LoL_Project.Mapper; +using API_LoL_Project.Mapper.API_LoL_Project.Mapper; using Model; +using System.Data.SqlTypes; namespace Business { @@ -26,23 +28,26 @@ namespace Business public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.skins - .Select(s => s.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.skins.GetItemsWithFilterAndOrdering( + s => true, + index, count, + orderingPropertyName, descending).Select(s => s.ToModel()); } public async Task> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.skins.Where(s => s.Champion.Name.Equals(champion.Name)) - .Select(s => s.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.skins.GetItemsWithFilterAndOrdering( + s => s.Champion.Name.Equals(champion.Name), + index, count, + orderingPropertyName, descending).Select(s => s.ToModel()); } public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - return parent.DbContext.skins.Where(s => s.Name.Contains(substring)) - .Select(s => s.ToModel()) - .Skip(index * count).Take(count); + return parent.DbContext.skins.GetItemsWithFilterAndOrdering( + s => s.Name.Contains(substring), + index, count, + orderingPropertyName, descending).Select(s => s.ToModel()); } public async Task GetNbItems() diff --git a/EntityFramework_LoL/Sources/Business/Extensions.cs b/EntityFramework_LoL/Sources/Business/Extensions.cs new file mode 100644 index 0000000..9f99257 --- /dev/null +++ b/EntityFramework_LoL/Sources/Business/Extensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Business +{ + static class Extensions + { + internal static IEnumerable GetItemsWithFilterAndOrdering(this IEnumerable collection, + Func filter, int index, int count, string? orderingPropertyName = null, bool descending = false) + { + IEnumerable temp = collection; + temp = temp.Where(item => filter(item)); + if (orderingPropertyName != null) + { + var prop = typeof(T).GetProperty(orderingPropertyName!); + if (prop != null) + { + temp = descending ? temp.OrderByDescending(item => prop.GetValue(item)) + : temp.OrderBy(item => prop.GetValue(item)); + } + } + return temp.Skip(index * count).Take(count) + } + } +} diff --git a/EntityFramework_LoL/Sources/Entities/Entities.Champions.db b/EntityFramework_LoL/Sources/Entities/Entities.Champions.db index 1c7c62b..9ee7a58 100644 Binary files a/EntityFramework_LoL/Sources/Entities/Entities.Champions.db and b/EntityFramework_LoL/Sources/Entities/Entities.Champions.db differ