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.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/")]
@ -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);
@ -185,6 +184,25 @@ public class Controller
.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)
{
// TODO: remove cycle detector

View File

@ -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);

View File

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

View File

@ -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<Folder> GetAllAccessibleFolders(User user)
{
@ -113,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<Folder> GetDirectlyAccessibleFolders(User user)
@ -122,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)

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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -1,7 +1,7 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Innovenergy.Backend;
namespace InnovEnergy.App.Backend;
/// <summary>
/// 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
{

View File

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

View File

@ -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<L,R>
{

View File

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

View File

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

View File

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

View File

@ -1,12 +1,13 @@
using System.Diagnostics.CodeAnalysis;
namespace Innovenergy.Backend.Model;
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();
}

View File

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

View File

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

View File

@ -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
{

View File

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

View File

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