Skip to content

Commit

Permalink
Merge pull request #1587 from solliancenet/aa-attachment-filter-080
Browse files Browse the repository at this point in the history
(0.8.0) Fix issue with missing resource references in resource provider
  • Loading branch information
ciprianjichici authored Aug 22, 2024
2 parents 5c556be + ba1acc1 commit 0ead3fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ private async Task<List<AttachmentDetail>> Filter(string serializedAction)
StatusCodes.Status400BadRequest);
if (resourceFilter.ObjectIDs is {Count: > 0})
{
var resourceNames = resourceFilter.ObjectIDs
.Select(id => this.GetResourcePath(id, false).ResourceTypeInstances.Last().ResourceId!)
.ToList();
var filteredReferences =
await _resourceReferenceStore!.GetResourceReferences(r => !string.IsNullOrWhiteSpace(r.ObjectId) && resourceFilter.ObjectIDs.Contains(r.ObjectId));
await _resourceReferenceStore!.GetResourceReferences(resourceNames);

return filteredReferences.Select(r => new AttachmentDetail
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,40 @@ public async Task LoadResourceReferences()
/// </summary>
/// <param name="predicate">The predicate to filter the resource references.</param>
/// <returns></returns>
/// <remarks>
/// This method is not safe in scenarios where multiple instances of a resource provider are running at the same time.
/// </remarks>
public async Task<IEnumerable<T>> GetResourceReferences(Func<T, bool> predicate)
{
await _lock.WaitAsync();
try
{
return _resourceReferences.Values.Where(predicate);
return _resourceReferences.Values.Where(rr => predicate(rr) && !rr.Deleted);
}
finally
{
_lock.Release();
}
}

/// <summary>
/// Gets the resource references for the specified resource names.
/// </summary>
/// <param name="resourceNames">The list of resource names for which the references should be retrieved.</param>
/// <returns></returns>
public async Task<IEnumerable<T>> GetResourceReferences(IEnumerable<string> resourceNames)
{
await _lock.WaitAsync();
try
{
if (resourceNames.Except(_resourceReferences.Keys).Any())
{
// Some of the resource references are missing, so we need to load them.
await LoadAndMergeResourceReferences();
}
return resourceNames
.Select(rn => _resourceReferences[rn])
.Where(rr => !rr.Deleted);
}
finally
{
Expand All @@ -131,12 +159,15 @@ public async Task<IEnumerable<T>> GetResourceReferences(Func<T, bool> predicate)
/// Gets all resource references in the store.
/// </summary>
/// <returns>A <see cref="List{T}"/> contain</returns>
/// <remarks>
/// This method is not safe in scenarios where multiple instances of a resource provider are running at the same time.
/// </remarks>
public async Task<List<T>> GetAllResourceReferences()
{
await _lock.WaitAsync();
try
{
return [.. _resourceReferences.Values];
return [.. _resourceReferences.Values.Where(rr => !rr.Deleted)];
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ protected async Task<List<ResourceProviderGetResult<T>>> LoadResources<T>(Resour
return null;
}

/// <summary>
/// Loads a resource based on its name.
/// </summary>
/// <typeparam name="T">The type of resource to load.</typeparam>
/// <param name="resourceName">The name of the resource.</param>
/// <returns>The loaded resource.</returns>
/// <exception cref="ResourceProviderException"></exception>
protected async Task<T?> LoadResource<T>(string resourceName) where T : ResourceBase
{
try
Expand Down

0 comments on commit 0ead3fb

Please sign in to comment.