using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Commerce.EntityViews; | |
using Sitecore.Framework.Pipelines; | |
using Sitecore.Services.Examples.Entities.Entities; | |
namespace Sitecore.Services.Examples.Entities.Pipelines.Blocks | |
{ | |
[PipelineDisplayName("DoActionAddBrandBlock")] | |
public class DoActionAddBrandBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext> | |
{ | |
private readonly CommerceCommander _commerceCommander; | |
public DoActionAddBrandBlock(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("AddBrand", StringComparison.OrdinalIgnoreCase)) | |
{ | |
return await Task.FromResult(entityView); | |
} | |
var brand = new Brand(); | |
var displayNameProperty = entityView.Properties.FirstOrDefault(p => p.Name.Equals("DisplayName", StringComparison.OrdinalIgnoreCase)); | |
if(displayNameProperty?.Value != null) | |
{ | |
brand.DisplayName = displayNameProperty.Value.ToString(); | |
} | |
var nameProperty = entityView.Properties.FirstOrDefault(p => p.Name.Equals("Name", StringComparison.OrdinalIgnoreCase)); | |
if (nameProperty?.Value != null) | |
{ | |
brand.Name = nameProperty.Value.ToString(); | |
brand.Id = CommerceEntity.IdPrefix<Brand>() + brand.Name; | |
} | |
var descriptionProperty = entityView.Properties.FirstOrDefault(p => p.Name.Equals("Description", StringComparison.OrdinalIgnoreCase)); | |
if (descriptionProperty?.Value != null) | |
{ | |
brand.Description = descriptionProperty.Value.ToString(); | |
} | |
//TODO : Create a command for persisting brand | |
var result = await _commerceCommander.Pipeline<IPersistEntityPipeline>().Run(new PersistEntityArgument(brand), context); | |
if (result.Entity != null && string.IsNullOrEmpty(result.Entity.Id) != true) | |
{ | |
var list = new List<string> | |
{ | |
(result.Entity as Brand)?.Id | |
}; | |
await _commerceCommander.Pipeline<IAddListEntitiesPipeline>().Run(new ListEntitiesArgument(list, CommerceEntity.ListName<Brand>()), context); | |
} | |
return await Task.FromResult(entityView); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment