You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
7.2 KiB

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<Champion?> 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<bool> 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<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<Champion> champs = new List<Champion>();
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<int> GetNbItems()
{
return await Task.FromResult(Context.Champions.Count());
}
public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<Champion> champs = new List<Champion>();
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<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<Champion> champs = new List<Champion>();
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<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByCharacteristic(string charName)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByClass(ChampionClass championClass)
{
throw new NotImplementedException();
}
public async Task<int> GetNbItemsByName(string substring)
{
return await Task.FromResult(Context.Champions.Where((arg) => arg.Name.Contains(substring)).Count());
}
public Task<int> GetNbItemsByRunePage(RunePage? runePage)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsBySkill(Skill? skill)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsBySkill(string skill)
{
throw new NotImplementedException();
}
public async Task<Champion?> 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;
}
}
}