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

feat(subform): Add GET and PUT endpoints for datamodel metadata #14275

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions backend/src/Designer/Controllers/DatamodelsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Studio.DataModeling.Validator.Json;
using Altinn.Studio.Designer.Filters;
using Altinn.Studio.Designer.Helpers;
Expand Down Expand Up @@ -248,6 +249,27 @@ public async Task<IActionResult> UseXsdFromRepo(string org, string repository, s
}
}

/// <summary>
/// Gets the model metadata for a given model.
/// </summary>
[HttpGet("datamodel/{modelName}/metadata")]
[UseSystemTextJson]
public async Task<IActionResult> GetModelMetadata(string org, string repository, string modelName)
{
var dataTypeConfiguration = await _schemaModelService.GetModelMetadata(org, repository, modelName);
return Ok(dataTypeConfiguration);
}

/// <summary>
/// Updates the model metadata for a given model.
/// </summary>
[HttpPut("datamodel/{modelName}/metadata")]
[UseSystemTextJson]
public async Task UpdateModelMetadata(string org, string repository, string modelName, [FromBody] DataType dataType)
{
await _schemaModelService.UpdateModelMetadata(org, repository, modelName, dataType);
}

private static string GetFileNameFromUploadedFile(IFormFile thefile)
{
return ContentDispositionHeaderValue.Parse(new StringSegment(thefile.ContentDisposition)).FileName.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SchemaModelService : ISchemaModelService
private readonly IXmlSchemaToJsonSchemaConverter _xmlSchemaToJsonSchemaConverter;
private readonly IJsonSchemaToXmlSchemaConverter _jsonSchemaToXmlSchemaConverter;
private readonly IModelMetadataToCsharpConverter _modelMetadataToCsharpConverter;
private readonly IApplicationMetadataService _applicationMetadataService;

/// <summary>
/// Initializes a new instance of the <see cref="SchemaModelService"/> class.
Expand All @@ -59,20 +60,23 @@ public class SchemaModelService : ISchemaModelService
/// <param name="jsonSchemaToXmlSchemaConverter">
/// Class for converting Json schemas to Xml schemas.</param>
/// <param name="modelMetadataToCsharpConverter">C# model generator</param>
/// <param name="applicationMetadataService"></param>
public SchemaModelService(
IAltinnGitRepositoryFactory altinnGitRepositoryFactory,
ILoggerFactory loggerFactory,
ServiceRepositorySettings serviceRepositorySettings,
IXmlSchemaToJsonSchemaConverter xmlSchemaToJsonSchemaConverter,
IJsonSchemaToXmlSchemaConverter jsonSchemaToXmlSchemaConverter,
IModelMetadataToCsharpConverter modelMetadataToCsharpConverter)
IModelMetadataToCsharpConverter modelMetadataToCsharpConverter,
IApplicationMetadataService applicationMetadataService)
{
_altinnGitRepositoryFactory = altinnGitRepositoryFactory;
_loggerFactory = loggerFactory;
_serviceRepositorySettings = serviceRepositorySettings;
_xmlSchemaToJsonSchemaConverter = xmlSchemaToJsonSchemaConverter;
_jsonSchemaToXmlSchemaConverter = jsonSchemaToXmlSchemaConverter;
_modelMetadataToCsharpConverter = modelMetadataToCsharpConverter;
_applicationMetadataService = applicationMetadataService;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -431,5 +435,20 @@ private bool NamespaceNeedsToBeSeparated(ApplicationMetadata application,
{
return application.DataTypes.All(d => d.AppLogic?.ClassRef != $"Altinn.App.Models.{csharpModelName}");
}

public async Task<DataType> GetModelMetadata(string org, string app, string modelName)
{
ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app);
DataType dataType = applicationMetadata.DataTypes.Find((dataType) => dataType.Id == modelName);
return dataType;
}

public async Task UpdateModelMetadata(string org, string app, string modelName, DataType dataType)
{
ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app);
applicationMetadata.DataTypes.RemoveAll((dataType) => dataType.Id == modelName);
applicationMetadata.DataTypes.Add(dataType);
await _applicationMetadataService.UpdateApplicationMetaDataLocally(org, app, applicationMetadata);
}
}
}
10 changes: 10 additions & 0 deletions backend/src/Designer/Services/Interfaces/ISchemaModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,15 @@ public interface ISchemaModelService
/// <param name="cancellationToken">An <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>Returns the model metadata</returns>
Task<ModelMetadata> GenerateModelMetadataFromJsonSchema(AltinnRepoEditingContext altinnRepoEditingContext, string relativeFilePath, CancellationToken cancellationToken = default);

/// <summary>
/// Gets the model metadata for a given model.
/// </summary>
Task<DataType> GetModelMetadata(string org, string app, string modelName);

/// <summary>
/// Updates the model metadata for a given model.
/// </summary>
Task UpdateModelMetadata(string org, string app, string modelName, DataType dataType);
}
}
Loading
Loading