From 1371e86c20dd39a8d841820acc01dfda2c7e71c5 Mon Sep 17 00:00:00 2001 From: Louwar Date: Wed, 15 Mar 2023 15:10:35 +0100 Subject: [PATCH] Update Manager --- Sources/EFManager/ManagerChampion.cs | 87 +++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/Sources/EFManager/ManagerChampion.cs b/Sources/EFManager/ManagerChampion.cs index 992c1e2..9c41a87 100644 --- a/Sources/EFManager/ManagerChampion.cs +++ b/Sources/EFManager/ManagerChampion.cs @@ -1,4 +1,5 @@ using EFlib; +using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Model; @@ -10,39 +11,109 @@ namespace EFManager public async Task AddItem(Champion? item) { - //await context.AddAsync(item.); TO DO + if (item == null) + return null; + + await context.AddAsync(item.toEF()); await context.SaveChangesAsync(); return item; } public Task DeleteItem(Champion? item) { - throw new NotImplementedException(); + if (item == null) + return false; + + context.Remove(item.toEF()); + await context.SaveChangesAsync(); + return true; } public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IQueryable query = Champions.Skip(index).Take(count); + + if (!string.IsNullOrEmpty(orderingPropertyName)) + { + query = descending ? + query.OrderByDescending(c => EF.Property(c, orderingPropertyName)) : + query.OrderBy(c => EF.Property(c, orderingPropertyName)); + } + + return await query.ToListAsync(); } public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IQueryable query = Champions + .Where(c => c.Characteristics.ContainsKey(charName)) + .Skip(index) + .Take(count); + + if (!string.IsNullOrEmpty(orderingPropertyName)) + { + query = descending ? + query.OrderByDescending(c => EF.Property(c, orderingPropertyName)) : + query.OrderBy(c => EF.Property(c, orderingPropertyName)); + } + + return await query.ToListAsync(); + } public Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IQueryable query = Champions + .Where(c => c.Class == championClass) + .Skip(index) + .Take(count); + + if (!string.IsNullOrEmpty(orderingPropertyName)) + { + query = descending ? + query.OrderByDescending(c => EF.Property(c, orderingPropertyName)) : + query.OrderBy(c => EF.Property(c, orderingPropertyName)); + } + + return await query.ToListAsync(); } public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IQueryable query = Champions + .Where(c => c.Name.Contains(substring)) + .Skip(index) + .Take(count); + + if (!string.IsNullOrEmpty(orderingPropertyName)) + { + query = descending ? + query.OrderByDescending(c => EF.Property(c, orderingPropertyName)) : + query.OrderBy(c => EF.Property(c, orderingPropertyName)); + } + + return await query.ToListAsync(); } public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) { - throw new NotImplementedException(); + IQueryable query = Champions; + + if (runePage != null) + { + query = query.Where(c => c.RunePageId == runePage.Id); + } + + query = query.Skip(index).Take(count); + + if (!string.IsNullOrEmpty(orderingPropertyName)) + { + query = descending ? + query.OrderByDescending(c => EF.Property(c, orderingPropertyName)) : + query.OrderBy(c => EF.Property(c, orderingPropertyName)); + } + + return await query.ToListAsync(); } public Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -62,7 +133,7 @@ namespace EFManager public Task GetNbItemsByCharacteristic(string charName) { - throw new NotImplementedException(); + return await context.Where(c => c.Characteristics.Any(ch => ch.Name == charName)).CountAsync(); } public Task GetNbItemsByClass(ChampionClass championClass)