Esempio n. 1
0
        /// <summary>
        /// Prepare paged customer attribute list model
        /// </summary>
        /// <param name="searchModel">Customer attribute search model</param>
        /// <returns>Customer attribute list model</returns>
        public virtual async Task <CustomerAttributeListModel> PrepareCustomerAttributeListModelAsync(CustomerAttributeSearchModel searchModel)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            //get customer attributes
            var customerAttributes = (await _customerAttributeService.GetAllCustomerAttributesAsync()).ToPagedList(searchModel);

            //prepare list model
            var model = await new CustomerAttributeListModel().PrepareToGridAsync(searchModel, customerAttributes, () =>
            {
                return(customerAttributes.SelectAwait(async attribute =>
                {
                    //fill in model values from the entity
                    var attributeModel = attribute.ToModel <CustomerAttributeModel>();

                    //fill in additional values (not existing in the entity)
                    attributeModel.AttributeControlTypeName = await _localizationService.GetLocalizedEnumAsync(attribute.AttributeControlType);

                    return attributeModel;
                }));
            });

            return(model);
        }
        /// <summary>
        /// Validates customer attributes
        /// </summary>
        /// <param name="attributesXml">Attributes in XML format</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the warnings
        /// </returns>
        public virtual async Task <IList <string> > GetAttributeWarningsAsync(string attributesXml)
        {
            var warnings = new List <string>();

            //ensure it's our attributes
            var attributes1 = await ParseCustomerAttributesAsync(attributesXml);

            //validate required customer attributes (whether they're chosen/selected/entered)
            var attributes2 = await _customerAttributeService.GetAllCustomerAttributesAsync();

            foreach (var a2 in attributes2)
            {
                if (!a2.IsRequired)
                {
                    continue;
                }

                var found = false;
                //selected customer attributes
                foreach (var a1 in attributes1)
                {
                    if (a1.Id != a2.Id)
                    {
                        continue;
                    }

                    var valuesStr = ParseValues(attributesXml, a1.Id);

                    found = valuesStr.Any(str1 => !string.IsNullOrEmpty(str1.Trim()));
                }

                if (found)
                {
                    continue;
                }

                //if not found
                var notFoundWarning = string.Format(await _localizationService.GetResourceAsync("ShoppingCart.SelectAttribute"), await _localizationService.GetLocalizedAsync(a2, a => a.Name));

                warnings.Add(notFoundWarning);
            }

            return(warnings);
        }