Skip to content

Instantly share code, notes, and snippets.

@websterian
Created October 10, 2018 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save websterian/69258ce70889ff5c6b043d14584b5186 to your computer and use it in GitHub Desktop.
Save websterian/69258ce70889ff5c6b043d14584b5186 to your computer and use it in GitHub Desktop.
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