using EFlib; using Microsoft.EntityFrameworkCore; using System.Linq; using Model; namespace EFManager { public partial class ManagerData { public class ManagerChampion : IChampionsManager { private readonly ManagerData parent; public ManagerChampion(ManagerData parent) => this.parent = parent; public async Task AddItem(Champion? item) { if (item == null) return null; await parent.DbContext.Champions.AddAsync(item.toEF()); await parent.DbContext.SaveChangesAsync(); return item; } public async Task DeleteItem(Champion? item) { if (item == null) return false; parent.DbContext.Champions.Remove(item.toEF()); await parent.DbContext.SaveChangesAsync(); return true; } 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.Champions.OrderByDescending( c => typeof(EFChampion).GetProperty(orderingPropertyName)) .Skip(index * count) .Take(count) .Select(ce => ce.toModel()) ); } else { return await Task.FromResult(parent.DbContext.Champions.OrderBy( c => typeof(EFChampion).GetProperty(orderingPropertyName)) .Skip(index * count) .Take(count) .Select(ce => ce.toModel()) ); } } else { return await Task.FromResult(parent.DbContext.Champions .Skip(index * count) .Take(count) .Select(ce => ce.toModel()) ); } } public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { throw new NotImplementedException(); } public Task> GetItemsByClass(ChampionClass championClass, 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> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) { throw new NotImplementedException(); } public Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { throw new NotImplementedException(); } public Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false) { throw new NotImplementedException(); } public async Task GetNbItems() { return parent.DbContext.Champions.Count(); } public async Task GetNbItemsByCharacteristic(string charName) { throw new NotImplementedException(); } public Task GetNbItemsByClass(ChampionClass championClass) { throw new NotImplementedException(); } public Task GetNbItemsByName(string substring) { throw new NotImplementedException(); } public Task GetNbItemsByRunePage(RunePage? runePage) { throw new NotImplementedException(); } public 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(); } public async Task UpdateItem(Champion? oldItem, Champion? newItem) { var toUpdate = parent.DbContext.Champions.Find(oldItem.Name); toUpdate = newItem.toEF(); parent.DbContext.SaveChanges(); return newItem; } } } }