using System; using System.Linq; using xModels.Dtos; using System.Reflection; using xCommons.Extensions; using xExceptions.Constants; using System.Threading.Tasks; using xDataService.Constants; using xIdentityModels.Models; using System.Linq.Expressions; using System.Collections.Generic; using xDataService.Configuration; using Microsoft.EntityFrameworkCore; namespace xIds.Extensions { public static class XModelExtensions { /// /// Determine a banned time is passed or not /// /// /// /// public static bool IsDelayTimePassed( this XBannedDevice source, int delaySeconds ) { // var passedTime = source.BannedOn.AddSeconds(delaySeconds); // var result = DateTime.UtcNow >= passedTime; // return result; } // /// // /// Apply Search Filter on a Enumerable // /// // /// // /// // /// // /// // public static IEnumerable ApplyFilter( // this IEnumerable source, // string filter // ) // where T : class // { // // // // Apply Filter ... // return source.Where(r => r.PropValuesContains(filter)); // } // /// // /// Count Query Pages based on Specified Total Number ... // /// // /// // /// // /// // public static int CountPages( // this XQuery source, // int totalItems // ) // { // // // int result = 0; // // // // Validate and Calculate ... // if (!source.IsNull() && source.PageSize > 0 && totalItems > 0) // { // // // result = totalItems / source.PageSize; // if (totalItems % source.PageSize > 0) // { // result++; // } // } // // // return result; // } // /// // /// Apply Sorting on a Enumerable // /// // /// // /// // /// // /// // /// // /// // public static IEnumerable ApplySorting( // this IEnumerable source, // string sortBy, // bool isAscending, // IDictionary>> columnsMap = null // ) // where T : class // { // // // if (columnsMap == null) // { // // // columnsMap = source.FirstOrDefault() // .GetDefaultColumnsMap(); // // // if (columnsMap == null) // { // return source; // } // } // // // // Validate SortBy ... // var sortByItem = columnsMap.Keys // .FirstOrDefault(p => // p.ToNormalString() == sortBy.ToNormalString()); // if (sortByItem.IsNullOrEmpty()) // { // return source; // } // // // // Apply Sorting ... // if (isAscending) // { // return source.OrderBy(sortByItem); // } // else // { // return source.OrderByDescending(sortByItem); // } // } // /// // /// Apply Paging on a Enumerable // /// // /// // /// // /// // /// // /// // public static IEnumerable ApplyPaging( // this IEnumerable source, // int? page, // int? pageSize // ) // { // // // if (!page.HasValue || // page <= 0) // { // page = 1; // } // // // if (!pageSize.HasValue || // pageSize <= 0 || // pageSize > XDataServiceConstants.MAX_AVAILABLE_PAGE_SIZE) // { // pageSize = XDataServiceConstants.DEFAULT_PAGE_SIZE; // } // // // var pageVal = page.GetValueOrDefault(); // var pageSizeVal = pageSize.GetValueOrDefault(); // // // return source // .Skip((pageVal - 1) * pageSizeVal) // .Take(pageSizeVal); // } /// /// Applying Filter to IQueryable /// /// /// public static async Task> ApplyFilterAsync( this IQueryable source, string filter ) where T : class { // // Apply Filter ... if (!filter.IsNullOrEmpty()) { // var items = source.AsAsyncEnumerable(); var filteredItems = new List(); await foreach (var item in items) { // if (item.GetPropValues() .ToNormalString() .Contains(filter .ToNormalString())) { // filteredItems.Add(item); } } // return filteredItems.AsQueryable(); } // return source; } public static XDataServiceConfiguration ToXDataServiceConfig(this Configurations.XDataServiceConfiguration config) { // var result = new XDataServiceConfiguration(); // if (!config.IsNullOrDefault()) { // result.PagingConfiguration.DefaultPageSize = config.PagingConfiguration.DefaultPageSize; result.PagingConfiguration.MaxAvailablePageSize = config.PagingConfiguration.MaxAvailablePageSize; result.PagingConfiguration.MinAvailablePageSize = config.PagingConfiguration.MinAvailablePageSize; } // return result; } // /// // /// provide a way to Order an IQueryable by string FieldName ... // /// // /// // /// // /// // /// // public static IOrderedQueryable OrderBy( // this IQueryable query, // string propertyName // ) // { // // // var entityType = typeof(TSource); // // // // Create x => x.PropName // var propertyInfo = entityType.GetProperty(propertyName); // if (propertyInfo.IsNull()) // { // XException.InvalidArgs.Throw(); // } // // // ParameterExpression arg = Expression.Parameter(entityType, "x"); // MemberExpression property = Expression.Property(arg, propertyName); // var selector = Expression.Lambda(property, new ParameterExpression[] { arg }); // // // // Get System.Linq.Queryable.OrderBy() method. // var enumarableType = typeof(System.Linq.Queryable); // var method = enumarableType.GetMethods() // .Where(m => m.Name == "OrderBy" && m.IsGenericMethodDefinition) // .Where(m => // { // // // var parameters = m.GetParameters().ToList(); // // // // Put more restriction here to ensure selecting the right overload // return parameters.Count == 2; // overload that has 2 parameters // }).Single(); // // // // The linq's OrderBy has two generic types, which provided here ... // MethodInfo genericMethod = method // .MakeGenericMethod(entityType, propertyInfo.PropertyType); // /* // Call query.OrderBy(selector), with query and selector: x=> x.PropName // Note that we pass the selector as Expression to the method and we don't compile it. // By doing so EF can extract "order by" columns and generate SQL for it. // */ // var newQuery = (IOrderedQueryable)genericMethod // .Invoke(genericMethod, new object[] { query, selector }); // // // return newQuery; // } // /// // /// provide a way to Order an IEnumerable by string FieldName ... // /// // /// // /// // /// // /// // public static IOrderedEnumerable OrderBy( // this IEnumerable query, // string propertyName // ) // { // // // var entityType = typeof(TSource); // var propertyInfo = entityType.GetProperty(propertyName); // if (propertyInfo.IsNull()) // { // XException.InvalidArgs.Throw(); // } // // // return query.OrderBy(x => propertyInfo.GetValue(x, null)); // } // /// // /// provide a way to Order an IQueryable by string FieldName ... // /// // /// // /// // /// // /// // public static IOrderedQueryable OrderByDescending( // this IQueryable query, // string propertyName // ) // { // // // var entityType = typeof(TSource); // // // // Create x => x.PropName // var propertyInfo = entityType.GetProperty(propertyName); // if (propertyInfo.IsNull()) // { // XException.InvalidArgs.Throw(); // } // // // ParameterExpression arg = Expression.Parameter(entityType, "x"); // MemberExpression property = Expression.Property(arg, propertyName); // var selector = Expression.Lambda(property, new ParameterExpression[] { arg }); // // // // Get System.Linq.Queryable.OrderBy() method. // var enumarableType = typeof(System.Linq.Queryable); // var method = enumarableType.GetMethods() // .Where(m => m.Name == "OrderByDescending" && m.IsGenericMethodDefinition) // .Where(m => // { // // // var parameters = m.GetParameters().ToList(); // // // // Put more restriction here to ensure selecting the right overload // return parameters.Count == 2; // overload that has 2 parameters // }).Single(); // // // // The linq's OrderBy has two generic types, which provided here ... // MethodInfo genericMethod = method // .MakeGenericMethod(entityType, propertyInfo.PropertyType); // /* // Call query.OrderBy(selector), with query and selector: x=> x.PropName // Note that we pass the selector as Expression to the method and we don't compile it. // By doing so EF can extract "order by" columns and generate SQL for it. // */ // var newQuery = (IOrderedQueryable)genericMethod // .Invoke(genericMethod, new object[] { query, selector }); // // // return newQuery; // } // /// // /// provide a way to Order an IEnumerable by string FieldName ... // /// // /// // /// // /// // /// // public static IOrderedEnumerable OrderByDescending( // this IEnumerable query, // string propertyName // ) // { // // // var entityType = typeof(TSource); // var propertyInfo = entityType.GetProperty(propertyName); // if (propertyInfo.IsNull()) // { // XException.InvalidArgs.Throw(); // } // // // return query.OrderByDescending(x => propertyInfo.GetValue(x, null)); // } } }