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("GetBrandDetailsViewBlock")] | |
public class GetBrandDetailsViewBlock : PipelineBlock<EntityView, EntityView, CommercePipelineExecutionContext> | |
{ | |
public override Task<EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context) | |
{ | |
Condition.Requires(entityView).IsNotNull($"{Name}: The argument cannot be null"); | |
var viewArg = context.CommerceContext.GetObject<EntityViewArgument>(); | |
var entityViewArgument = viewArg; | |
var viewName = entityViewArgument?.ViewName; | |
if (string.IsNullOrEmpty(viewName) || !viewArg.ViewName.Equals("Details", StringComparison.OrdinalIgnoreCase) && !viewArg.ViewName.Equals("Master", StringComparison.OrdinalIgnoreCase)) | |
{ | |
return Task.FromResult(entityView); | |
} | |
if (!viewArg.ForAction.Equals("AddBrand", StringComparison.OrdinalIgnoreCase) || !viewArg.ViewName.Equals("Details", StringComparison.OrdinalIgnoreCase)) | |
{ | |
var isEdit = viewArg.ForAction.Equals("EditBrand", StringComparison.OrdinalIgnoreCase); | |
if (!(viewArg.Entity is Brand) || !isEdit && !string.IsNullOrEmpty(viewArg.ForAction)) | |
{ | |
return Task.FromResult(entityView); | |
} | |
var brand = (Brand) viewArg.Entity; | |
if (!viewArg.ViewName.Equals("Master", StringComparison.OrdinalIgnoreCase)) | |
{ | |
return Task.FromResult(entityView); | |
} | |
var properties = entityView.Properties; | |
var viewProperty = properties.FirstOrDefault(p => p.Name.Equals("Name", StringComparison.OrdinalIgnoreCase)); | |
if (viewProperty != null) | |
{ | |
var viewProperty1 = viewProperty; | |
viewProperty1.RawValue = brand.Name; | |
var viewProperties = entityView.Properties; | |
var rawValue = viewProperties.FirstOrDefault(p => p.Name.Equals("DisplayName", StringComparison.OrdinalIgnoreCase)); | |
if (rawValue == null) | |
{ | |
var properties1 = entityView.Properties; | |
var viewProperty2 = new ViewProperty | |
{ | |
Name = "DisplayName", | |
RawValue = viewProperty.RawValue | |
}; | |
properties1.Add(viewProperty2); | |
} | |
else | |
{ | |
rawValue.RawValue = viewProperty.RawValue; | |
} | |
} | |
var detailsView = new EntityView | |
{ | |
EntityId = entityView.EntityId, | |
Name = "Details" | |
}; | |
entityView.ChildViews.Add(detailsView); | |
//Save | |
} | |
else | |
{ | |
var nameProperty = new ViewProperty | |
{ | |
Name = "Name", | |
UiType = "string" | |
}; | |
entityView.Properties.Add(nameProperty); | |
var displayNameProperty = new ViewProperty | |
{ | |
Name = "Displayname", | |
UiType = "string" | |
}; | |
entityView.Properties.Add(displayNameProperty); | |
var descriptionProperty = new ViewProperty | |
{ | |
Name = "Description", | |
UiType = "string" | |
}; | |
entityView.Properties.Add(descriptionProperty); | |
} | |
return Task.FromResult(entityView); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment