This commit is contained in:
Sina Blattmann 2023-03-08 13:45:41 +01:00
commit c7604a86cf
23 changed files with 67 additions and 52 deletions

View File

@ -1,14 +1,14 @@
using System.Net; using System.Net;
using System.Text; using System.Text;
using Innovenergy.Backend.Database; using InnovEnergy.App.Backend.Database;
using Innovenergy.Backend.Model; using InnovEnergy.App.Backend.Model;
using Innovenergy.Backend.Model.Relations; using InnovEnergy.App.Backend.Model.Relations;
using Innovenergy.Backend.Utils; using InnovEnergy.App.Backend.Utils;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using HttpContextAccessor = Microsoft.AspNetCore.Http.HttpContextAccessor; using HttpContextAccessor = Microsoft.AspNetCore.Http.HttpContextAccessor;
namespace Innovenergy.Backend.Controllers; namespace InnovEnergy.App.Backend.Controllers;
[ApiController] [ApiController]
[Route("api/")] [Route("api/")]
@ -174,8 +174,7 @@ public class Controller
using var db = Db.Connect(); using var db = Db.Connect();
var folders = db var folders = db
.GetDirectlyAccessibleFolders(caller) .GetDirectlyAccessibleFolders(caller) // ReSharper disable once AccessToDisposedClosure
.Do(f => f.ParentId = 0) // ReSharper disable once AccessToDisposedClosure
.Select(f => PopulateChildren(db, f)); .Select(f => PopulateChildren(db, f));
var installations = db.GetDirectlyAccessibleInstallations(caller); var installations = db.GetDirectlyAccessibleInstallations(caller);
@ -185,6 +184,25 @@ public class Controller
.ToList(); // important! .ToList(); // important!
} }
[Returns<TreeNode[]>] // 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<TreeNode>;
var installations = db.GetAllAccessibleInstallations(caller);
return folders
.Concat(installations)
.ToList(); // important!
}
private static Folder PopulateChildren(Db db, Folder folder, HashSet<Int64>? hs = null) private static Folder PopulateChildren(Db db, Folder folder, HashSet<Int64>? hs = null)
{ {
// TODO: remove cycle detector // TODO: remove cycle detector

View File

@ -1,6 +1,6 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Innovenergy.Backend.Controllers; namespace InnovEnergy.App.Backend.Controllers;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public record Credentials(String Username, String Password); public record Credentials(String Username, String Password);

View File

@ -1,7 +1,7 @@
using System.Net; using System.Net;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Innovenergy.Backend.Controllers; namespace InnovEnergy.App.Backend.Controllers;
public class ReturnsAttribute : ProducesResponseTypeAttribute public class ReturnsAttribute : ProducesResponseTypeAttribute
{ {

View File

@ -1,11 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Innovenergy.Backend.Model; using InnovEnergy.App.Backend.Model;
using Innovenergy.Backend.Model.Relations; using InnovEnergy.App.Backend.Model.Relations;
using Innovenergy.Backend.Utils; using InnovEnergy.App.Backend.Utils;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using SQLite; using SQLite;
namespace Innovenergy.Backend.Database; namespace InnovEnergy.App.Backend.Database;
public partial class Db : IDisposable public partial class Db : IDisposable
{ {
@ -98,8 +98,6 @@ public partial class Db : IDisposable
} }
public IEnumerable<Folder> GetAllAccessibleFolders(User user) public IEnumerable<Folder> GetAllAccessibleFolders(User user)
{ {
return GetDirectlyAccessibleFolders(user) return GetDirectlyAccessibleFolders(user)
@ -113,7 +111,8 @@ public partial class Db : IDisposable
.Where(r => r.UserId == user.Id) .Where(r => r.UserId == user.Id)
.Select(r => r.InstallationId) .Select(r => r.InstallationId)
.Select(GetInstallationById) .Select(GetInstallationById)
.NotNull(); .NotNull()
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user
} }
public IEnumerable<Folder> GetDirectlyAccessibleFolders(User user) public IEnumerable<Folder> GetDirectlyAccessibleFolders(User user)
@ -122,7 +121,8 @@ public partial class Db : IDisposable
.Where(r => r.UserId == user.Id) .Where(r => r.UserId == user.Id)
.Select(r => r.FolderId) .Select(r => r.FolderId)
.Select(GetFolderById) .Select(GetFolderById)
.NotNull(); .NotNull()
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user;
} }
public Result AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId) public Result AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId)

View File

@ -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 public partial class Db
{ {

View File

@ -1,9 +1,9 @@
using Innovenergy.Backend.Model; using InnovEnergy.App.Backend.Model;
using Innovenergy.Backend.Utils; using InnovEnergy.App.Backend.Utils;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using SQLite; using SQLite;
namespace Innovenergy.Backend.Database; namespace InnovEnergy.App.Backend.Database;
public partial class Db public partial class Db
{ {

View File

@ -1,8 +1,8 @@
using Innovenergy.Backend.Model; using InnovEnergy.App.Backend.Model;
using Innovenergy.Backend.Utils; using InnovEnergy.App.Backend.Utils;
using SQLite; using SQLite;
namespace Innovenergy.Backend.Database; namespace InnovEnergy.App.Backend.Database;
public partial class Db public partial class Db
{ {

View File

@ -2,18 +2,17 @@ using System.Diagnostics.CodeAnalysis;
using System.Net.Mail; using System.Net.Mail;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Flurl.Http; using Flurl.Http;
using Innovenergy.Backend.Model; using InnovEnergy.App.Backend.Model;
using Innovenergy.Backend.Utils; using InnovEnergy.App.Backend.Utils;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using SQLite; using SQLite;
#pragma warning disable CS0472 #pragma warning disable CS0472
#pragma warning disable CS8602 #pragma warning disable CS8602
namespace Innovenergy.Backend.Database; namespace InnovEnergy.App.Backend.Database;
public partial class Db public partial class Db
{ {

View File

@ -1,7 +1,7 @@
using Innovenergy.Backend.Model.Relations; using InnovEnergy.App.Backend.Model.Relations;
using SQLite; using SQLite;
namespace Innovenergy.Backend.Database; namespace InnovEnergy.App.Backend.Database;
public partial class Db public partial class Db
{ {

View File

@ -1,7 +1,7 @@
using Innovenergy.Backend.Model.Relations; using InnovEnergy.App.Backend.Model.Relations;
using SQLite; using SQLite;
namespace Innovenergy.Backend.Database; namespace InnovEnergy.App.Backend.Database;
public partial class Db public partial class Db
{ {

View File

@ -1,7 +1,7 @@
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
namespace Innovenergy.Backend; namespace InnovEnergy.App.Backend;
/// <summary> /// <summary>
/// This is for convenient testing! Todo throw me out? /// This is for convenient testing! Todo throw me out?

View File

@ -1,4 +1,4 @@
namespace Innovenergy.Backend.Model; namespace InnovEnergy.App.Backend.Model;
public class Folder : TreeNode public class Folder : TreeNode
{ {

View File

@ -1,4 +1,4 @@
namespace Innovenergy.Backend.Model; namespace InnovEnergy.App.Backend.Model;
public class Installation : TreeNode public class Installation : TreeNode

View File

@ -1,7 +1,7 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using SQLite; using SQLite;
namespace Innovenergy.Backend.Model.Relations; namespace InnovEnergy.App.Backend.Model.Relations;
public abstract class Relation<L,R> public abstract class Relation<L,R>
{ {

View File

@ -1,6 +1,6 @@
using SQLite; using SQLite;
namespace Innovenergy.Backend.Model.Relations; namespace InnovEnergy.App.Backend.Model.Relations;
public class Session : Relation<String, Int64> public class Session : Relation<String, Int64>
{ {

View File

@ -1,6 +1,6 @@
using SQLite; using SQLite;
namespace Innovenergy.Backend.Model.Relations; namespace InnovEnergy.App.Backend.Model.Relations;
internal class User2Folder : Relation<Int64, Int64> internal class User2Folder : Relation<Int64, Int64>
{ {

View File

@ -1,6 +1,6 @@
using SQLite; using SQLite;
namespace Innovenergy.Backend.Model.Relations; namespace InnovEnergy.App.Backend.Model.Relations;
internal class User2Installation : Relation<Int64, Int64> internal class User2Installation : Relation<Int64, Int64>
{ {

View File

@ -1,12 +1,13 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Innovenergy.Backend.Model; namespace InnovEnergy.App.Backend.Model;
public abstract partial class TreeNode public abstract partial class TreeNode
{ {
// Note: Only consider Id, but not ParentId for TreeNode equality checks
protected Boolean Equals(TreeNode other) protected Boolean Equals(TreeNode other)
{ {
return Id == other.Id && ParentId == other.ParentId; return Id == other.Id;
} }
public override Boolean Equals(Object? obj) public override Boolean Equals(Object? obj)
@ -17,8 +18,5 @@ public abstract partial class TreeNode
} }
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")] [SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override Int32 GetHashCode() public override Int32 GetHashCode() => Id.GetHashCode();
{
return HashCode.Combine(Id, ParentId);
}
} }

View File

@ -1,6 +1,6 @@
using SQLite; using SQLite;
namespace Innovenergy.Backend.Model; namespace InnovEnergy.App.Backend.Model;
public abstract partial class TreeNode public abstract partial class TreeNode
{ {

View File

@ -1,6 +1,6 @@
using SQLite; using SQLite;
namespace Innovenergy.Backend.Model; namespace InnovEnergy.App.Backend.Model;
public class User : TreeNode public class User : TreeNode
{ {

View File

@ -1,7 +1,7 @@
using Innovenergy.Backend.Database; using InnovEnergy.App.Backend.Database;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
namespace Innovenergy.Backend; namespace InnovEnergy.App.Backend;
public static class Program public static class Program
{ {

View File

@ -1,6 +1,6 @@
using System.Security.Cryptography; using System.Security.Cryptography;
namespace Innovenergy.Backend.Utils; namespace InnovEnergy.App.Backend.Utils;
public static class Crypto public static class Crypto
{ {

View File

@ -1,4 +1,4 @@
namespace Innovenergy.Backend.Utils; namespace InnovEnergy.App.Backend.Utils;
public class Result public class Result
{ {