private IQueryable <IncomeEntity> ApplySorting(IQueryable <IncomeEntity> sql, ISortableQuery <IncomeOverviewSortType> query) { var sortDescriptor = query.SortDescriptor; if (sortDescriptor == null) { sortDescriptor = new SortDescriptor <IncomeOverviewSortType>(IncomeOverviewSortType.ByWhen); } switch (sortDescriptor.Type) { case IncomeOverviewSortType.ByAmount: sql = sql.OrderBy(sortDescriptor.Direction, o => o.Amount); break; case IncomeOverviewSortType.ByDescription: sql = sql.OrderBy(sortDescriptor.Direction, o => o.Description); break; case IncomeOverviewSortType.ByWhen: sql = sql.OrderBy(sortDescriptor.Direction, o => o.When); break; default: throw Ensure.Exception.NotSupported(sortDescriptor.Type.ToString()); } return(sql); }
/// <summary> /// Converts a IQuery concrete implementation into a query string parameter collection /// </summary> /// <typeparam name="T">The concrete type of the implementation</typeparam> /// <param name="entity">The query object to parse</param> /// <returns>A collection of query string parameters</returns> public static IEnumerable <QueryParam> ToQueryParams <T>(this T entity) where T : class, IQuery { if (entity == null) { yield break; } CheckRequiredPropertiesOnInterface(entity); foreach (PropertyInfo prop in Utility.GetAllProperiesOfObject(entity)) { string name = prop.Name; object gotten = prop.GetGetMethod().Invoke(entity, null); CheckRequiredPropertyOnMember(entity, prop, gotten, false); if (gotten == null) { continue; } if (gotten is IEnumerable <string> ) { IEnumerable <string> values = ((IEnumerable <string>)gotten).Where(v => !v.NullOrEmpty()); string value = string.Join(";", values); if (!value.NullOrEmpty()) { yield return(new QueryParam(name, value)); } } else if (gotten is DateTime) { string value = ((DateTime)gotten).ToUnixTime().ToString(); if (!value.NullOrEmpty()) { yield return(new QueryParam(name, value)); } } else if (gotten is Enum) { string value; var alt = ((Enum)gotten).GetCustomAttribute <EnumMemberAttribute>(); if (alt != null && !alt.Value.NullOrEmpty()) { value = alt.Value; } else { value = Enum.GetName(gotten.GetType(), gotten); } if (entity is ISortableQuery && gotten is QuerySortEnum) { var allowed = prop.GetCustomAttribute <AllowedSortValuesAttribute>(); if (allowed != null && !allowed.Values.Contains((QuerySortEnum)gotten)) { string message = Error.SortValueConstraint; throw new ApiException(message.FormatWith(name, value, Error.BadRequestParameter)); } ISortableQuery sortable = (ISortableQuery)entity; var allowedType = ((Enum)gotten).GetCustomAttribute <AllowedRangeTypeAttribute>(); if (allowedType != null) { Type expected = allowedType.Target; if (expected == null && (sortable.Min != null || sortable.Max != null)) { string message = Error.SortMinMaxConstraint; throw new ApiException(message.FormatWith(name, value, Error.BadRequestParameter)); } if (expected != null) { CheckTypeConstrain("Min", sortable.Min, expected); CheckTypeConstrain("Max", sortable.Max, expected); } } } yield return(new QueryParam(name, value)); } else { string value = gotten.ToString(); if (!value.NullOrEmpty()) { yield return(new QueryParam(name, value)); } } } }