From d1fbd6b943c60402b040b9b4d7108cf34013891d Mon Sep 17 00:00:00 2001 From: Ivar Date: Mon, 5 Dec 2022 13:24:36 +0100 Subject: [PATCH 1/2] Add a few convenience implementations for UpdateDataValues on IInstance interface --- src/Altinn.App.Core/Interface/IInstance.cs | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Altinn.App.Core/Interface/IInstance.cs b/src/Altinn.App.Core/Interface/IInstance.cs index e5a9c74e7..4a68b1eec 100644 --- a/src/Altinn.App.Core/Interface/IInstance.cs +++ b/src/Altinn.App.Core/Interface/IInstance.cs @@ -92,6 +92,37 @@ public interface IInstance /// Returns the updated instance. Task UpdateDataValues(int instanceOwnerPartyId, Guid instanceGuid, DataValues dataValues); + /// + /// Update data data values. + /// + /// + /// The provided data value will be added with the existing collection of data values on the instance. + /// + /// The instance + /// The data value (null unsets the value) + /// Returns the updated instance. + async Task UpdateDataValues(Instance instance, Dictionary dataValues) + { + var instanceOwnerPartyId = int.Parse(instance.Id.Split("/")[0]); + var instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); + return await UpdateDataValues(instanceOwnerPartyId, instanceGuid, new DataValues{Values = dataValues}); + } + + /// + /// Update single data value. + /// + /// + /// The provided data value will be added with the existing collection of data values on the instance. + /// + /// The instance + /// The key of the DataValues collection to be updated. + /// The data value (null unsets the value) + /// Returns the updated instance. + async Task UpdateDataValue(Instance instance, string key, string? value) + { + return await UpdateDataValues(instance, new Dictionary{{key, value}}); + } + /// /// Delete instance. /// From ea4d900fb8f45b2e6c0e0fe9c64aff6d6159f416 Mon Sep 17 00:00:00 2001 From: Ivar Date: Mon, 2 Jan 2023 10:35:43 +0100 Subject: [PATCH 2/2] Use InstanceIdentifier instead of manually splitting instance.Id Added a new convenicence constructor to improve type safety when constructing from an instance --- src/Altinn.App.Core/Interface/IInstance.cs | 6 +++--- src/Altinn.App.Core/Models/InstanceIdentifier.cs | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Altinn.App.Core/Interface/IInstance.cs b/src/Altinn.App.Core/Interface/IInstance.cs index 4a68b1eec..875ff9a1d 100644 --- a/src/Altinn.App.Core/Interface/IInstance.cs +++ b/src/Altinn.App.Core/Interface/IInstance.cs @@ -1,3 +1,4 @@ +using Altinn.App.Core.Models; using Altinn.Platform.Storage.Interface.Models; using Microsoft.Extensions.Primitives; @@ -103,9 +104,8 @@ public interface IInstance /// Returns the updated instance. async Task UpdateDataValues(Instance instance, Dictionary dataValues) { - var instanceOwnerPartyId = int.Parse(instance.Id.Split("/")[0]); - var instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); - return await UpdateDataValues(instanceOwnerPartyId, instanceGuid, new DataValues{Values = dataValues}); + var id = new InstanceIdentifier(instance); + return await UpdateDataValues(id.InstanceOwnerPartyId, id.InstanceGuid, new DataValues{Values = dataValues}); } /// diff --git a/src/Altinn.App.Core/Models/InstanceIdentifier.cs b/src/Altinn.App.Core/Models/InstanceIdentifier.cs index 309287c90..eb10aa707 100644 --- a/src/Altinn.App.Core/Models/InstanceIdentifier.cs +++ b/src/Altinn.App.Core/Models/InstanceIdentifier.cs @@ -1,3 +1,5 @@ +using Altinn.Platform.Storage.Interface.Models; + namespace Altinn.App.Core.Models { /// @@ -27,6 +29,12 @@ public InstanceIdentifier(string instanceId) IsNoInstance = false; } + /// + /// Initializes a new instance of the class. + /// + /// Is the instance you want to get an idenifier from + public InstanceIdentifier(Instance instance) : this(instance.Id) {} + /// /// Initializes a new instance of the class. For instances without OwnerPartyId and InstanceId, ex: Stateless applications. ///