Skip to content

Commit

Permalink
Backend endpoints for instance creation and dataelement operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondyr committed Nov 26, 2024
1 parent a629a0e commit d40f94c
Show file tree
Hide file tree
Showing 41 changed files with 966 additions and 436 deletions.
116 changes: 49 additions & 67 deletions backend/src/Designer/Controllers/Preview/DataController.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Studio.Designer.Filters;
using Altinn.Studio.Designer.Helpers;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Models.Preview;
using Altinn.Studio.Designer.Services.Implementation;
using Altinn.Studio.Designer.Services.Interfaces;
using Altinn.Studio.Designer.Services.Interfaces.Preview;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Altinn.Studio.Designer.Controllers.Preview
{
[Authorize]
[AutoValidateAntiforgeryToken]
[Route("{org:regex(^(?!designer))}/{app:regex(^(?!datamodels$)[[a-z]][[a-z0-9-]]{{1,28}}[[a-z0-9]]$)}/instances/{partyId}/{instanceGuid}/data")]
public class DataController(IHttpContextAccessor httpContextAccessor,
IPreviewService previewService,
ISchemaModelService schemaModelService,
public class DataController(
IInstanceService instanceService,
IDataService dataService
) : Controller
{
[HttpGet("{dataGuid}")]
public ActionResult Get([FromRoute] Guid dataGuid)
[UseSystemTextJson]
public ActionResult Get(
[FromRoute] Guid dataGuid
)
{
JsonNode dataItem = dataService.GetDataElement(dataGuid);
return Ok(dataItem);
}

[HttpPost]
public ActionResult Post(
[UseSystemTextJson]
public ActionResult<DataElement> Post(
[FromRoute] int partyId,
[FromRoute] Guid instanceGuid,
[FromQuery] string dataType
)
{
DataElement dataElement = dataService.CreateDataElement(partyId, instanceGuid, dataType);
instanceService.AddDataElement(instanceGuid, dataElement);
return Created("link-to-app-placeholder", dataElement);
}

[HttpPatch]
[UseSystemTextJson]
public ActionResult<DataPatchResponseMultiple> PatchMultiple(
[FromRoute] string org,
[FromRoute] string app,
[FromRoute] int partyId,
[FromRoute] Guid instanceGuid,
[FromBody] DataPatchRequestMultiple dataPatch
)
{
Instance instance = instanceService.GetInstance(instanceGuid);

List<DataModelPairResponse> newDataModels = [];
dataPatch.Patches.ForEach(patch =>
{
JsonNode dataItem = dataService.PatchDataElement(patch.DataElementId, patch.Patch);
newDataModels.Add(new DataModelPairResponse(patch.DataElementId, dataItem));
});

return Ok(new DataPatchResponseMultiple()
{
ValidationIssues = [],
NewDataModels = newDataModels,
Instance = instance,
});
}

[HttpPatch("{dataGuid}")]
[UseSystemTextJson]
public ActionResult<DataPatchResponse> Patch(
Expand All @@ -60,71 +83,30 @@ [FromBody] DataPatchRequest dataPatch
});
}

[HttpDelete("{dataTypeId}")]
public ActionResult DeleteAttachment([FromRoute] Guid dataGuid)
[HttpDelete("{dataGuid}")]
public ActionResult<DataPostResponse> Delete(
[FromRoute] Guid instanceGuid,
[FromRoute] Guid dataGuid
)
{
instanceService.RemoveDataElement(instanceGuid, dataGuid);
return Ok();
}

[HttpGet("{dataGuid}/validate")]
public ActionResult ValidateInstanceForData([FromRoute] Guid dataGuid)
{
return Ok(new List<string>());
}

[HttpPost("{dataTypeId}/tags")]
public ActionResult UpdateTagsForAttachment([FromBody] string tag)
{
return Created("link-to-app-placeholder", tag);
}

[HttpGet(PreviewService.MockDataTaskId)]
public async Task<ActionResult> GetDefaultFormData(
[FromRoute] string org,
[FromRoute] string app,
[FromRoute] int partyId,
CancellationToken cancellationToken
public ActionResult ValidateInstanceForData(
[FromRoute] Guid dataGuid
)
{
string developer = AuthenticationHelper.GetDeveloperUserName(httpContextAccessor.HttpContext);
string refererHeader = Request.Headers.Referer;
string layoutSetName = GetSelectedLayoutSetInEditorFromRefererHeader(refererHeader);
DataType dataType = await previewService.GetDataTypeForLayoutSetName(org, app, developer, layoutSetName, cancellationToken);
// For apps that does not have a datamodel
if (dataType == null)
{
Instance mockInstance = await previewService.GetMockInstance(org, app, developer, partyId, layoutSetName, cancellationToken);
return Ok(mockInstance.Id);
}
string modelPath = $"/App/models/{dataType.Id}.schema.json";
string decodedPath = Uri.UnescapeDataString(modelPath);
string formData = await schemaModelService.GetSchema(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer), decodedPath, cancellationToken);
return Ok(formData);
return Ok(new List<string>());
}

[HttpPut(PreviewService.MockDataTaskId)]
public async Task<ActionResult> UpdateFormData(
[FromRoute] string org,
[FromRoute] string app,
[FromRoute] int partyId,
CancellationToken cancellationToken
[HttpPost("{dataGuid}/tags")]
public ActionResult UpdateTagsForAttachment(
[FromBody] string tag
)
{
return await GetDefaultFormData(org, app, partyId, cancellationToken);
}

[HttpPatch(PreviewService.MockDataTaskId)]
public ActionResult PatchFormData()
{
return Ok();
}

private static string GetSelectedLayoutSetInEditorFromRefererHeader(string refererHeader)
{
Uri refererUri = new(refererHeader);
string layoutSetName = HttpUtility.ParseQueryString(refererUri.Query)["selectedLayoutSet"];

return string.IsNullOrEmpty(layoutSetName) ? null : layoutSetName;
return Created("link-to-app-placeholder", tag);
}
}
}
Expand Down
Loading

0 comments on commit d40f94c

Please sign in to comment.