Beispiel #1
0
        /// <summary>
        /// Execute a mapping from the model to a new entity
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="model">Model to map from</param>
        /// <returns>Mapped entity</returns>
        public static TEntity ToEntity <TEntity>(this BaseNopEntityModel model) where TEntity : BaseEntity
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            return(model.Map <TEntity>());
        }
        /// <summary>
        /// Save entity use code
        /// </summary>
        /// <param name="model">Base Nop entity model</param>
        private void SaveEntityUseCode(BaseNopEntityModel model)
        {
            //ensure that received model is BaseNopEntityModel
            if (model == null)
            {
                return;
            }

            //get entity by received model
            var entity = model is CustomerModel ? (BaseEntity)_customerService.GetCustomerById(model.Id)
                : model is CustomerRoleModel ? (BaseEntity)_customerService.GetCustomerRoleById(model.Id)
                : model is ProductModel ? (BaseEntity)_productService.GetProductById(model.Id)
                : model is CheckoutAttributeModel ? (BaseEntity)_checkoutAttributeService.GetCheckoutAttributeById(model.Id)
                : null;

            if (entity == null)
            {
                return;
            }

            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return;
            }

            //ensure that Avalara tax provider is active
            if (!(_taxService.LoadActiveTaxProvider(_workContext.CurrentCustomer) is AvalaraTaxProvider))
            {
                return;
            }

            //whether there is a form value for the entity use code
            if (_httpContextAccessor.HttpContext.Request.Form.TryGetValue(AvalaraTaxDefaults.EntityUseCodeAttribute, out StringValues entityUseCodeValue) &&
                !StringValues.IsNullOrEmpty(entityUseCodeValue))
            {
                //save attribute
                var entityUseCode = !entityUseCodeValue.ToString().Equals(Guid.Empty.ToString()) ? entityUseCodeValue.ToString() : null;
                _genericAttributeService.SaveAttribute(entity, AvalaraTaxDefaults.EntityUseCodeAttribute, entityUseCode);
            }
        }