Exemple #1
0
        public async Task <IActionResult> Catalog(CatalogModel model)
        {
            // Fill main fields
            model.FillFields();

            bool hasCustomTypes = false;

            if (model.CategoryName != null)
            {
                hasCustomTypes = model.TryToFillCustomFields(model.CategoryName + "Description");
            }

            // Select by categories
            Expression <Func <ProductInfo, bool> > selector = null;

            if (model.CategoryName == null || !CatalogModel.TypeToNameDict.ContainsKey(model.CategoryName))
            {
                selector = (pr) => true;
            }
            else
            {
                selector = (pr) => pr.TableName == model.CategoryName + "Descriptions";
            }

            var products = (await db.GetProductInfosAsync()).Where(selector);

            // Filter by custom values
            if (model.CustomValuesInfo != null && model.CustomValuesInfo.FromValues != null && model.CustomValuesInfo.ToValues != null)
            {
                var descriptions = ControllerHelper.FilterCatalogValues(await db.GetDescriptionsAsync(model.CategoryName + "Descriptions"),
                                                                        ReflectionHelper.FoundType(model.CategoryName + "Description"),
                                                                        model.CustomValuesInfo);

                var ids = descriptions.Select(d => d.Id).OrderBy(id => id).ToHashSet();
                products = products.Where(pr => ids.Contains(pr.Id));
            }
            // Filter by main values
            if (model.StandartValuesInfo != null && model.StandartValuesInfo.FromValues != null && model.StandartValuesInfo.ToValues != null)
            {
                products = ControllerHelper.FilterCatalogValues(products, typeof(ProductInfo), model.StandartValuesInfo);
            }

            if (!hasCustomTypes)
            {
                model.CustomValuesInfo.Types = new Type[0];
            }
            // Sort by parameter
            if (model.SortedBy != null)
            {
                products = ControllerHelper.OrderValuesByParameter(products, model.SortedBy, model.SortedByDescending);
            }

            model.Count    = products.Count();
            model.Products = products;

            return(View(model));
        }