Skip to content

Commit

Permalink
Added DataConext Methods
Browse files Browse the repository at this point in the history
Refactorred Constructors to  take on Config for cache time out.
  • Loading branch information
bluemodus-brandon committed Jul 13, 2024
1 parent 39417b0 commit d34043e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
39 changes: 35 additions & 4 deletions src/XperienceCommunity.DataContext/ContentItemContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CMS.ContentEngine;
using CMS.Helpers;
using CMS.Websites.Routing;
using XperienceCommunity.DataContext.Configurations;
using XperienceCommunity.DataContext.Extensions;
using XperienceCommunity.DataContext.Interfaces;

Expand All @@ -17,8 +18,11 @@ namespace XperienceCommunity.DataContext
private readonly ContentQueryExecutor<T> _contentQueryExecutor;
private readonly string _contentType;
private readonly IWebsiteChannelContext _websiteChannelContext;
private readonly XperienceDataContextConfig _config;
private bool? _includeTotalCount;
private string? _language;
private int? _linkedItemsDepth;
private (int?, int?) _offset;
private IQueryable<T>? _query;
private bool? _useFallBack;

Expand All @@ -28,8 +32,9 @@ namespace XperienceCommunity.DataContext
/// <param name="websiteChannelContext">The website channel context.</param>
/// <param name="cache">The cache service.</param>
/// <param name="contentQueryExecutor"></param>
/// <param name="config"></param>
public ContentItemContext(IWebsiteChannelContext websiteChannelContext,
IProgressiveCache cache, ContentQueryExecutor<T> contentQueryExecutor)
IProgressiveCache cache, ContentQueryExecutor<T> contentQueryExecutor, XperienceDataContextConfig config)
{
ArgumentNullException.ThrowIfNull(cache);
ArgumentNullException.ThrowIfNull(websiteChannelContext);
Expand All @@ -38,9 +43,9 @@ public ContentItemContext(IWebsiteChannelContext websiteChannelContext,
websiteChannelContext;
_cache = cache;
_contentQueryExecutor = contentQueryExecutor;

_config = config;
_contentType = typeof(T).GetContentTypeName() ??
throw new InvalidOperationException("Content type name could not be determined.");
throw new InvalidOperationException("Content type name could not be determined.");
}

/// <summary>
Expand All @@ -63,13 +68,29 @@ public ContentItemContext(IWebsiteChannelContext websiteChannelContext,
return result.FirstOrDefault();
}

public IDataContext<T> IncludeTotalCount(bool includeTotalCount)
{
_includeTotalCount = includeTotalCount;
return this;
}

public IDataContext<T> InLanguage(string language, bool useFallBack = true)
{
_language = language;
_useFallBack = useFallBack;
return this;
}

public IDataContext<T> Offset(int start, int count)
{
if (start >= 0 && count >= 0)
{
_offset = (start, count);
}

return this;
}

/// <summary>
/// Orders the content items based on the specified key selector.
/// </summary>
Expand Down Expand Up @@ -192,6 +213,16 @@ private ContentItemQueryBuilder BuildQuery(Expression expression, int? topN = nu
subQuery.TopN(topN.Value);
}
if (_includeTotalCount.HasValue)
{
subQuery.IncludeTotalCount();
}
if (_offset is { Item1: not null, Item2: not null })
{
subQuery.Offset(_offset.Item1.Value, _offset.Item2.Value);
}
var visitor = new ContentItemQueryExpressionVisitor(subQuery);
visitor.Visit(expression);
Expand Down Expand Up @@ -243,7 +274,7 @@ private async Task<T> GetOrCacheAsync<T>(Func<Task<T>> executeFunc, string cache
return await executeFunc();
}

var cacheSettings = new CacheSettings(10, true, cacheKey);
var cacheSettings = new CacheSettings(_config.CacheTimeOut, true, cacheKey);

return await _cache.LoadAsync(async cs =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static IEnumerable<object> ExtractValues(object? value)
return ExtractValues(collectionValue);
}

return new[] { value };
return [value];
}
}
}
18 changes: 17 additions & 1 deletion src/XperienceCommunity.DataContext/Interfaces/IDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,40 @@ public interface IDataContext<T> where T : class
/// Filters the items by language.
/// </summary>
/// <param name="language">The language code to filter items.</param>
/// <param name="useFallBack"></param>
/// <param name="useFallBack">Indicates whether to use fallback language if the specified language is not available.</param>
/// <returns>The current context for chaining.</returns>
IDataContext<T> InLanguage(string language, bool useFallBack = true);

/// <summary>
/// Orders the items based on the specified key selector.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="keySelector">The key selector to order items.</param>
/// <returns>The current context for chaining.</returns>
IDataContext<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelector);

/// <summary>
/// Offsets the items by the specified start index and count.
/// </summary>
/// <param name="start">The start index of the items to offset.</param>
/// <param name="count">The number of items to offset.</param>
/// <returns>The current context for chaining.</returns>
IDataContext<T> Offset(int start, int count);

/// <summary>
/// Limits the number of items.
/// </summary>
/// <param name="count">The maximum number of items to return.</param>
/// <returns>The current context for chaining.</returns>
IDataContext<T> Take(int count);

/// <summary>
/// Includes the total count of items in the query result.
/// </summary>
/// <param name="includeTotalCount">Indicates whether to include the total count of items in the query result.</param>
/// <returns>The current context for chaining.</returns>
IDataContext<T> IncludeTotalCount(bool includeTotalCount);

/// <summary>
/// Retrieves the content items asynchronously.
/// </summary>
Expand Down
35 changes: 33 additions & 2 deletions src/XperienceCommunity.DataContext/PageContentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CMS.Helpers;
using CMS.Websites;
using CMS.Websites.Routing;
using XperienceCommunity.DataContext.Configurations;
using XperienceCommunity.DataContext.Extensions;
using XperienceCommunity.DataContext.Interfaces;

Expand All @@ -14,14 +15,17 @@ namespace XperienceCommunity.DataContext
private readonly string _contentType;
private readonly PageContentQueryExecutor<T> _pageContentQueryExecutor;
private readonly IWebsiteChannelContext _websiteChannelContext;
private readonly XperienceDataContextConfig _config;
private string? _channelName;
private bool? _includeTotalCount;
private string? _language;
private int? _linkedItemsDepth;
private (int?, int?) _offset;
private PathMatch? _pathMatch;
private IQueryable<T>? _query;

public PageContentContext(IProgressiveCache cache, PageContentQueryExecutor<T> pageContentQueryExecutor,
IWebsiteChannelContext websiteChannelContext)
IWebsiteChannelContext websiteChannelContext, XperienceDataContextConfig config)
{
ArgumentNullException.ThrowIfNull(cache);
ArgumentNullException.ThrowIfNull(websiteChannelContext);
Expand All @@ -32,6 +36,7 @@ public PageContentContext(IProgressiveCache cache, PageContentQueryExecutor<T> p
throw new InvalidOperationException("Content type name could not be determined.");
_pageContentQueryExecutor = pageContentQueryExecutor;
_websiteChannelContext = websiteChannelContext;
_config = config;
}

public async Task<T?> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate,
Expand All @@ -54,12 +59,28 @@ public IPageContentContext<T> InChannel(string channelName)
return this;
}

public IDataContext<T> IncludeTotalCount(bool includeTotalCount)
{
_includeTotalCount = includeTotalCount;
return this;
}

public IDataContext<T> InLanguage(string language, bool useFallBack = true)
{
_language = language;
return this;
}

public IDataContext<T> Offset(int start, int count)
{
if (start >= 0 && count >= 0)
{
_offset = (start, count);
}

return this;
}

public IPageContentContext<T> OnPath(PathMatch pathMatch)
{
_pathMatch = pathMatch;
Expand Down Expand Up @@ -177,6 +198,16 @@ private ContentItemQueryBuilder BuildQuery(Expression expression, int? topN = nu
subQuery.TopN(topN.Value);
}
if (_includeTotalCount.HasValue)
{
subQuery.IncludeTotalCount();
}
if (_offset is { Item1: not null, Item2: not null })
{
subQuery.Offset(_offset.Item1.Value, _offset.Item2.Value);
}
var visitor = new ContentItemQueryExpressionVisitor(subQuery);
visitor.Visit(expression);
Expand Down Expand Up @@ -228,7 +259,7 @@ private async Task<T> GetOrCacheAsync<T>(Func<Task<T>> executeFunc, string cache
return await executeFunc();
}

var cacheSettings = new CacheSettings(10, true, cacheKey);
var cacheSettings = new CacheSettings(_config.CacheTimeOut, true, cacheKey);

return await _cache.LoadAsync(async cs =>
{
Expand Down

0 comments on commit d34043e

Please sign in to comment.