Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Linq;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.EntityViews;
using Sitecore.Framework.Conditions;
using Sitecore.Framework.Pipelines;
using Sitecore.Services.Examples.Entities.Entities;
namespace Sitecore.Services.Examples.Entities.Pipelines.Blocks
{
[PipelineDisplayName("GetBrandsViewBlock")]
public class GetBrandsViewBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext>
{
private readonly IFindEntitiesInListPipeline _findEntitiesInListPipeline;
public GetBrandsViewBlock(IFindEntitiesInListPipeline findEntitiesInListPipeline)
{
_findEntitiesInListPipeline = findEntitiesInListPipeline;
}
public override async Task<EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
{
Condition.Requires(entityView).IsNotNull($"{Name}: The argument cannot be null.");
var entityViewArgument = context.CommerceContext.GetObjects<EntityViewArgument>().FirstOrDefault();
var viewName = entityViewArgument?.ViewName;
if (string.IsNullOrEmpty(viewName) || !entityViewArgument.ViewName.Equals("Brands", StringComparison.OrdinalIgnoreCase) && !entityViewArgument.ViewName.Equals("BrandsDashboard", StringComparison.OrdinalIgnoreCase))
{
return await Task.FromResult(entityView);
}
if (!entityViewArgument.ViewName.Equals("BrandsDashboard", StringComparison.OrdinalIgnoreCase))
{
await Task.FromResult(entityView);
}
var brandsView = new EntityView()
{
EntityId = string.Empty,
ItemId = string.Empty,
Name = "All Brands",
DisplayRank = 100
};
entityView.ChildViews.Add(brandsView);
brandsView.UiHint = "Table";
var brandsResult = await _findEntitiesInListPipeline.Run(new FindEntitiesInListArgument(typeof(Brand), CommerceEntity.ListName<Brand>(), 0, 2147483647), context);
foreach (var brandEntity in brandsResult.List.Items)
{
var brand = brandEntity as Brand;
var summaryEntityView = new EntityView()
{
EntityId = brand.Id,
ItemId = brand.Id,
DisplayName = brand.DisplayName,
Name = "Summary"
};
var viewProperties = summaryEntityView.Properties;
var viewProperty = new ViewProperty()
{
Name = "ItemId",
RawValue = brand.Id,
IsHidden = true,
IsReadOnly = true
};
viewProperties.Add(viewProperty);
var displayNameProperty = new ViewProperty()
{
Name = "Name",
RawValue = brand.Name,
IsReadOnly = true,
UiType = "EntityLink"
};
viewProperties.Add(displayNameProperty);
var descriptionProperty = new ViewProperty()
{
Name = "Displayname",
RawValue = brand.DisplayName,
IsReadOnly = true,
UiType = "string"
};
viewProperties.Add(descriptionProperty);
var dateCreatedProperty = new ViewProperty()
{
Name = "DateCreated",
RawValue = brand.DateCreated,
IsReadOnly = true
};
viewProperties.Add(dateCreatedProperty);
var dateUpdated = new ViewProperty()
{
Name = "DateUpdated",
RawValue = brand.DateUpdated,
IsReadOnly = true
};
viewProperties.Add(dateUpdated);
brandsView.ChildViews.Add(summaryEntityView);
}
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.