using EntityFrameworkLib; using EntityFrameworkLib.Mappers; using Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DBDataManager { static class Extensions { internal static Task> GetItemsWithFilterAndOrdering(this IEnumerable collection, Func filter, int index, int count, string? orderingPropertyName = null, bool descending = false) { IEnumerable 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 Task.FromResult>(temp.Skip(index * count).Take(count)); } public static IEnumerable toPocos(this IEnumerable champions) { List result = new List(); foreach(ChampionEntity champion in champions) { result.Add(champion.toModel()); } return result; } public static IEnumerable toPocos(this IEnumerable skins) { List result = new List(); foreach(SkinEntity skin in skins) { result.Add(skin.toModel()); } return result; } internal static Task GetNbItemsWithFilter(this IEnumerable collection, Func filter) { return Task.FromResult(collection.Count(item => filter(item))); } internal static Task UpdateItem(this IList collection, Champion? oldItem, Champion? newItem) { if (oldItem == null || newItem == null) return Task.FromResult(default(Champion)); if (!collection.Contains(oldItem)) { return Task.FromResult(default(Champion)); } collection.Remove(oldItem!); collection.Add(newItem!); return Task.FromResult(newItem); } } }