Skip to content

Instantly share code, notes, and snippets.

View trnktms's full-sized avatar
:octocat:

Tamás Tárnok trnktms

:octocat:
View GitHub Profile
@trnktms
trnktms / Startup.cs
Last active February 29, 2024 07:54
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
// Overwrite the default ApiContentResponseBuilder to resolve partial layouts
services.AddSingleton<IApiContentResponseBuilder, ExtendedApiContentResponseBuilder>();
services.AddSingleton<IComponentResolver, HeaderResolver>();
services.AddSingleton<IComponentResolver, FooterResolver>();
public class ExtendedApiContentResponseBuilder : LayoutBuilderBase<IApiContentResponse>, IApiContentResponseBuilder
{
private readonly IApiContentRouteBuilder _apiContentRouteBuilder;
public ExtendedApiContentResponseBuilder(
IApiContentNameProvider apiContentNameProvider,
IApiContentRouteBuilder apiContentRouteBuilder,
IOutputExpansionStrategyAccessor outputExpansionStrategyAccessor,
IEnumerable<IComponentResolver> componentResolvers,
IApiPublishedContentCache apiPublishedContentCache)
public abstract class LayoutBuilderBase<T> : ApiContentBuilderBase<T> where T : IApiContent
{
private const string LayoutPropertyKey = "layout";
protected readonly IEnumerable<IComponentResolver> ComponentResolvers;
protected readonly IApiPublishedContentCache ApiPublishedContentCache;
protected LayoutBuilderBase(
IApiContentNameProvider apiContentNameProvider,
IApiContentRouteBuilder apiContentRouteBuilder,
public interface IComponentResolver
{
int Order { get; }
IPublishedContent ResolveContent(IPublishedContent content);
}
public class FooterResolver : IComponentResolver
{
public int Order => 100;
private readonly IApiPublishedContentCache _apiPublishedContentCache;
public FooterResolver(IApiPublishedContentCache apiPublishedContentCache)
{
_apiPublishedContentCache = apiPublishedContentCache;
}
public class HeaderResolver : IComponentResolver
{
public int Order => -100;
private readonly IApiPublishedContentCache _apiPublishedContentCache;
public HeaderResolver(IApiPublishedContentCache apiPublishedContentCache)
{
_apiPublishedContentCache = apiPublishedContentCache;
}
public class FallbackDeliveryApiJsonTypeResolver : DeliveryApiJsonTypeResolver
{
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
var typeInfo = base.GetTypeInfo(type, options);
if (typeInfo.PolymorphismOptions != null)
{
typeInfo.PolymorphismOptions.UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType;
}
public class ExtendedApiContentResponse : ApiContentResponse, IApiContentResponse
{
public ExtendedApiContentResponse(
Guid id,
string name,
string contentType,
DateTime createDate,
DateTime updateDate,
IApiContentRoute route,
IDictionary<string, object?> properties,
@trnktms
trnktms / Startup.cs
Last active September 27, 2023 09:21
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers().AddJsonOptions(Constants.JsonOptionsNames.DeliveryApi, options =>
{
// To resolve System.Text.Json polymorphic type resolving issue for the query controller
{
"system": {
"shared": {
"addToCartToasterText": "Product added to cart",
"removeFromCartToasterText": "Product removed from cart",
"removeFromCartButtonLabel": "Remove from cart",
"addToCartButtonLabel": "Add to cart",
"offBadgeLabel": "Off",
"productsBaseUrl": [
{