Adding extension class

Arthur_EF_Business
Arthur VALIN 2 years ago
parent 9ed397d7d7
commit 2ac48443f3

@ -28,50 +28,61 @@ namespace Business
public async Task<IEnumerable<Champion?>> 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<IEnumerable<Champion?>> 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<IEnumerable<Champion?>> 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<IEnumerable<Champion?>> 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<IEnumerable<Champion?>> 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<IEnumerable<Champion?>> 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<IEnumerable<Champion?>> 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<int> GetNbItems()

@ -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<IEnumerable<RunePage?>> 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<IEnumerable<RunePage?>> 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<IEnumerable<RunePage?>> 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<IEnumerable<RunePage?>> 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<int> GetNbItems()

@ -27,23 +27,26 @@ namespace Business
public async Task<IEnumerable<Rune?>> 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<IEnumerable<Rune?>> 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<IEnumerable<Rune?>> 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<int> GetNbItems()

@ -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<IEnumerable<Skin?>> 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<IEnumerable<Skin?>> 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<IEnumerable<Skin?>> 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<int> GetNbItems()

@ -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<T?> GetItemsWithFilterAndOrdering<T>(this IEnumerable<T> collection,
Func<T, bool> filter, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<T> 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)
}
}
}
Loading…
Cancel
Save