From 55628e21867f19b35eb4a26f6532b371cb1b885b Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 14:58:59 +0100 Subject: [PATCH] updating installations with new order numbers automatically updates the ordernumber2installation relation table --- .../App/Backend/DataTypes/Methods/Session.cs | 40 +++++++++++++++---- csharp/App/Backend/Database/Create.cs | 5 +++ csharp/App/Backend/Database/Db.cs | 1 + .../Relations/OrderNumber2Installation.cs | 5 ++- csharp/InnovEnergy.sln.DotSettings | 1 + 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index 564f90035..e29e9e769 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -98,15 +98,39 @@ public static class SessionMethods var user = session?.User; var original = Db.GetInstallationById(installation?.Id); - + var originalOrderNumbers = original!.GetOrderNumbers(); + + if (!Equals(originalOrderNumbers, installation?.OrderNumbers)) + { + foreach (var orderNumber in installation!.OrderNumbers) + { + if (originalOrderNumbers.Contains(orderNumber)) continue; + var o2I = new OrderNumber2Installation + { + OrderNumber = orderNumber, + InstallationId = installation.Id + }; + Db.Create(o2I); + } + + foreach (var orderNumberOld in originalOrderNumbers) + { + if (!installation!.OrderNumbers.Contains(orderNumberOld)) + { + Db.OrderNumber2Installation.Delete(i => + i.InstallationId == installation.Id && i.OrderNumber == orderNumberOld); + } + } + } + return user is not null - && installation is not null - && original is not null - && user.HasWriteAccess - && user.HasAccessTo(installation) - && installation - .WithParentOf(original) // prevent moving - .Apply(Db.Update); + && installation is not null + && original is not null + && user.HasWriteAccess + && user.HasAccessTo(installation) + && installation + .WithParentOf(original) // prevent moving + .Apply(Db.Update); } public static Boolean Delete(this Session? session, Installation? installation) diff --git a/csharp/App/Backend/Database/Create.cs b/csharp/App/Backend/Database/Create.cs index 436ca6246..95849acaa 100644 --- a/csharp/App/Backend/Database/Create.cs +++ b/csharp/App/Backend/Database/Create.cs @@ -52,4 +52,9 @@ public static partial class Db { return Connection.Insert(folderAccess) > 0; } + + public static Boolean Create(OrderNumber2Installation o2i) + { + return Connection.Insert(o2i) > 0; + } } \ No newline at end of file diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index 161e30485..e9cb827ca 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -100,4 +100,5 @@ public static partial class Db .Select(i => i.RenewS3BucketUrl()) .WhenAll(); } + } \ No newline at end of file diff --git a/csharp/App/Backend/Relations/OrderNumber2Installation.cs b/csharp/App/Backend/Relations/OrderNumber2Installation.cs index ec2b7d84d..a994f7fb7 100644 --- a/csharp/App/Backend/Relations/OrderNumber2Installation.cs +++ b/csharp/App/Backend/Relations/OrderNumber2Installation.cs @@ -4,6 +4,7 @@ namespace InnovEnergy.App.Backend.Relations; public class OrderNumber2Installation : Relation { - [Indexed] public String OrderNumber { get => Left ; init => Left = value;} - [Indexed] public Int64 InstallationId { get => Right; init => Right = value;} + [Indexed] public String OrderNumber { get => Left ; set => Left = value;} + [Indexed] public Int64 InstallationId { get => Right; set => Right = value;} + } \ No newline at end of file diff --git a/csharp/InnovEnergy.sln.DotSettings b/csharp/InnovEnergy.sln.DotSettings index 116325457..8232b1720 100644 --- a/csharp/InnovEnergy.sln.DotSettings +++ b/csharp/InnovEnergy.sln.DotSettings @@ -34,6 +34,7 @@ True True True + True True True True