Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure ImageDeliveryChannels are ordered #862

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/protagonist/API/Features/Image/Ingest/AssetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ public class AssetProcessor
Func<Asset, Task>? requiresReingestPreSave = null,
CancellationToken cancellationToken = default)
{
Asset? existingAsset;
try
{
existingAsset = await assetRepository.GetAsset(assetBeforeProcessing.Asset.Id, true);
var existingAsset = await assetRepository.GetAsset(assetBeforeProcessing.Asset.Id, true);

if (existingAsset == null)
{
Expand Down Expand Up @@ -98,7 +97,7 @@ public class AssetProcessor
return new ProcessAssetResult
{
Result = ModifyEntityResult<Asset>.Failure(
"Delivery channels are required when updating an existing Asset via PUT",
"Delivery channels are required when updating an existing Asset",
WriteResult.BadRequest
)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ public class DeliveryChannelProcessor
deliveryChannelsBeforeProcessing, existingAsset != null);
return deliveryChannelChanged;
}
catch (InvalidOperationException)
catch (InvalidOperationException ioEx)
{
throw new APIException("Failed to match delivery channel policy")
{
StatusCode = 400
};
throw new BadRequestException("Failed to match delivery channel policy", ioEx);
}
}

Expand Down Expand Up @@ -160,7 +157,7 @@ private async Task<bool> SetImageDeliveryChannels(Asset asset, DeliveryChannelsB
private async Task<DeliveryChannelPolicy> GetDeliveryChannelPolicy(Asset asset, DeliveryChannelsBeforeProcessing deliveryChannel)
{
DeliveryChannelPolicy deliveryChannelPolicy;
if (deliveryChannel.Policy.IsNullOrEmpty())
if (string.IsNullOrEmpty(deliveryChannel.Policy))
{
deliveryChannelPolicy = await defaultDeliveryChannelRepository.MatchDeliveryChannelPolicyForChannel(
asset.MediaType!, asset.Space, asset.Customer, deliveryChannel.Channel);
Expand Down Expand Up @@ -201,11 +198,9 @@ private async Task AddDeliveryChannelsForMediaType(Asset asset)
if (matchedDeliveryChannels.Any(x => x.Channel == AssetDeliveryChannels.None) &&
matchedDeliveryChannels.Count != 1)
{
throw new APIException("An asset can only be automatically assigned a delivery channel of type 'None' when it is the only one available. " +
"Please check your default delivery channel configuration.")
{
StatusCode = 400
};
throw new BadRequestException(
"An asset can only be automatically assigned a delivery channel of type 'None' when it is the only one available. " +
"Please check your default delivery channel configuration.");
}

foreach (var deliveryChannel in matchedDeliveryChannels)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using DLCS.Model.Assets;
using DLCS.Model.Policies;
using DLCS.Repository.Assets;
using Microsoft.EntityFrameworkCore;
using Test.Helpers.Data;
using Test.Helpers.Integration;

namespace DLCS.Repository.Tests.Assets;

[Trait("Category", "Database")]
[Collection(DatabaseCollection.CollectionName)]
public class AssetQueryXTests
{
private readonly DlcsContext dbContext;

public AssetQueryXTests(DlcsDatabaseFixture dbFixture)
{
dbContext = dbFixture.DbContext;
dbFixture.CleanUp();
}

[Fact]
public async Task IncludeDeliveryChannelsWithPolicy_ReturnsDeliveryChannels_ByOrderOfChannel()
{
var assetId = AssetIdGenerator.GetAssetId();
await dbContext.ImageDeliveryChannels.AddRangeAsync(
new()
{
ImageId = assetId, Channel = "gamma",
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.ImageDefault
},
new()
{
ImageId = assetId, Channel = "alpha",
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.ImageDefault
},
new()
{
ImageId = assetId, Channel = "beta",
DeliveryChannelPolicyId = KnownDeliveryChannelPolicies.ImageDefault
});
await dbContext.Images.AddTestAsset(assetId);
await dbContext.SaveChangesAsync();

// Act
var result = await dbContext.Images
.Where(i => i.Id == assetId)
.IncludeDeliveryChannelsWithPolicy()
.ToListAsync();

// Assert
result.Single().ImageDeliveryChannels.Should().BeInAscendingOrder(idc => idc.Channel);
}
}
8 changes: 4 additions & 4 deletions src/protagonist/DLCS.Repository/Assets/AssetQueryX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IQueryable<Asset> AsOrderedAssetQuery(this IQueryable<Asset> asset
/// The orderBy field can be the API version of property or the full property version.
/// Defaults to "Created" field ordering if no field specified.
/// </summary>
public static IQueryable<Asset> AsOrderedAssetQuery(this IQueryable<Asset> assetQuery, string? orderBy,
private static IQueryable<Asset> AsOrderedAssetQuery(this IQueryable<Asset> assetQuery, string? orderBy,
bool descending = false)
{
var field = GetPropertyName(orderBy);
Expand Down Expand Up @@ -57,7 +57,6 @@ private static string GetPropertyName(string? orderBy)
};
}


// Create an Expression from the PropertyName.
// I think Split(".") handles nested properties maybe - seems unnecessary but from an SO post
// "x" means nothing when creating the Parameter, it's just used for debug messages
Expand Down Expand Up @@ -115,11 +114,12 @@ private static LambdaExpression CreateExpression(Type type, string propertyName)

return filtered;
}

/// <summary>
/// Include asset delivery channels and their associated policies.
/// </summary>
public static IQueryable<Asset> IncludeDeliveryChannelsWithPolicy(this IQueryable<Asset> assetQuery)
=> assetQuery.Include(a => a.ImageDeliveryChannels)
=> assetQuery
.Include(a => a.ImageDeliveryChannels.OrderBy(idc => idc.Channel))
.ThenInclude(dc => dc.DeliveryChannelPolicy);
}
Loading