Created
October 10, 2018 20:16
-
-
Save websterian/576b9b9655b245811d5b0d8a979f1a2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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