Move tables to main DB class, convert entity related function to extensions

This commit is contained in:
ig 2023-03-13 11:48:04 +01:00
parent 5a082c22e8
commit d974a7f176
7 changed files with 70 additions and 95 deletions

View File

@ -10,9 +10,40 @@ public static partial class Db
{
internal const String DbPath = "./db.sqlite";
private static readonly SQLiteConnection _Db = new SQLiteConnection(DbPath);
public static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath);
public static TableQuery<Session> Sessions => Connection.Table<Session>();
public static TableQuery<Folder> Folders => Connection.Table<Folder>();
public static TableQuery<Installation> Installations => Connection.Table<Installation>();
public static TableQuery<User> Users => Connection.Table<User>();
public static TableQuery<User2Folder> User2Folder => Connection.Table<User2Folder>();
public static TableQuery<User2Installation> User2Installation => Connection.Table<User2Installation>();
public static Int32 NbUser2Installation => User2Installation.Count();
public static Int32 NbUser2Folder => User2Folder.Count();
public static Int32 NbFolders => Folders.Count();
public static Int32 NbInstallations => Installations.Count();
public static Int32 NbUsers => Users.Count();
public static Folder? GetFolderById(Int64 id)
{
return Folders
.FirstOrDefault(f => f.Id == id);
}
public static Installation? GetInstallationById(Int64 id)
{
return Installations
.FirstOrDefault(i => i.Id == id);
}
public static User? GetUserById(Int64 id)
{
return Users
.FirstOrDefault(u => u.Id == id);
}
private static TableQuery<Session> Sessions => _Db.Table<Session>();
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
static Db()
@ -38,8 +69,8 @@ public static partial class Db
{
try
{
_Db.Insert(treeNode);
return SQLite3.LastInsertRowid(_Db.Handle);
Connection.Insert(treeNode);
return SQLite3.LastInsertRowid(Connection.Handle);
}
catch (Exception e)
{
@ -52,7 +83,7 @@ public static partial class Db
{
try
{
_Db.Insert(session);
Connection.Insert(session);
return true;
}
catch (Exception e)
@ -66,7 +97,7 @@ public static partial class Db
{
try
{
_Db.InsertOrReplace(treeNode);
Connection.InsertOrReplace(treeNode);
return true;
}
catch (Exception e)
@ -80,7 +111,7 @@ public static partial class Db
{
try
{
_Db.Delete(treeNode);
Connection.Delete(treeNode);
return true;
}
catch (Exception e)
@ -141,7 +172,7 @@ public static partial class Db
try
{
_Db.Insert(con);
Connection.Insert(con);
return true;
}
catch (Exception e)
@ -160,7 +191,7 @@ public static partial class Db
try
{
_Db.Insert(con);
Connection.Insert(con);
return true;
}
catch (Exception e)

View File

@ -6,7 +6,7 @@ public static partial class Db
{
public static void CreateFakeRelations()
{
_Db.RunInTransaction(() =>
Connection.RunInTransaction(() =>
{
CreateFakeUserTree();
CreateFakeFolderTree();
@ -62,7 +62,7 @@ public static partial class Db
private static void GiveFakeUsersAccessToFolders()
{
foreach (var uf in User2Folder) // remove existing relations
_Db.Delete(uf);
Connection.Delete(uf);
var nFolders = NbFolders;
var nUsers = NbUsers;
@ -75,14 +75,14 @@ public static partial class Db
UserId = user.Id,
FolderId = Random.Shared.Next(nFolders) + 1
};
_Db.Insert(relation);
Connection.Insert(relation);
}
}
private static void GiveFakeUsersAccessToInstallations()
{
foreach (var ui in User2Installation) // remove existing relations
_Db.Delete(ui);
Connection.Delete(ui);
var nbInstallations = NbInstallations;
@ -94,7 +94,7 @@ public static partial class Db
UserId = user.Id,
InstallationId = Random.Shared.Next(nbInstallations) + 1
};
_Db.Insert(relation);
Connection.Insert(relation);
}
}
}

View File

@ -1,64 +1,49 @@
using InnovEnergy.App.Backend.Model;
using InnovEnergy.Lib.Utils;
using SQLite;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static TableQuery<Folder> Folders => _Db.Table<Folder>();
public static Int32 NbFolders => Folders.Count();
public static Folder? GetFolderById(Int64 id)
{
return Folders.FirstOrDefault(u => u.Id == id);
// if (folder is null)
// return null;
//return PopulateDescendants(folder);
}
public static IEnumerable<Folder> GetChildFolders(Folder parent)
public static IEnumerable<Folder> GetChildFolders(this Folder parent)
{
return Folders.Where(f => f.ParentId == parent.Id);
}
public static IEnumerable<Installation> GetChildInstallations(Folder parent)
public static IEnumerable<Installation> GetChildInstallations(this Folder parent)
{
return Installations.Where(f => f.ParentId == parent.Id);
}
public static IEnumerable<Folder> GetDescendantFolders(Folder parent)
public static IEnumerable<Folder> GetDescendantFolders(this Folder parent)
{
return parent.Traverse(GetChildFolders);
}
public static Boolean IsDescendantOf(Folder folder, Int64 ancestorFolderId)
public static Boolean IsDescendantOf(this Folder folder, Int64 ancestorFolderId)
{
return Ancestors(folder)
.Any(u => u.Id == ancestorFolderId);
}
public static Boolean IsDescendantOf(Folder folder, Folder ancestor)
public static Boolean IsDescendantOf(this Folder folder, Folder ancestor)
{
return IsDescendantOf(folder, ancestor.Id);
}
private static IEnumerable<Folder> Ancestors(Folder child)
private static IEnumerable<Folder> Ancestors(this Folder child)
{
return child.Unfold(GetParent);
}
public static Folder? GetParent(Folder f)
public static Folder? GetParent(this Folder f)
{
return IsRoot(f)
? null
: GetFolderById(f.ParentId);
}
public static Boolean IsRoot(Folder f)
public static Boolean IsRoot(this Folder f)
{
return f.ParentId == 0; // root has ParentId 0 by definition
}

View File

@ -5,15 +5,7 @@ namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static TableQuery<Installation> Installations => _Db.Table<Installation>();
public static Int32 NbInstallations => Installations.Count();
public static Installation? GetInstallationById(Int64 id) => Installations
.FirstOrDefault(u => u.Id == id);
private static IEnumerable<Folder> Ancestors(Installation installation)
public static IEnumerable<Folder> Ancestors(this Installation installation)
{
var parentFolder = GetParent(installation);
@ -22,29 +14,29 @@ public static partial class Db
: Ancestors(parentFolder);
}
public static Folder? GetParent(Installation installation)
public static Folder? GetParent(this Installation installation)
{
return IsRoot(installation)
? null
: GetFolderById(installation.ParentId);
}
public static Boolean IsRoot(Installation i)
public static Boolean IsRoot(this Installation i)
{
return i.ParentId == 0; // root has ParentId 0 by definition
}
public static Int64 CreateInstallation(Installation installation)
public static Int64 CreateInstallation(this Installation installation)
{
return Create(installation);
}
public static Boolean UpdateInstallation(Installation installation)
public static Boolean UpdateInstallation(this Installation installation)
{
return Update(installation);
}
public static Boolean DeleteInstallation(Installation installation)
public static Boolean DeleteInstallation(this Installation installation)
{
User2Installation.Delete(i => i.InstallationId == installation.Id);

View File

@ -7,67 +7,57 @@ using System.Text.RegularExpressions;
using InnovEnergy.App.Backend.Model;
using InnovEnergy.App.Backend.Utils;
using InnovEnergy.Lib.Utils;
using SQLite;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static TableQuery<User> Users => _Db.Table<User>();
public static Int32 NbUsers => Users.Count();
public static IEnumerable<User> GetChildUsers(User parent)
public static IEnumerable<User> GetChildUsers(this User parent)
{
return Users.Where(f => f.ParentId == parent.Id);
return Users
.Where(f => f.ParentId == parent.Id);
}
public static IEnumerable<User> GetDescendantUsers(User parent)
public static IEnumerable<User> GetDescendantUsers(this User parent)
{
return parent.Traverse(GetChildUsers);
}
public static User? GetUserById(Int64 id)
{
return Users.FirstOrDefault(u => u.Id == id);
}
public static Boolean IsDescendantOf(User user, User ancestor)
public static Boolean IsDescendantOf(this User user, User ancestor)
{
return Ancestors(user)
.Any(u => u.Id == ancestor.Id);
}
private static IEnumerable<User> Ancestors(User child)
private static IEnumerable<User> Ancestors(this User child)
{
return child.Unfold(GetParent);
}
public static User? GetParent(User u)
public static User? GetParent(this User u)
{
return IsRoot(u)
? null
: GetUserById(u.ParentId);
}
public static Boolean IsRoot(User u)
public static Boolean IsRoot(this User u)
{
return u.ParentId == 0; // root has ParentId 0 by definition
}
public static Boolean HasDirectAccessToFolder(User user, Folder folder)
public static Boolean HasDirectAccessToFolder(this User user, Folder folder)
{
return HasDirectAccessToFolder(user, folder.Id);
}
public static Boolean HasDirectAccessToFolder(User user, Int64 folderId)
public static Boolean HasDirectAccessToFolder(this User user, Int64 folderId)
{
return User2Folder.Any(r => r.FolderId == folderId && r.UserId == user.Id);
}
public static Boolean HasAccessToFolder(User user, Int64 folderId)
public static Boolean HasAccessToFolder(this User user, Int64 folderId)
{
var folder = GetFolderById(folderId);
if (folder is null)

View File

@ -1,12 +0,0 @@
using InnovEnergy.App.Backend.Model.Relations;
using SQLite;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static TableQuery<User2Folder> User2Folder => _Db.Table<User2Folder>();
public static Int32 NbUser2Folder => User2Folder.Count();
}

View File

@ -1,11 +0,0 @@
using InnovEnergy.App.Backend.Model.Relations;
using SQLite;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
private static TableQuery<User2Installation> User2Installation => _Db.Table<User2Installation>();
public static Int32 NbUser2Installation => User2Installation.Count();
}