Created
September 27, 2023 07:28
-
-
Save trnktms/ca406c315891b1e5ac1d69752fc2d532 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
public class ExtendedApiContentResponseBuilder : ApiContentBuilderBase<IApiContentResponse>, IApiContentResponseBuilder | |
{ | |
private readonly IApiContentRouteBuilder _apiContentRouteBuilder; | |
private readonly IOutputExpansionStrategyAccessor _outputExpansionStrategyAccessor; | |
private readonly IApiPublishedContentCache _apiPublishedContentCache; | |
public ExtendedApiContentResponseBuilder( | |
IApiContentNameProvider apiContentNameProvider, | |
IApiContentRouteBuilder apiContentRouteBuilder, | |
IOutputExpansionStrategyAccessor outputExpansionStrategyAccessor, | |
IApiPublishedContentCache apiPublishedContentCache) | |
: base(apiContentNameProvider, apiContentRouteBuilder, outputExpansionStrategyAccessor) | |
{ | |
_apiContentRouteBuilder = apiContentRouteBuilder; | |
_outputExpansionStrategyAccessor = outputExpansionStrategyAccessor; | |
_apiPublishedContentCache = apiPublishedContentCache; | |
} | |
protected override IApiContentResponse Create(IPublishedContent content, string name, IApiContentRoute route, IDictionary<string, object?> properties) | |
{ | |
var cultures = GetCultures(content); | |
var system = GetSystem(); | |
return new ExtendedApiContentResponse( | |
content.Key, | |
name, | |
content.ContentType.Alias, | |
content.CreateDate, | |
content.UpdateDate, | |
route, | |
properties, | |
cultures, | |
system); | |
} | |
private IDictionary<string, object?>? GetSystem() | |
{ | |
var siteSettings = _apiPublishedContentCache.GetByRoute("/site-data/site-settings"); | |
if (siteSettings == null) | |
{ | |
return new Dictionary<string, object?>(0); | |
} | |
var properties = _outputExpansionStrategyAccessor.TryGetValue(out var outputExpansionStrategy) | |
? new Dictionary<string, object?> { { "shared", outputExpansionStrategy.MapContentProperties(siteSettings) } } | |
: new Dictionary<string, object?>(); | |
return properties; | |
} | |
/// <summary> | |
/// HACK: Copied from the original ApiContentResponseBuilder | |
/// </summary> | |
private Dictionary<string, IApiContentRoute> GetCultures(IPublishedContent content) | |
{ | |
var routesByCulture = new Dictionary<string, IApiContentRoute>(); | |
foreach (var publishedCultureInfo in content.Cultures.Values) | |
{ | |
if (publishedCultureInfo.Culture.IsNullOrWhiteSpace()) | |
{ | |
continue; | |
} | |
var cultureRoute = _apiContentRouteBuilder.Build(content, publishedCultureInfo.Culture); | |
if (cultureRoute == null) | |
{ | |
continue; | |
} | |
routesByCulture[publishedCultureInfo.Culture] = cultureRoute; | |
} | |
return routesByCulture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment