Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.EntityViews;
using Sitecore.Framework.Pipelines;
namespace Sitecore.Services.Examples.Entities.Pipelines.Blocks
{
[PipelineDisplayName("DoActionRemoveBrandBlock")]
public class DoActionRemoveBrandBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
{
private readonly CommerceCommander _commerceCommander;
public DoActionRemoveBrandBlock(CommerceCommander commerceCommander)
{
_commerceCommander = commerceCommander;
}
public override async Task<EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
{
var action = entityView?.Action;
if (string.IsNullOrEmpty(action) || !entityView.Action.Equals("RemoveBrand", StringComparison.OrdinalIgnoreCase))
{
return await Task.FromResult(entityView);
}
else
{
var id = (string.IsNullOrEmpty(entityView.EntityId) ? entityView.ItemId : entityView.EntityId);
if (!string.IsNullOrEmpty(id))
{
//TODO : Add delete command, dont call pipeline directly
await _commerceCommander.Pipeline<IDeleteEntityPipeline>().Run(new DeleteEntityArgument(id), context);
return await Task.FromResult(entityView);
}
else
{
var validationError = context.GetPolicy<KnownResultCodes>().ValidationError;
var objArray = new object[] { "EntityId" };
await context.CommerceContext.AddMessage(validationError, "InvalidOrMissingPropertyValue", objArray, "Invalid or missing value for property 'EntityId'.");
return await Task.FromResult(entityView);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.