Skip to content

Commit

Permalink
Replicate logic from storage (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjololo authored Jun 15, 2023
1 parent dc9d451 commit c60ccdd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/Controllers/Storage/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public async Task<ActionResult<DataElementList>> GetMany(int instanceOwnerPartyI
/// <param name="instanceGuid">The id of the instance that the data element is associated with.</param>
/// <param name="dataType">The data type identifier for the data being uploaded.</param>
/// <param name="refs">An optional array of data element references.</param>
/// <param name="generatedFromIds">An optional array of generated from references containing data element ids.</param>
/// <param name="generatedFromTask">An optional id of the task the data element was generated from</param>
/// <returns>The metadata of the new data element.</returns>
[Authorize(Policy = AuthzConstants.POLICY_INSTANCE_WRITE)]
[HttpPost("data")]
Expand All @@ -242,7 +242,7 @@ public async Task<ActionResult<DataElement>> CreateAndUploadData(
[FromRoute] Guid instanceGuid,
[FromQuery] string dataType,
[FromQuery(Name = "refs")] List<Guid> refs = null,
[FromQuery(Name = "generatedFrom")] List<Guid> generatedFromIds = null)
[FromQuery(Name = "generatedFromTask")] string generatedFromTask = null)
{
if (instanceOwnerPartyId == 0 || string.IsNullOrEmpty(dataType) || Request.Body == null)
{
Expand All @@ -268,7 +268,7 @@ public async Task<ActionResult<DataElement>> CreateAndUploadData(
return BadRequest("Requested element type is not declared in application metadata");
}

var streamAndDataElement = await ReadRequestAndCreateDataElementAsync(Request, dataType, refs, generatedFromIds, instance);
var streamAndDataElement = await ReadRequestAndCreateDataElementAsync(Request, dataType, refs, generatedFromTask, instance);
Stream theStream = streamAndDataElement.Stream;
DataElement newData = streamAndDataElement.DataElement;

Expand Down Expand Up @@ -305,7 +305,7 @@ public async Task<ActionResult<DataElement>> CreateAndUploadData(
/// <param name="instanceGuid">The id of the instance that the data element is associated with.</param>
/// <param name="dataGuid">The id of the data element to replace.</param>
/// <param name="refs">An optional array of data element references.</param>
/// <param name="generatedFromIds">An optional array of generated from references containing data element ids.</param>
/// <param name="generatedFromTask">An optional id of the task the data element was generated from</param>
/// <returns>The metadata of the updated data element.</returns>
[Authorize(Policy = AuthzConstants.POLICY_INSTANCE_WRITE)]
[HttpPut("data/{dataGuid}")]
Expand All @@ -321,7 +321,7 @@ public async Task<ActionResult<DataElement>> OverwriteData(
Guid instanceGuid,
Guid dataGuid,
[FromQuery(Name = "refs")] List<Guid> refs = null,
[FromQuery(Name = "generatedFrom")] List<Guid> generatedFromIds = null)
[FromQuery(Name = "generatedFromTask")] string generatedFromTask = null)
{
if (instanceOwnerPartyId == 0 || Request.Body == null)
{
Expand Down Expand Up @@ -368,7 +368,7 @@ public async Task<ActionResult<DataElement>> OverwriteData(
return StatusCode(500, "Storage url does not match with instance metadata");
}

var streamAndDataElement = await ReadRequestAndCreateDataElementAsync(Request, dataElement.DataType, refs, generatedFromIds, instance);
var streamAndDataElement = await ReadRequestAndCreateDataElementAsync(Request, dataElement.DataType, refs, generatedFromTask, instance);
Stream theStream = streamAndDataElement.Stream;
DataElement updatedData = streamAndDataElement.DataElement;

Expand Down Expand Up @@ -484,7 +484,7 @@ public async Task<ActionResult> SetFileScanStatus(
/// <summary>
/// Creates a data element by reading the first multipart element or body of the request.
/// </summary>
private async Task<(Stream Stream, DataElement DataElement)> ReadRequestAndCreateDataElementAsync(HttpRequest request, string elementType, List<Guid> refs, List<Guid> generatedFromIds, Instance instance)
private async Task<(Stream Stream, DataElement DataElement)> ReadRequestAndCreateDataElementAsync(HttpRequest request, string elementType, List<Guid> refs, string generatedFromTask, Instance instance)
{
DateTime creationTime = DateTime.UtcNow;
Stream theStream;
Expand Down Expand Up @@ -532,7 +532,7 @@ public async Task<ActionResult> SetFileScanStatus(

string user = User.GetUserOrOrgId();

DataElement newData = DataElementHelper.CreateDataElement(elementType, refs, instance, creationTime, contentType, contentFileName, fileSize, user, generatedFromIds);
DataElement newData = DataElementHelper.CreateDataElement(elementType, refs, instance, creationTime, contentType, contentFileName, fileSize, user, generatedFromTask);

return (theStream, newData);
}
Expand Down
24 changes: 10 additions & 14 deletions src/Helpers/Storage/DataElementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class DataElementHelper
/// Creates a data element based on element type, instance id, content type, content file name and file size.
/// </summary>
/// <returns>DataElement</returns>
public static DataElement CreateDataElement(string dataType, List<Guid> refs, Instance instance, DateTime creationTime, string contentType, string contentFileName, long fileSize, string user, List<Guid> generatedFromId)
public static DataElement CreateDataElement(string dataType, List<Guid> refs, Instance instance, DateTime creationTime, string contentType, string contentFileName, long fileSize, string user, string generatedFromTask)
{
string dataId = Guid.NewGuid().ToString();

Expand All @@ -38,21 +38,17 @@ public static DataElement CreateDataElement(string dataType, List<Guid> refs, In
Refs = refs,
};

if (generatedFromId != null && generatedFromId.Any())
if (!String.IsNullOrEmpty(generatedFromTask))
{
var references = new List<Reference>();
foreach (Guid id in generatedFromId)
newData.References = new List<Reference>()
{
references.Add(
new Reference
{
Relation = Interface.Enums.RelationType.GeneratedFrom,
Value = id.ToString(),
ValueType = Interface.Enums.ReferenceType.DataElement
});
}

newData.References = references;
new Reference
{
Relation = Interface.Enums.RelationType.GeneratedFrom,
Value = generatedFromTask,
ValueType = Interface.Enums.ReferenceType.Task
}
};
}

string filePath = DataFileName(instance.AppId, guidFromInstanceId, newData.Id);
Expand Down
2 changes: 1 addition & 1 deletion src/LocalTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Altinn.Authorization.ABAC" Version="0.0.5" />
<PackageReference Include="Altinn.Common.PEP" Version="1.3.0" />
<PackageReference Include="Altinn.Platform.Models" Version="1.2.0" />
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="3.21.0" />
<PackageReference Include="Altinn.Platform.Storage.Interface" Version="3.22.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="JWTCookieAuthentication" Version="2.4.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
Expand Down

0 comments on commit c60ccdd

Please sign in to comment.