Skip to content

Instantly share code, notes, and snippets.

@deanebarker
Last active April 2, 2024 15:29
Show Gist options
  • Save deanebarker/1641988d7b83362e41983e767e215ddc to your computer and use it in GitHub Desktop.
Save deanebarker/1641988d7b83362e41983e767e215ddc to your computer and use it in GitHub Desktop.
A POC of providing personalization data with content area items in an API response
[ServiceConfiguration(typeof(IPropertyConverterProvider), Lifecycle = ServiceInstanceScope.Singleton)]
public class CustomPropertyConverterProvider : IPropertyConverterProvider
{
public int SortOrder => 200;
public IPropertyConverter Resolve(PropertyData propertyData)
{
if (propertyData is PropertyContentArea)
{
return new PersonalizedContentAreaPropertyConverter();
}
return null;
}
}
[ServiceConfiguration(typeof(IPropertyConverter), Lifecycle = ServiceInstanceScope.Singleton)]
public class PersonalizedContentAreaPropertyConverter : IPropertyConverter
{
public IPropertyModel Convert(PropertyData propertyData, ConverterContext converterContext)
{
var model = new PersonalizedContentAreaPropertyModel(propertyData as PropertyContentArea, converterContext);
return model;
}
}
public class PersonalizedContentAreaPropertyModel(PropertyContentArea propertyContentArea, ConverterContext converterContext) : ContentAreaPropertyModel(propertyContentArea, converterContext)
{
public object PersonalizedData
{
get
{
var vgRepo = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
var items = ((ContentArea)PropertyDataProperty.Value).Items;
var personalizedData = new List<object>();
foreach (var item in items)
{
personalizedData.Add(new
{
contentLink = item.ContentLink,
allowedRoles = item.AllowedRoles?.Select(ar => vgRepo.Load(Guid.Parse(ar)).Name)
});
}
return personalizedData;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment