Skip to content

Commit

Permalink
Merge pull request #1 from brandonhenricks/features/additional-reposi…
Browse files Browse the repository at this point in the history
…tories

Features/additional repositories
  • Loading branch information
brandonhenricks authored Jan 9, 2025
2 parents bd206b5 + 67ae0bf commit a15b8cc
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/XperienceCommunity.DataRepository/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static IServiceCollection AddXperienceDataRepositories(this IServiceColle

services.TryAddScoped(typeof(IPageRepository<>), typeof(PageTypeRepository<>));

services.TryAddScoped<IMediaFileRepository, MediaFileRepository>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Immutable;

using CMS.MediaLibrary;

namespace XperienceCommunity.DataRepository.Interfaces;

/// <summary>
/// Interface for media file repository to handle media file operations.
/// </summary>
public interface IMediaFileRepository
{
/// <summary>
/// Retrieves a list of media files based on the provided GUIDs.
/// </summary>
/// <param name="mediaFileGuids">The GUIDs of the media files to retrieve.</param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains an immutable list of <see cref="MediaFileInfo"/>.</returns>
Task<ImmutableList<MediaFileInfo>> GetMediaFilesAsync(IEnumerable<Guid> mediaFileGuids,
CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves a list of media files based on the provided related items.
/// </summary>
/// <param name="items">The related items to retrieve media files from.</param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains an immutable list of <see cref="MediaFileInfo"/>.</returns>
Task<ImmutableList<MediaFileInfo>> GetAssetsFromRelatedItemsAsync(IEnumerable<AssetRelatedItem> items,
CancellationToken cancellationToken = default);
}
100 changes: 100 additions & 0 deletions src/XperienceCommunity.DataRepository/MediaFileRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Collections.Immutable;

using CMS.DataEngine;
using CMS.Helpers;
using CMS.MediaLibrary;

using XperienceCommunity.DataRepository.Interfaces;
using XperienceCommunity.DataRepository.Models;

namespace XperienceCommunity.DataRepository;

public sealed class MediaFileRepository : IMediaFileRepository
{
private readonly IProgressiveCache cache;
private readonly int cacheMinutes;

public MediaFileRepository(IProgressiveCache cache, RepositoryOptions options)
{
this.cache = cache;
cacheMinutes = options?.CacheMinutes ?? 10;
}

public async Task<ImmutableList<MediaFileInfo>> GetAssetsFromRelatedItemsAsync(IEnumerable<AssetRelatedItem> items,
CancellationToken cancellationToken = default)
{
var assetItems = items?.ToList() ?? [];

if (assetItems.Count == 0)
{
return [];
}

return await cache.LoadAsync(
async (cacheSettings, ct) =>
{
var results = (await new ObjectQuery<MediaFileInfo>()
.ForAssets(assetItems)
.GetEnumerableTypedResultAsync(cancellationToken: ct))
.ToList() ?? [];

string[] dependencyKeys = results
.Select(result => $"mediafile|{result.FileGUID}")
.ToArray();

cacheSettings.CacheDependency = CacheHelper.GetCacheDependency(dependencyKeys);

return results.ToImmutableList();
},
new CacheSettings(
cacheMinutes: cacheMinutes,
useSlidingExpiration: true,
cacheItemNameParts:
[
nameof(MediaFileRepository),
nameof(GetAssetsFromRelatedItemsAsync),
.. assetItems.OrderBy(item => item.Name).Select(item => item.Name) ?? [],
]
), cancellationToken
);
}

public async Task<ImmutableList<MediaFileInfo>> GetMediaFilesAsync(IEnumerable<Guid> mediaFileGuids,
CancellationToken cancellationToken = default)
{
var guidList = mediaFileGuids?.ToList() ?? [];

if (guidList.Count == 0)
{
return [];
}

return await cache.LoadAsync(
async (cacheSettings, ct) =>
{
var results = (await new ObjectQuery<MediaFileInfo>()
.WhereIn(nameof(MediaFileInfo.FileGUID), guidList)
.GetEnumerableTypedResultAsync(cancellationToken: ct))
?.ToList() ?? [];

string[] dependencyKeys = guidList
.Select(x => $"mediafile|{x}")
.ToArray();

cacheSettings.CacheDependency = CacheHelper.GetCacheDependency(dependencyKeys);

return results.ToImmutableList();
},
new CacheSettings(
cacheMinutes: cacheMinutes,
useSlidingExpiration: true,
cacheItemNameParts:
[
nameof(MediaFileRepository),
nameof(GetMediaFilesAsync),
guidList.GetHashCode(),
]
), cancellationToken
);
}
}

0 comments on commit a15b8cc

Please sign in to comment.