Returns the unit of work save changes result

This commit is contained in:
BinaryAlley committed 2026-09-26 12:59:17 +03:00
1 parent f3e465255e
commit 64ef3a1e22
141 files changed
+1388 -1151

No files matched your search

@@ -57,23 +57,26 @@ public class ReorderLibraryArtworkProvidersCommandHandler : ICommandHandler<Reor
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
// An authenticated request must always carry a user identity.
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// admins can reorder the artwork providers of any library; for everyone else, only their own libraries
// Admins can reorder the artwork providers of any library; for everyone else, only their own libraries.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(command.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
// Load the artwork provider configurations that are currently registered for the library, so that only providers it actually has are reordered.
Result<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.ArtworkProviderConfigurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
// Index the configurations by plugin id, so that each requested plugin can be matched without repeatedly searching the list.
Dictionary<Guid, LibraryArtworkProviderConfigurationEntity> configurationsByPluginId = getConfigurationsResult.Value.ToDictionary(configuration => configuration.PluginId);
// Walk the requested order and assign each provider the 1-based rank that matches its position in the command, so the persisted order follows the user's choice.
for (int rank = 0; rank < command.PluginIds.Count; rank++)
{
Guid pluginId = command.PluginIds[rank];
@@ -86,7 +89,10 @@ public class ReorderLibraryArtworkProvidersCommandHandler : ICommandHandler<Reor
}
}
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// Persist all the rank changes in a single save, so that the reorder is applied atomically.
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
if (saveChangesResult.IsFailure)
return saveChangesResult.Errors;
return Result.Success;
}
}
@@ -56,23 +56,26 @@ public class ReorderLibraryMetadataProvidersCommandHandler : ICommandHandler<Reo
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
// An authenticated request must always carry a user identity.
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// admins can reorder the metadata providers of any library; for everyone else, only their own libraries
// Admins can reorder the metadata providers of any library; for everyone else, only their own libraries.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(command.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
// Load the metadata provider configurations that are currently registered for the library, so that only providers it actually has are reordered.
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
// Index the configurations by plugin id, so that each requested plugin can be matched without repeatedly searching the list.
Dictionary<Guid, LibraryMetadataProviderConfigurationEntity> configurationsByPluginId = getConfigurationsResult.Value.ToDictionary(configuration => configuration.PluginId);
// Walk the requested order and assign each provider the 1-based rank that matches its position in the command, so the persisted order follows the user's choice.
for (int rank = 0; rank < command.PluginIds.Count; rank++)
{
Guid pluginId = command.PluginIds[rank];
@@ -85,7 +88,10 @@ public class ReorderLibraryMetadataProvidersCommandHandler : ICommandHandler<Reo
}
}
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// Persist all the rank changes in a single save, so that the reorder is applied atomically.
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
if (saveChangesResult.IsFailure)
return saveChangesResult.Errors;
return Result.Success;
}
}
@@ -56,18 +56,19 @@ public class SetLibraryArtworkProviderEnabledCommandHandler : ICommandHandler<Se
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
// An authenticated request must always carry a user identity.
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// admins can configure the artwork providers of any library; for everyone else, only their own libraries
// Admins can configure the artwork providers of any library; for everyone else, only their own libraries.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(command.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
// Look up the configuration of this provider for the library, so that an existing one is toggled instead of duplicated.
Result<LibraryArtworkProviderConfigurationEntity?> getConfigurationResult = await _unitOfWork.ArtworkProviderConfigurationRepository.GetByLibraryAndPluginIdAsync(command.LibraryId, command.PluginId, cancellationToken).ConfigureAwait(false);
if (getConfigurationResult.IsFailure)
return getConfigurationResult.Errors;
@@ -75,12 +76,13 @@ public class SetLibraryArtworkProviderEnabledCommandHandler : ICommandHandler<Se
LibraryArtworkProviderConfigurationEntity configuration;
if (getConfigurationResult.Value is not null)
{
// The provider is already configured for the library, so only its enabled state changes, keeping its existing rank in the display order.
configuration = getConfigurationResult.Value;
configuration.IsEnabled = command.IsEnabled;
}
else
{
// compute the rank to assign to the new configuration, appending it after the existing ones
// Compute the rank to assign to the new configuration, appending it after the existing ones.
Result<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.ArtworkProviderConfigurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
@@ -98,10 +100,14 @@ public class SetLibraryArtworkProviderEnabledCommandHandler : ICommandHandler<Se
};
}
// Insert the new configuration, or update the existing one, and persist the change.
Result<Updated> upsertResult = await _unitOfWork.ArtworkProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
if (upsertResult.IsFailure)
return upsertResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
if (saveChangesResult.IsFailure)
return saveChangesResult.Errors;
return Result.Success;
}
}
@@ -56,18 +56,19 @@ public class SetLibraryMetadataProviderEnabledCommandHandler : ICommandHandler<S
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
// An authenticated request must always carry a user identity.
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// admins can configure the metadata providers of any library; for everyone else, only their own libraries
// Admins can configure the metadata providers of any library; for everyone else, only their own libraries.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(command.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
// Look up the configuration of this provider for the library, so that an existing one is toggled instead of duplicated.
Result<LibraryMetadataProviderConfigurationEntity?> getConfigurationResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryAndPluginIdAsync(command.LibraryId, command.PluginId, cancellationToken).ConfigureAwait(false);
if (getConfigurationResult.IsFailure)
return getConfigurationResult.Errors;
@@ -75,12 +76,13 @@ public class SetLibraryMetadataProviderEnabledCommandHandler : ICommandHandler<S
LibraryMetadataProviderConfigurationEntity configuration;
if (getConfigurationResult.Value is not null)
{
// The provider is already configured for the library, so only its enabled state changes, keeping its existing rank in the display order.
configuration = getConfigurationResult.Value;
configuration.IsEnabled = command.IsEnabled;
}
else
{
// compute the rank to assign to the new configuration, appending it after the existing ones
// Compute the rank to assign to the new configuration, appending it after the existing ones.
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
@@ -98,10 +100,14 @@ public class SetLibraryMetadataProviderEnabledCommandHandler : ICommandHandler<S
};
}
// Insert the new configuration, or update the existing one, and persist the change.
Result<Updated> upsertResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
if (upsertResult.IsFailure)
return upsertResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
if (saveChangesResult.IsFailure)
return saveChangesResult.Errors;
return Result.Success;
}
}
@@ -49,7 +49,11 @@ public class UpdatePluginSettingsCommandHandler : ICommandHandler<UpdatePluginSe
Result<Updated> updateResult = await _unitOfWork.PluginRepository.UpdateSettingsAsync(command.PluginId, settingsJson, cancellationToken).ConfigureAwait(false);
if (updateResult.IsFailure)
return updateResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
if (saveChangesResult.IsFailure)
return saveChangesResult.Errors;
return Result.Success;
}
}