Ejemplo n.º 1
0
        /// <summary>
        /// Sorts the elements of an array by a given attribute of an element in the array.
        /// {% assign sorted = pages | sort:"date:desc;name" %}
        /// </summary>
        /// <param name="input"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static object SortList(object input, string sort)
        {
            var               retVal            = input;
            IEnumerable       enumerable        = retVal as IEnumerable;
            IMutablePagedList muttablePagedList = input as IMutablePagedList;

            var sortInfos = SortInfo.Parse(sort).ToList();

            if (muttablePagedList != null)
            {
                muttablePagedList.Slice(muttablePagedList.PageNumber, muttablePagedList.PageSize, sortInfos);
            }
            if (enumerable != null)
            {
                //Queryable.Cast<T>(input).OrderBySortInfos(sortInfos) call by reflection
                var        queryable      = enumerable.AsQueryable();
                var        elementType    = enumerable.GetType().GetEnumerableType();
                MethodInfo castMethodInfo = typeof(Queryable).GetMethods().Where(x => x.Name == "Cast" && x.IsGenericMethod).First();
                castMethodInfo = castMethodInfo.MakeGenericMethod(new Type[] { elementType });

                var genericQueryable = castMethodInfo.Invoke(null, new object[] { queryable });

                var orderBySortInfosMethodInfo = typeof(IQueryableExtensions).GetMethod("OrderBySortInfos");
                orderBySortInfosMethodInfo = orderBySortInfosMethodInfo.MakeGenericMethod(new Type[] { elementType });
                retVal = orderBySortInfosMethodInfo.Invoke(null, new object[] { genericQueryable, sortInfos.ToArray() });
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        public virtual Search ToLiquidSearch(IMutablePagedList <storefrontModel.Product> products, WorkContext workContext)
        {
            var factory = ServiceLocator.Current.GetInstance <ShopifyModelFactory>();
            var result  = factory.CreateSearch();

            result.Performed = true;
            result.Terms     = workContext.CurrentProductSearchCriteria.Keyword;

            result.Results = new MutablePagedList <dotLiquid.Drop>((pageNumber, pageSize, sortInfos) =>
            {
                products.Slice(pageNumber, pageSize, sortInfos);
                return(new StaticPagedList <dotLiquid.Drop>(products.Select(x => x.ToShopifyModel()), products));
            });

            if (workContext.Aggregations != null)
            {
                result.AllTags = new TagCollection(new MutablePagedList <Tag>((pageNumber, pageSize, sortInfos) =>
                {
                    workContext.Aggregations.Slice(pageNumber, pageSize, sortInfos);
                    var tags = workContext.Aggregations
                               .Where(a => a.Items != null)
                               .SelectMany(a => a.Items.Select(item => item.ToShopifyModel(a.Field, a.Label)));
                    return(new StaticPagedList <Tag>(tags, workContext.Aggregations));
                }, workContext.Aggregations.PageNumber, workContext.Aggregations.PageSize));
            }

            return(result);
        }
Ejemplo n.º 3
0
        public static Search ToShopifyModel(this IMutablePagedList <Storefront.Model.Catalog.Product> products, string terms)
        {
            var retVal = new Search
            {
                Results = new MutablePagedList <Drop>((pageNumber, pageSize) =>
                {
                    products.Slice(pageNumber, pageSize);
                    return(new StaticPagedList <Drop>(products.Select(x => x.ToShopifyModel()).OfType <Drop>(), products));
                }),
                Terms     = terms,
                Performed = true
            };

            return(retVal);
        }
Ejemplo n.º 4
0
 public void Slice(int pageNumber, int pageSize, IEnumerable <SortInfo> sortInfos)
 {
     _mutablePagedList.Slice(pageNumber, pageSize, sortInfos);
 }
Ejemplo n.º 5
0
 public void Slice(int pageNumber, int pageSize, IEnumerable <SortInfo> sortInfos, NameValueCollection @params = null)
 {
     _mutablePagedList.Slice(pageNumber, pageSize, sortInfos, @params);
 }