Skip to content

Commit

Permalink
Code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne committed Nov 15, 2023
1 parent cf256d0 commit b1b1357
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
22 changes: 12 additions & 10 deletions src/Altinn.App.Api/Controllers/StatelessDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,21 @@ public async Task<ActionResult> Get(
await _prefillService.PrefillDataModel(owner.PartyId, dataType, appModel);

Instance virtualInstance = new Instance() { InstanceOwner = owner };
await ProcessAllDataWrite(virtualInstance, appModel);

return Ok(appModel);
}

private async Task ProcessAllDataWrite(Instance virtualInstance, object appModel)
{
foreach (var dataProcessor in _dataProcessors)
{
_logger.LogInformation("ProcessDataRead for {modelType} using {dataProcesor}", appModel.GetType().Name, dataProcessor.GetType().Name);
_logger.LogInformation(
"ProcessDataRead for {modelType} using {dataProcesor}",
appModel.GetType().Name,
dataProcessor.GetType().Name);
await dataProcessor.ProcessDataRead(virtualInstance, null, appModel);
}

return Ok(appModel);
}

/// <summary>
Expand Down Expand Up @@ -152,14 +160,8 @@ public async Task<ActionResult> GetAnonymous([FromQuery] string dataType)
}

object appModel = _appModel.Create(classRef);

var virtualInstance = new Instance();

foreach (var dataProcessor in _dataProcessors)
{
_logger.LogInformation("ProcessDataRead for {modelType} using {dataProcesor}", appModel.GetType().Name, dataProcessor.GetType().Name);
await dataProcessor.ProcessDataRead(virtualInstance, null, appModel);
}
await ProcessAllDataWrite(virtualInstance, appModel);

return Ok(appModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private async Task<ModelDeserializerResult> DeserializeMultipartAsync(Stream str
MultipartSection? firstSection = await reader.ReadNextSectionAsync();
if (firstSection?.ContentDisposition?.Contains("name=\"dataModel\"") != true)
{
return ModelDeserializerResult.FromError("First entry in mulipart serialization must have name=\"dataModel\"");
return ModelDeserializerResult.FromError("First entry in multipart serialization must have name=\"dataModel\"");
}
var modelResult = await DeserializeJsonAsync(firstSection.Body);
if (modelResult.HasError)
Expand All @@ -83,12 +83,12 @@ private async Task<ModelDeserializerResult> DeserializeMultipartAsync(Stream str
{
if (secondSection?.ContentDisposition?.Contains("name=\"previousValues\"") != true)
{
return ModelDeserializerResult.FromError("First entry in mulipart serialization must have name=\"dataModel\"");
return ModelDeserializerResult.FromError("Second entry in multipart serialization must have name=\"previousValues\"");
}
reportedChanges = await System.Text.Json.JsonSerializer.DeserializeAsync<Dictionary<string, string?>>(secondSection.Body);
if (await reader.ReadNextSectionAsync() != null)
{
return ModelDeserializerResult.FromError("Multipart request had more than 2 elements");
return ModelDeserializerResult.FromError("Multipart request had more than 2 elements. Only \"dataModel\" and the optional \"previousValues\" are supported.");
}
}
return ModelDeserializerResult.FromSuccess(modelResult.Model, reportedChanges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ public async Task<object> GetFormData(Guid instanceGuid, Type type, string org,
HttpResponseMessage response = await _client.GetAsync(token, apiUrl);
if (response.IsSuccessStatusCode)
{
using Stream stream = await response.Content.ReadAsStreamAsync();
await using Stream stream = await response.Content.ReadAsStreamAsync();
ModelDeserializer deserializer = new ModelDeserializer(_logger, type);
ModelDeserializerResult deserializerResult = await deserializer.DeserializeAsync(stream, "application/xml");

if (deserializerResult.Error is not null || deserializerResult.Model is null )
if (deserializerResult.HasError)
{
_logger.LogError($"Cannot deserialize XML form data read from storage: {deserializerResult.Error}");
throw new ServiceException(HttpStatusCode.Conflict, $"Cannot deserialize XML form data from storage {deserializerResult.Error}");
Expand Down

0 comments on commit b1b1357

Please sign in to comment.