コード例 #1
0
        /// <summary>
        /// Prepare paged list model of products that use the specification attribute
        /// </summary>
        /// <param name="searchModel">Search model of products that use the specification attribute</param>
        /// <param name="specificationAttribute">Specification attribute</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the list model of products that use the specification attribute
        /// </returns>
        public virtual async Task <SpecificationAttributeProductListModel> PrepareSpecificationAttributeProductListModelAsync(
            SpecificationAttributeProductSearchModel searchModel, SpecificationAttribute specificationAttribute)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

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

            //get products
            var products = await _specificationAttributeService.GetProductsBySpecificationAttributeIdAsync(
                specificationAttributeId : specificationAttribute.Id,
                pageIndex : searchModel.Page - 1, pageSize : searchModel.PageSize);

            //prepare list model
            var model = new SpecificationAttributeProductListModel().PrepareToGrid(searchModel, products, () =>
            {
                //fill in model values from the entity
                return(products.Select(product =>
                {
                    var specificationAttributeProductModel = product.ToModel <SpecificationAttributeProductModel>();
                    specificationAttributeProductModel.ProductId = product.Id;
                    specificationAttributeProductModel.ProductName = product.Name;
                    specificationAttributeProductModel.SpecificationAttributeId = specificationAttribute.Id;

                    return specificationAttributeProductModel;
                }));
            });

            return(model);
        }