/// <summary> /// Convert <see cref="ListOptions"/> to dictionary for use in a route value parameter scenario. This overload substitutes existing values with the ones /// found inside the replacements object. /// </summary> /// <typeparam name="TReplacements"></typeparam> /// <param name="options"></param> /// <param name="replacements"></param> /// <returns></returns> public static IDictionary <string, object> ToDictionary <TReplacements>(this ListOptions options, TReplacements replacements) where TReplacements : class { if (null == replacements) { throw new ArgumentNullException(nameof(replacements)); } var overrides = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase).Merge(replacements, typeof(TReplacements)); var sortings = options.GetSortings(); var sortKey = nameof(options.Sort).ToLower(); if (overrides.ContainsKey(sortKey)) { var sort = overrides[sortKey].ToString(); if (sortings.Any(s => s.Path.Equals(sort))) { overrides[sortKey] = sortings.Select(s => s.Path.Equals(sort) ? s.NextState() : s).Where(s => s.HasValue).Select(s => s.Value).ToUriString(); } else { overrides[sortKey] = sortings.Union(new[] { SortByClause.Parse(sort) }).ToUriString(); } } return(options.ToDictionary().Merge(overrides, null)); }
/// <summary> /// Overrides the ConvertFrom method of TypeConverter. /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <returns></returns> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { return(SortByClause.Parse((string)value)); } return(base.ConvertFrom(context, culture, value)); }
/// <summary> /// Break the Sort parameter into multiple sort by clauses. /// Takes the Name-,Date+ etc... and enumerates it. /// </summary> /// <returns></returns> public IEnumerable <SortByClause> GetSortings() { var list = (Sort ?? string.Empty).Split(','); foreach (var item in list) { if (string.IsNullOrWhiteSpace(item)) { continue; } var sortBy = SortByClause.Parse(item); if (SortRedirects.ContainsKey(sortBy.Path)) { sortBy = new SortByClause(SortRedirects[sortBy.Path], sortBy.Direction, sortBy.DataType); } yield return(sortBy); } }