diff --git a/src/Altinn.Broker.API/Helpers/ValidateUseManifestFileShim.cs b/src/Altinn.Broker.API/Helpers/ValidateUseManifestFileShim.cs new file mode 100644 index 00000000..e92df78a --- /dev/null +++ b/src/Altinn.Broker.API/Helpers/ValidateUseManifestFileShim.cs @@ -0,0 +1,32 @@ +using System.ComponentModel.DataAnnotations; + +namespace Altinn.Broker.Helpers +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public class ValidateUseManifestFileShim : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + var useManifestFileShimProperty = validationContext.ObjectType.GetProperty("UseManifestFileShim"); + var externalServiceCodeLegacyProperty = validationContext.ObjectType.GetProperty("ExternalServiceCodeLegacy"); + var externalServiceEditionCodeLegacyProperty = validationContext.ObjectType.GetProperty("ExternalServiceEditionCodeLegacy"); + var useManifestFileShimValue = (bool?)useManifestFileShimProperty.GetValue(validationContext.ObjectInstance, null); + var externalServiceCodeLegacyValue = externalServiceCodeLegacyProperty.GetValue(validationContext.ObjectInstance, null); + var externalServiceEditionCodeLegacyValue = externalServiceEditionCodeLegacyProperty.GetValue(validationContext.ObjectInstance, null); + + if (useManifestFileShimValue == true) + { + if (externalServiceCodeLegacyValue == null || (externalServiceCodeLegacyValue is string strValue && string.IsNullOrEmpty(strValue))) + { + return new ValidationResult("ExternalServiceCodeLegacy must be set and not be an empty string."); + } + + if (externalServiceEditionCodeLegacyValue == null || (externalServiceEditionCodeLegacyValue is int intValue && intValue == 0)) + { + return new ValidationResult("ExternalServiceEditionCodeLegacy must be set and not be zero."); + } + } + return ValidationResult.Success; + } + } +} \ No newline at end of file diff --git a/src/Altinn.Broker.API/Models/ResourceExt.cs b/src/Altinn.Broker.API/Models/ResourceExt.cs index 52888db9..0df9f4ac 100644 --- a/src/Altinn.Broker.API/Models/ResourceExt.cs +++ b/src/Altinn.Broker.API/Models/ResourceExt.cs @@ -1,10 +1,14 @@ using System.Text.Json.Serialization; +using Altinn.Broker.Helpers; + namespace Altinn.Broker.Models; /// /// API input model for file initialization. /// +[ValidateUseManifestFileShim] + public class ResourceExt { diff --git a/src/Altinn.Broker.Application/ConfigureResource/ConfigureResourceHandler.cs b/src/Altinn.Broker.Application/ConfigureResource/ConfigureResourceHandler.cs index e4817d1c..35e030cc 100644 --- a/src/Altinn.Broker.Application/ConfigureResource/ConfigureResourceHandler.cs +++ b/src/Altinn.Broker.Application/ConfigureResource/ConfigureResourceHandler.cs @@ -61,24 +61,14 @@ public async Task> Process(ConfigureResourceRequest request, if (request.UseManifestFileShim is not null) { var updateManifestFileShimResult = await UpdateUseManifestFileShim(resource, request.UseManifestFileShim.Value, cancellationToken); + await resourceRepository.UpdateExternalServiceCodeLegacy(resource.Id, request.ExternalServiceCodeLegacy, cancellationToken); + await resourceRepository.UpdateExternalServiceEditionCodeLegacy(resource.Id, request.ExternalServiceEditionCodeLegacy, cancellationToken); + if (updateManifestFileShimResult.IsT1) { return updateManifestFileShimResult.AsT1; } } - if (request.UseManifestFileShim == true) - { - if (!string.IsNullOrEmpty(request.ExternalServiceCodeLegacy) && request.ExternalServiceEditionCodeLegacy is not null && request.ExternalServiceEditionCodeLegacy != 0) - { - await resourceRepository.UpdateExternalServiceCodeLegacy(resource.Id, request.ExternalServiceCodeLegacy, cancellationToken); - await resourceRepository.UpdateExternalServiceEditionCodeLegacy(resource.Id, request.ExternalServiceEditionCodeLegacy, cancellationToken); - } - else - { - throw new ArgumentException("ExternalServiceCodeLegacy and ExternalServiceEditionCodeLegacy must be set when UseManifestFileShim is true"); - } - - } return Task.CompletedTask; }