Skip to content

Content Management

Details

NuGet CodeFandango.Common.Content

Overview

This package provides helper methods for working with the SquidEx content management system.

Instantiation

The expected way to use the IContentManager is through dependency injection. You can configure this using:

builder.Services.AddContentManager(builder.Configuration, opts => {
    opts.AppName = "appname"; // The same appname used in the Configuration extension.
});

This expects certain configuration options to be present. Specifically:

  • squidex section
    • appName - the Squidex App/Site Name
    • clientId - for authentication
    • clientSecret - for authentication
    • url - the URL to access the Squidex API.
  • ConnectionStrings
    • redis - a connection string for Redis for use in caching content
  • defaultContentCacheTtl - optional content cache TTL. Defaults to 30 minutes.

It's also possible to configure Converters for use with the Squidex Serializer Settings by doing this:

builder.Services.AddContentManager(builder.Configuration, opts => {
    opts.AppName = "appname"; // The same appname used in the Configuration extension.
    opts.Converters.Add(new SomeContentConverter());
});

Methods

Get a single content item

public async Task<TResult> GetAsync<TEntity, TData, TResult>(
        string id,
        Func<TEntity, TResult> map,
        TimeSpan? ttl = null, CancellationToken ct = default)
        where TEntity : Content<TData> where TData : class, new()

Retrieves a specific content item by its identifier, maps it to the desired result type, and caches the result.

Parameters

  • id - The unique identifier of the content item to retrieve.
  • map - A function that maps the retrieved content entity to the specified result type.
  • ttl - Optional time-to-live duration for the cached result. If null, a default value of 30 minutes or the defaultContentCacheTtl configuration value is used.
  • ct - A cancellation token to allow triggering early termination of the operation.
  • TEntity - The type of the content entity being retrieved.
  • TData - The type of the data contained within the content entity.
  • TResult - The type to which the content item will be mapped.

Get all content items

public async Task<IList<TResult>> GetAllAsync<TEntity, TData, TResult>(
        string cacheKey,
        Func<TEntity, TResult> map,
        string? entityType = null,
        TimeSpan? ttl = null, CancellationToken ct = default)
        where TEntity : Content<TData> where TData : class, new()

Retrieves all content items of the specified type, maps them to the desired result type, and caches the results.

Parameters

  • cacheKey - A unique key used to identify the cached data for this operation.
  • map - A function to map the retrieved content items to the specified result type.
  • entityType - An optional parameter to specify the type of content being retrieved. Defaults to the name of the entity type.
  • ttl - Optional time-to-live duration for the cached result. If null, a default value of 30 minutes or the defaultContentCacheTtl configuration value is used.
  • ct - A cancellation token to allow triggering early termination of the operation.
  • TEntity - The type of the content entity being retrieved.
  • TData - The type of the data contained within the content entity.
  • TResult - The type to which the content item will be mapped.

Examples

var result = await contentManager.GetAllAsync<Page, PageModel, PageDefinition>(
    "page-list",
    map => new PageDefinition
    {
        Slug = map.Data.Slug ?? map.Id,
        Title = map.Data.Title ?? "Title Unknown",
        Order = map.Data.Order
    },
    "page"
);
var posts = await contentManager.GetAllAsync<Post, PostModel, PostDto>(
    "posts",
    MapPost,
    "post");

private static PostDto MapPost(Post map)
{
    return new PostDto
    {
        Created = map.Created,
        Slug = map.Data.Slug,
        Title = map.Data.Title,
        Content = Markdown.ToHtml(map.Data.Content ?? "",
            new MarkdownPipelineBuilder().UseAdvancedExtensions().Build()),
        Tags = map.Data.Tags,
    };
}                

Query for content items

public async Task<IList<TResult>> QueryAsync<TEntity, TData, TResult>(
    string cacheKey,
    ContentQuery query,
    Func<TEntity, TResult> map,
    string? schemaName = null,
    TimeSpan? ttl = null, CancellationToken ct = default)
    where TEntity : Content<TData> where TData : class, new()

Queries content items from the specified schema, maps them to the desired result type, and caches the results.

Parameters

  • cacheKey - A unique key used to identify the cached data for this operation.
  • query - The content query.
  • map - A function to map the retrieved content items to the specified result type.
  • schemaName - An optional parameter to specify the type of content being retrieved. Defaults to the name of the entity type.
  • ttl - Optional time-to-live duration for the cached result. If null, a default value of 30 minutes or the defaultContentCacheTtl configuration value is used.
  • ct - A cancellation token to allow triggering early termination of the operation.
  • TEntity - The type of the content entity being retrieved.
  • TData - The type of the data contained within the content entity.
  • TResult - The type to which the content item will be mapped.

Examples

var dto = await contentManager.QueryAsync<Post, PostModel, PostDto>(
    slug,
    new ContentQuery { Filter = $"data/slug/iv eq '{slug}'" },
    MapPost, // See previous examples for the implementation
    "post"
);

Get distinct field values

public async Task<Dictionary<string, int>> GetDistinctFieldValuesAsync(string cacheKey, string schemaName,
        string field, TimeSpan? ttl = null, CancellationToken ct = default)
public async Task<Dictionary<string, int>> GetDistinctFieldValuesAsync(string cacheKey, string schemaName, 
        Func<DynamicContent, string[]> getTags, TimeSpan? ttl = null, CancellationToken ct = default)

Retrieves distinct field values for a specified schema and field, along with their occurrence counts, and caches the result.

  • cacheKey - A unique key used to identify the cached data for this operation.
  • schemaName - Specifies the type of content being retrieved.
  • field - The field of the schema we're interested in the values of.
  • getTags - A function to extract an array of tags from a given content item.
  • ttl - Optional time-to-live duration for the cached result. If null, a default value of 30 minutes or the defaultContentCacheTtl configuration value is used.
  • ct - A cancellation token to allow triggering early termination of the operation.

Examples

var tagList = await contentManager.GetDistinctFieldValuesAsync("post_tags", "Post", "tags");
var tagList = await contentManager.GetDistinctFieldValuesAsync("post_tags", "post", content =>
{
    var tags = content.Data["tags"]["iv"]?.ToObject<string[]>();
    return (tags != null ? tags.ToArray()! : [])!;
});