Skip to content

Commit

Permalink
Lagt til validering i resourceExt
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Todorov committed Nov 7, 2024
1 parent 1d64399 commit fcaff82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
32 changes: 32 additions & 0 deletions src/Altinn.Broker.API/Helpers/ValidateUseManifestFileShim.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
4 changes: 4 additions & 0 deletions src/Altinn.Broker.API/Models/ResourceExt.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Text.Json.Serialization;

using Altinn.Broker.Helpers;

namespace Altinn.Broker.Models;

/// <summary>
/// API input model for file initialization.
/// </summary>
[ValidateUseManifestFileShim]

public class ResourceExt
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,14 @@ public async Task<OneOf<Task, Error>> 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;
}

Expand Down

0 comments on commit fcaff82

Please sign in to comment.