public override async Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
        {
            Condition.Requires(entityView).IsNotNull($"{Name}: The argument cannot be null.");

            // Only proceed if the right action was invoked
            if (string.IsNullOrEmpty(entityView.Action) || !entityView.Action.StartsWith("Edit-", StringComparison.OrdinalIgnoreCase))
            {
                return(entityView);
            }

            // Check if we have a sellable item on the context
            var sellableItem = context.CommerceContext.GetObject <SellableItem>(x => x.Id.Equals(entityView.EntityId));

            if (sellableItem == null)
            {
                return(entityView);
            }

            var applicableComponentTypes = await this.catalogSchemaCommander.GetApplicableComponentTypes(context.CommerceContext, sellableItem);

            var editedComponentType = applicableComponentTypes.SingleOrDefault(comp => entityView.Action == $"Edit-{comp.FullName}");

            if (editedComponentType != null)
            {
                // Get the component from the sellable item or its variation
                var editedComponent = catalogSchemaCommander.GetEditedComponent(sellableItem, entityView.ItemId, editedComponentType);

                await ValidateConstraints(entityView.Properties,
                                          editedComponentType, editedComponent, context);
            }

            return(entityView);
        }
示例#2
0
        public async override Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
        {
            Condition.Requires(entityView).IsNotNull($"{Name}: The argument cannot be null.");

            // Only proceed if the right action was invoked
            if (string.IsNullOrEmpty(entityView.Action) || !entityView.Action.StartsWith("Edit-", StringComparison.OrdinalIgnoreCase))
            {
                return(entityView);
            }

            // Get the sellable item from the context
            var sellableItem = context.CommerceContext.GetObject <SellableItem>(x => x.Id.Equals(entityView.EntityId));

            if (sellableItem == null)
            {
                return(entityView);
            }

            KnownResultCodes errorCodes = context.CommerceContext.GetPolicy <KnownResultCodes>();

            if (context.CommerceContext.AnyMessage(msg => msg.Code == errorCodes.ValidationError))
            {   // We found an error
                return(entityView);
            }

            var applicableComponentTypes = await this.catalogSchemaCommander.GetApplicableComponentTypes(context.CommerceContext, sellableItem);

            var editedComponentType = applicableComponentTypes.SingleOrDefault(comp => entityView.Action == $"Edit-{comp.FullName}");

            if (editedComponentType != null)
            {
                // Get the component from the sellable item or its variation
                var editedComponent = catalogSchemaCommander.GetEditedComponent(sellableItem, entityView.ItemId, editedComponentType);

                // Set the properties from the view on the component
                catalogSchemaCommander.SetPropertyValuesOnEditedComponent(entityView.Properties, editedComponentType, editedComponent, context.CommerceContext);

                // Persist changes
                await this.catalogSchemaCommander.Pipeline <IPersistEntityPipeline>().Run(new PersistEntityArgument(sellableItem), context);
            }

            return(entityView);
        }