using System; using Microsoft.EntityFrameworkCore; using Model; using EFLib; namespace DataManagers { public class DbChampionManager : IChampionsManager { private LolContext Context { get; set; } public DbChampionManager(LolContext context) { Context = context; } public async Task AddItem(Champion? item) { if (item != null && Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Id == item.Id || arg.Name == item.Name) == null) { await Context.Champions.AddAsync(item.ToEntity()); await Context.SaveChangesAsync(); return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Name == item.Name)?.ToPoco()); } return null; } public async Task DeleteItem(Champion? item) { if (item != null) { ChampionEntity? entity = await Context.Champions.SingleOrDefaultAsync((ChampionEntity arg) => arg.Id == item.Id || arg.Name == item.Name); if (entity != null) { Context.Champions.Remove(entity); int test = await Context.SaveChangesAsync(); return test==1; } } return false; } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) { IEnumerable champs = new List(); switch (orderingPropertyName?.ToLower()) { case "bio": champs = await Task.Run(() => Context.Champions.OrderBy((arg) => arg.Bio).Skip(index * count).Take(count).ToList().ToPocos()); break; case "class": champs = await Task.Run(() => Context.Champions.OrderBy((arg) => arg.ChampClass).Skip(index * count).Take(count).ToList().ToPocos()); break; default: champs = await Task.Run(() => Context.Champions.OrderBy((arg) => arg.Name).Skip(index * count).Take(count).ToList().ToPocos()); break; } if (descending) { champs.Reverse(); } return champs; } public async Task GetNbItems() { return await Task.FromResult(Context.Champions.Count()); } public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) { throw new NotImplementedException(); } public async Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) { IEnumerable champs = new List(); switch (orderingPropertyName?.ToLower()) { case "bio": champs = await Task.Run(() => Context.Champions.Where((arg) => arg.ChampClass == championClass).OrderBy((arg) => arg.Bio).Skip(index * count).Take(count).ToList().ToPocos()); break; case "class": champs = await Task.Run(() => Context.Champions.Where((arg) => arg.ChampClass == championClass).OrderBy((arg) => arg.ChampClass).Skip(index * count).Take(count).ToList().ToPocos()); break; default: champs = await Task.Run(() => Context.Champions.Where((arg) => arg.ChampClass == championClass).OrderBy((arg) => arg.Name).Skip(index * count).Take(count).ToList().ToPocos()); break; } if (descending) { champs.Reverse(); } return champs; } public async Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) { IEnumerable champs = new List(); switch (orderingPropertyName?.ToLower()) { case "bio": champs = await Task.Run(() => Context.Champions.Where((arg) => arg.Name.Contains(substring)).OrderBy((arg) => arg.Bio).Skip(index * count).Take(count).ToList().ToPocos()); break; case "class": champs = await Task.Run(() => Context.Champions.Where((arg) => arg.Name.Contains(substring)).OrderBy((arg) => arg.ChampClass).Skip(index * count).Take(count).ToList().ToPocos()); break; default: champs = await Task.Run(() => Context.Champions.Where((arg) => arg.Name.Contains(substring)).OrderBy((arg) => arg.Name).Skip(index * count).Take(count).ToList().ToPocos()); break; } if (descending) { champs.Reverse(); } return champs; } 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 Task GetNbItemsByCharacteristic(string charName) { throw new NotImplementedException(); } public Task GetNbItemsByClass(ChampionClass championClass) { throw new NotImplementedException(); } public async Task GetNbItemsByName(string substring) { return await Task.FromResult(Context.Champions.Where((arg) => arg.Name.Contains(substring)).Count()); } public Task GetNbItemsByRunePage(RunePage? runePage) { throw new NotImplementedException(); } public Task GetNbItemsBySkill(Skill? skill) { throw new NotImplementedException(); } public Task GetNbItemsBySkill(string skill) { throw new NotImplementedException(); } public async Task UpdateItem(Champion? oldItem, Champion? newItem) { ChampionEntity? champ = await Context.Champions.SingleOrDefaultAsync((arg) => arg.Id == oldItem.Id); if (champ != null) { champ.Bio = newItem.Bio; champ.Name = newItem.Name; champ.ChampClass = newItem.Class; champ.Icon = newItem.Icon; Context.Champions.Update(champ); await Context.SaveChangesAsync(); return champ.ToPoco(); } return null; } } }