Esempio n. 1
0
        /// <summary>
        /// Prepare paged category product list model
        /// </summary>
        /// <param name="searchModel">Category product search model</param>
        /// <param name="category">Category</param>
        /// <returns>Category product list model</returns>
        public virtual CategoryProductListModel PrepareCategoryProductListModel(CategoryProductSearchModel searchModel, Category category)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            //get product categories
            var productCategories = _categoryService.GetProductCategoriesByCategoryId(category.Id,
                                                                                      showHidden: true,
                                                                                      pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);

            //prepare grid model
            var model = new CategoryProductListModel
            {
                //fill in model values from the entity
                Data = productCategories.Select(productCategory => new CategoryProductModel
                {
                    Id                = productCategory.Id,
                    CategoryId        = productCategory.CategoryId,
                    ProductId         = productCategory.ProductId,
                    ProductName       = _productService.GetProductById(productCategory.ProductId)?.Name,
                    IsFeaturedProduct = productCategory.IsFeaturedProduct,
                    DisplayOrder      = productCategory.DisplayOrder
                }),
                Total = productCategories.TotalCount
            };

            return(model);
        }
        /// <summary>
        /// Prepare paged category product list model
        /// </summary>
        /// <param name="searchModel">Category product search model</param>
        /// <param name="category">Category</param>
        /// <returns>Category product list model</returns>
        public virtual CategoryProductListModel PrepareCategoryProductListModel(CategoryProductSearchModel searchModel, Category category)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            //get product categories
            var productCategories = _categoryService.GetProductCategoriesByCategoryId(category.Id,
                                                                                      showHidden: true,
                                                                                      pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);

            //prepare grid model
            var model = new CategoryProductListModel
            {
                Data = productCategories.Select(productCategory =>
                {
                    //fill in model values from the entity
                    var categoryProductModel = productCategory.ToModel <CategoryProductModel>();

                    //fill in additional values (not existing in the entity)
                    categoryProductModel.ProductName = _productService.GetProductById(productCategory.ProductId)?.Name;

                    return(categoryProductModel);
                }),
                Total = productCategories.TotalCount
            };

            return(model);
        }