From 702fa89914ea8c7de101e868e7caa0ce44a7faf8 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 8 Mar 2023 13:20:33 +0100 Subject: [PATCH 1/4] Fix namespaces in Backend --- csharp/App/Backend/Controllers/Controller.cs | 10 +++++----- csharp/App/Backend/Controllers/Credentials.cs | 2 +- csharp/App/Backend/Controllers/ReturnsAttribute.cs | 2 +- csharp/App/Backend/Database/Db.cs | 10 ++++------ csharp/App/Backend/Database/Fake.cs | 4 ++-- csharp/App/Backend/Database/Folder.cs | 6 +++--- csharp/App/Backend/Database/Installation.cs | 6 +++--- csharp/App/Backend/Database/User.cs | 7 +++---- csharp/App/Backend/Database/User2Folder.cs | 4 ++-- csharp/App/Backend/Database/User2Installation.cs | 4 ++-- csharp/App/Backend/HeaderFilter.cs | 2 +- csharp/App/Backend/Model/Folder.cs | 2 +- csharp/App/Backend/Model/Installation.cs | 2 +- csharp/App/Backend/Model/Relations/Relation.cs | 2 +- csharp/App/Backend/Model/Relations/Session.cs | 2 +- csharp/App/Backend/Model/Relations/User2Folder.cs | 2 +- .../App/Backend/Model/Relations/User2Installation.cs | 2 +- csharp/App/Backend/Model/TreeNode.Equality.cs | 2 +- csharp/App/Backend/Model/TreeNode.cs | 2 +- csharp/App/Backend/Model/User.cs | 2 +- csharp/App/Backend/Program.cs | 4 ++-- csharp/App/Backend/Utils/Crypto.cs | 2 +- csharp/App/Backend/Utils/Result.cs | 2 +- 23 files changed, 40 insertions(+), 43 deletions(-) diff --git a/csharp/App/Backend/Controllers/Controller.cs b/csharp/App/Backend/Controllers/Controller.cs index 1aac55752..91c5e012e 100644 --- a/csharp/App/Backend/Controllers/Controller.cs +++ b/csharp/App/Backend/Controllers/Controller.cs @@ -1,14 +1,14 @@ using System.Net; using System.Text; -using Innovenergy.Backend.Database; -using Innovenergy.Backend.Model; -using Innovenergy.Backend.Model.Relations; -using Innovenergy.Backend.Utils; +using InnovEnergy.App.Backend.Database; +using InnovEnergy.App.Backend.Model; +using InnovEnergy.App.Backend.Model.Relations; +using InnovEnergy.App.Backend.Utils; using InnovEnergy.Lib.Utils; using Microsoft.AspNetCore.Mvc; using HttpContextAccessor = Microsoft.AspNetCore.Http.HttpContextAccessor; -namespace Innovenergy.Backend.Controllers; +namespace InnovEnergy.App.Backend.Controllers; [ApiController] [Route("api/")] diff --git a/csharp/App/Backend/Controllers/Credentials.cs b/csharp/App/Backend/Controllers/Credentials.cs index 2c5c72b45..f3149f7e8 100644 --- a/csharp/App/Backend/Controllers/Credentials.cs +++ b/csharp/App/Backend/Controllers/Credentials.cs @@ -1,6 +1,6 @@ using System.Diagnostics.CodeAnalysis; -namespace Innovenergy.Backend.Controllers; +namespace InnovEnergy.App.Backend.Controllers; [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public record Credentials(String Username, String Password); \ No newline at end of file diff --git a/csharp/App/Backend/Controllers/ReturnsAttribute.cs b/csharp/App/Backend/Controllers/ReturnsAttribute.cs index 6d94088f5..2ef924055 100644 --- a/csharp/App/Backend/Controllers/ReturnsAttribute.cs +++ b/csharp/App/Backend/Controllers/ReturnsAttribute.cs @@ -1,7 +1,7 @@ using System.Net; using Microsoft.AspNetCore.Mvc; -namespace Innovenergy.Backend.Controllers; +namespace InnovEnergy.App.Backend.Controllers; public class ReturnsAttribute : ProducesResponseTypeAttribute { diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index aaabfbfd9..f8ff601bd 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -1,11 +1,11 @@ using System.Diagnostics.CodeAnalysis; -using Innovenergy.Backend.Model; -using Innovenergy.Backend.Model.Relations; -using Innovenergy.Backend.Utils; +using InnovEnergy.App.Backend.Model; +using InnovEnergy.App.Backend.Model.Relations; +using InnovEnergy.App.Backend.Utils; using InnovEnergy.Lib.Utils; using SQLite; -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db : IDisposable { @@ -97,8 +97,6 @@ public partial class Db : IDisposable return direct.Concat(fromFolders); } - - public IEnumerable GetAllAccessibleFolders(User user) { diff --git a/csharp/App/Backend/Database/Fake.cs b/csharp/App/Backend/Database/Fake.cs index 427c78434..63fff39ee 100644 --- a/csharp/App/Backend/Database/Fake.cs +++ b/csharp/App/Backend/Database/Fake.cs @@ -1,6 +1,6 @@ -using Innovenergy.Backend.Model.Relations; +using InnovEnergy.App.Backend.Model.Relations; -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db { diff --git a/csharp/App/Backend/Database/Folder.cs b/csharp/App/Backend/Database/Folder.cs index 34e20f529..8a4785639 100644 --- a/csharp/App/Backend/Database/Folder.cs +++ b/csharp/App/Backend/Database/Folder.cs @@ -1,9 +1,9 @@ -using Innovenergy.Backend.Model; -using Innovenergy.Backend.Utils; +using InnovEnergy.App.Backend.Model; +using InnovEnergy.App.Backend.Utils; using InnovEnergy.Lib.Utils; using SQLite; -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db { diff --git a/csharp/App/Backend/Database/Installation.cs b/csharp/App/Backend/Database/Installation.cs index a362a39df..6e554f483 100644 --- a/csharp/App/Backend/Database/Installation.cs +++ b/csharp/App/Backend/Database/Installation.cs @@ -1,8 +1,8 @@ -using Innovenergy.Backend.Model; -using Innovenergy.Backend.Utils; +using InnovEnergy.App.Backend.Model; +using InnovEnergy.App.Backend.Utils; using SQLite; -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db { diff --git a/csharp/App/Backend/Database/User.cs b/csharp/App/Backend/Database/User.cs index ea2344aaf..0719ab9fe 100644 --- a/csharp/App/Backend/Database/User.cs +++ b/csharp/App/Backend/Database/User.cs @@ -2,18 +2,17 @@ using System.Diagnostics.CodeAnalysis; using System.Net.Mail; using System.Security.Cryptography; using System.Text; -using System.Text.Json; using System.Text.Json.Nodes; using Flurl.Http; -using Innovenergy.Backend.Model; -using Innovenergy.Backend.Utils; +using InnovEnergy.App.Backend.Model; +using InnovEnergy.App.Backend.Utils; using InnovEnergy.Lib.Utils; using SQLite; #pragma warning disable CS0472 #pragma warning disable CS8602 -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db { diff --git a/csharp/App/Backend/Database/User2Folder.cs b/csharp/App/Backend/Database/User2Folder.cs index 465eea722..29351f1dd 100644 --- a/csharp/App/Backend/Database/User2Folder.cs +++ b/csharp/App/Backend/Database/User2Folder.cs @@ -1,7 +1,7 @@ -using Innovenergy.Backend.Model.Relations; +using InnovEnergy.App.Backend.Model.Relations; using SQLite; -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db { diff --git a/csharp/App/Backend/Database/User2Installation.cs b/csharp/App/Backend/Database/User2Installation.cs index ca329deee..069588d7c 100644 --- a/csharp/App/Backend/Database/User2Installation.cs +++ b/csharp/App/Backend/Database/User2Installation.cs @@ -1,7 +1,7 @@ -using Innovenergy.Backend.Model.Relations; +using InnovEnergy.App.Backend.Model.Relations; using SQLite; -namespace Innovenergy.Backend.Database; +namespace InnovEnergy.App.Backend.Database; public partial class Db { diff --git a/csharp/App/Backend/HeaderFilter.cs b/csharp/App/Backend/HeaderFilter.cs index 8ca9cdff8..f3520b75e 100644 --- a/csharp/App/Backend/HeaderFilter.cs +++ b/csharp/App/Backend/HeaderFilter.cs @@ -1,7 +1,7 @@ using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; -namespace Innovenergy.Backend; +namespace InnovEnergy.App.Backend; /// /// This is for convenient testing! Todo throw me out? diff --git a/csharp/App/Backend/Model/Folder.cs b/csharp/App/Backend/Model/Folder.cs index c9c39c594..041c0f479 100644 --- a/csharp/App/Backend/Model/Folder.cs +++ b/csharp/App/Backend/Model/Folder.cs @@ -1,4 +1,4 @@ -namespace Innovenergy.Backend.Model; +namespace InnovEnergy.App.Backend.Model; public class Folder : TreeNode { diff --git a/csharp/App/Backend/Model/Installation.cs b/csharp/App/Backend/Model/Installation.cs index 4cca94295..b9858e9c8 100644 --- a/csharp/App/Backend/Model/Installation.cs +++ b/csharp/App/Backend/Model/Installation.cs @@ -1,4 +1,4 @@ -namespace Innovenergy.Backend.Model; +namespace InnovEnergy.App.Backend.Model; public class Installation : TreeNode diff --git a/csharp/App/Backend/Model/Relations/Relation.cs b/csharp/App/Backend/Model/Relations/Relation.cs index 31250df14..a791a4609 100644 --- a/csharp/App/Backend/Model/Relations/Relation.cs +++ b/csharp/App/Backend/Model/Relations/Relation.cs @@ -1,7 +1,7 @@ using System.Diagnostics.CodeAnalysis; using SQLite; -namespace Innovenergy.Backend.Model.Relations; +namespace InnovEnergy.App.Backend.Model.Relations; public abstract class Relation { diff --git a/csharp/App/Backend/Model/Relations/Session.cs b/csharp/App/Backend/Model/Relations/Session.cs index ff64b6644..829cc9a60 100644 --- a/csharp/App/Backend/Model/Relations/Session.cs +++ b/csharp/App/Backend/Model/Relations/Session.cs @@ -1,6 +1,6 @@ using SQLite; -namespace Innovenergy.Backend.Model.Relations; +namespace InnovEnergy.App.Backend.Model.Relations; public class Session : Relation { diff --git a/csharp/App/Backend/Model/Relations/User2Folder.cs b/csharp/App/Backend/Model/Relations/User2Folder.cs index 6e9f56433..f93cab568 100644 --- a/csharp/App/Backend/Model/Relations/User2Folder.cs +++ b/csharp/App/Backend/Model/Relations/User2Folder.cs @@ -1,6 +1,6 @@ using SQLite; -namespace Innovenergy.Backend.Model.Relations; +namespace InnovEnergy.App.Backend.Model.Relations; internal class User2Folder : Relation { diff --git a/csharp/App/Backend/Model/Relations/User2Installation.cs b/csharp/App/Backend/Model/Relations/User2Installation.cs index 43fa45d0e..fcd2760dd 100644 --- a/csharp/App/Backend/Model/Relations/User2Installation.cs +++ b/csharp/App/Backend/Model/Relations/User2Installation.cs @@ -1,6 +1,6 @@ using SQLite; -namespace Innovenergy.Backend.Model.Relations; +namespace InnovEnergy.App.Backend.Model.Relations; internal class User2Installation : Relation { diff --git a/csharp/App/Backend/Model/TreeNode.Equality.cs b/csharp/App/Backend/Model/TreeNode.Equality.cs index b044a020f..e2e374ef4 100644 --- a/csharp/App/Backend/Model/TreeNode.Equality.cs +++ b/csharp/App/Backend/Model/TreeNode.Equality.cs @@ -1,6 +1,6 @@ using System.Diagnostics.CodeAnalysis; -namespace Innovenergy.Backend.Model; +namespace InnovEnergy.App.Backend.Model; public abstract partial class TreeNode { diff --git a/csharp/App/Backend/Model/TreeNode.cs b/csharp/App/Backend/Model/TreeNode.cs index b3cf22170..d02890a62 100644 --- a/csharp/App/Backend/Model/TreeNode.cs +++ b/csharp/App/Backend/Model/TreeNode.cs @@ -1,6 +1,6 @@ using SQLite; -namespace Innovenergy.Backend.Model; +namespace InnovEnergy.App.Backend.Model; public abstract partial class TreeNode { diff --git a/csharp/App/Backend/Model/User.cs b/csharp/App/Backend/Model/User.cs index a1a0be392..762c819a4 100644 --- a/csharp/App/Backend/Model/User.cs +++ b/csharp/App/Backend/Model/User.cs @@ -1,6 +1,6 @@ using SQLite; -namespace Innovenergy.Backend.Model; +namespace InnovEnergy.App.Backend.Model; public class User : TreeNode { diff --git a/csharp/App/Backend/Program.cs b/csharp/App/Backend/Program.cs index fd0206737..ae662b779 100644 --- a/csharp/App/Backend/Program.cs +++ b/csharp/App/Backend/Program.cs @@ -1,7 +1,7 @@ -using Innovenergy.Backend.Database; +using InnovEnergy.App.Backend.Database; using Microsoft.OpenApi.Models; -namespace Innovenergy.Backend; +namespace InnovEnergy.App.Backend; public static class Program { diff --git a/csharp/App/Backend/Utils/Crypto.cs b/csharp/App/Backend/Utils/Crypto.cs index 199b17fb9..c176fd284 100644 --- a/csharp/App/Backend/Utils/Crypto.cs +++ b/csharp/App/Backend/Utils/Crypto.cs @@ -1,6 +1,6 @@ using System.Security.Cryptography; -namespace Innovenergy.Backend.Utils; +namespace InnovEnergy.App.Backend.Utils; public static class Crypto { diff --git a/csharp/App/Backend/Utils/Result.cs b/csharp/App/Backend/Utils/Result.cs index 21e85876c..979e9184e 100644 --- a/csharp/App/Backend/Utils/Result.cs +++ b/csharp/App/Backend/Utils/Result.cs @@ -1,4 +1,4 @@ -namespace Innovenergy.Backend.Utils; +namespace InnovEnergy.App.Backend.Utils; public class Result { From e6fa2933b54b09aab3cfff9b76b8710ccf79d750 Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 8 Mar 2023 13:28:22 +0100 Subject: [PATCH 2/4] Only consider Id, but not ParentId for TreeNode equality checks --- csharp/App/Backend/Model/TreeNode.Equality.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/csharp/App/Backend/Model/TreeNode.Equality.cs b/csharp/App/Backend/Model/TreeNode.Equality.cs index e2e374ef4..f37cd51d7 100644 --- a/csharp/App/Backend/Model/TreeNode.Equality.cs +++ b/csharp/App/Backend/Model/TreeNode.Equality.cs @@ -4,9 +4,10 @@ namespace InnovEnergy.App.Backend.Model; public abstract partial class TreeNode { + // Note: Only consider Id, but not ParentId for TreeNode equality checks protected Boolean Equals(TreeNode other) { - return Id == other.Id && ParentId == other.ParentId; + return Id == other.Id; } public override Boolean Equals(Object? obj) @@ -17,8 +18,5 @@ public abstract partial class TreeNode } [SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] - public override Int32 GetHashCode() - { - return HashCode.Combine(Id, ParentId); - } + public override Int32 GetHashCode() => Id.GetHashCode(); } \ No newline at end of file From faf35be6cfe5bb7bee25ca44ded2610342796aea Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 8 Mar 2023 13:33:55 +0100 Subject: [PATCH 3/4] Set parentId=0 already in DB layer, not in controller --- csharp/App/Backend/Controllers/Controller.cs | 3 +-- csharp/App/Backend/Database/Db.cs | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/csharp/App/Backend/Controllers/Controller.cs b/csharp/App/Backend/Controllers/Controller.cs index 91c5e012e..b7935922b 100644 --- a/csharp/App/Backend/Controllers/Controller.cs +++ b/csharp/App/Backend/Controllers/Controller.cs @@ -174,8 +174,7 @@ public class Controller using var db = Db.Connect(); var folders = db - .GetDirectlyAccessibleFolders(caller) - .Do(f => f.ParentId = 0) // ReSharper disable once AccessToDisposedClosure + .GetDirectlyAccessibleFolders(caller) // ReSharper disable once AccessToDisposedClosure .Select(f => PopulateChildren(db, f)); var installations = db.GetDirectlyAccessibleInstallations(caller); diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index f8ff601bd..1625c128f 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -111,7 +111,8 @@ public partial class Db : IDisposable .Where(r => r.UserId == user.Id) .Select(r => r.InstallationId) .Select(GetInstallationById) - .NotNull(); + .NotNull() + .Do(i => i.ParentId = 0); // hide inaccessible parents from calling user } public IEnumerable GetDirectlyAccessibleFolders(User user) @@ -120,7 +121,8 @@ public partial class Db : IDisposable .Where(r => r.UserId == user.Id) .Select(r => r.FolderId) .Select(GetFolderById) - .NotNull(); + .NotNull() + .Do(i => i.ParentId = 0); // hide inaccessible parents from calling user; } public Result AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId) From 72293807ee8b20e7af1fa3fc0bd27fc01111fbea Mon Sep 17 00:00:00 2001 From: ig Date: Wed, 8 Mar 2023 13:40:34 +0100 Subject: [PATCH 4/4] add new API: GetAllFoldersAndInstallations --- csharp/App/Backend/Controllers/Controller.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/csharp/App/Backend/Controllers/Controller.cs b/csharp/App/Backend/Controllers/Controller.cs index b7935922b..9136ac151 100644 --- a/csharp/App/Backend/Controllers/Controller.cs +++ b/csharp/App/Backend/Controllers/Controller.cs @@ -184,6 +184,25 @@ public class Controller .ToList(); // important! } + [Returns] // assuming swagger knows about arrays but not lists (JSON) + [Returns(HttpStatusCode.Unauthorized)] + [HttpGet($"{nameof(GetAllFoldersAndInstallations)}/")] + public Object GetAllFoldersAndInstallations() + { + var caller = GetCaller(); + if (caller == null) + return new HttpResponseMessage(HttpStatusCode.Unauthorized); + + using var db = Db.Connect(); + + var folders = db.GetAllAccessibleFolders(caller) as IEnumerable; + var installations = db.GetAllAccessibleInstallations(caller); + + return folders + .Concat(installations) + .ToList(); // important! + } + private static Folder PopulateChildren(Db db, Folder folder, HashSet? hs = null) { // TODO: remove cycle detector