generated from Kentico/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from brandonhenricks/features/additional-reposi…
…tories Features/additional repositories
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
src/XperienceCommunity.DataRepository/Interfaces/IMediaFileRepository.cs
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
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
100
src/XperienceCommunity.DataRepository/MediaFileRepository.cs
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
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 | ||
); | ||
} | ||
} |