Move tables to main DB class, convert entity related function to extensions
This commit is contained in:
parent
5a082c22e8
commit
d974a7f176
|
@ -10,9 +10,40 @@ public static partial class Db
|
||||||
{
|
{
|
||||||
internal const String DbPath = "./db.sqlite";
|
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")]
|
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
|
||||||
static Db()
|
static Db()
|
||||||
|
@ -38,8 +69,8 @@ public static partial class Db
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Db.Insert(treeNode);
|
Connection.Insert(treeNode);
|
||||||
return SQLite3.LastInsertRowid(_Db.Handle);
|
return SQLite3.LastInsertRowid(Connection.Handle);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -52,7 +83,7 @@ public static partial class Db
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Db.Insert(session);
|
Connection.Insert(session);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -66,7 +97,7 @@ public static partial class Db
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Db.InsertOrReplace(treeNode);
|
Connection.InsertOrReplace(treeNode);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -80,7 +111,7 @@ public static partial class Db
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Db.Delete(treeNode);
|
Connection.Delete(treeNode);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -141,7 +172,7 @@ public static partial class Db
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Db.Insert(con);
|
Connection.Insert(con);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -160,7 +191,7 @@ public static partial class Db
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_Db.Insert(con);
|
Connection.Insert(con);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
@ -6,7 +6,7 @@ public static partial class Db
|
||||||
{
|
{
|
||||||
public static void CreateFakeRelations()
|
public static void CreateFakeRelations()
|
||||||
{
|
{
|
||||||
_Db.RunInTransaction(() =>
|
Connection.RunInTransaction(() =>
|
||||||
{
|
{
|
||||||
CreateFakeUserTree();
|
CreateFakeUserTree();
|
||||||
CreateFakeFolderTree();
|
CreateFakeFolderTree();
|
||||||
|
@ -62,7 +62,7 @@ public static partial class Db
|
||||||
private static void GiveFakeUsersAccessToFolders()
|
private static void GiveFakeUsersAccessToFolders()
|
||||||
{
|
{
|
||||||
foreach (var uf in User2Folder) // remove existing relations
|
foreach (var uf in User2Folder) // remove existing relations
|
||||||
_Db.Delete(uf);
|
Connection.Delete(uf);
|
||||||
|
|
||||||
var nFolders = NbFolders;
|
var nFolders = NbFolders;
|
||||||
var nUsers = NbUsers;
|
var nUsers = NbUsers;
|
||||||
|
@ -75,14 +75,14 @@ public static partial class Db
|
||||||
UserId = user.Id,
|
UserId = user.Id,
|
||||||
FolderId = Random.Shared.Next(nFolders) + 1
|
FolderId = Random.Shared.Next(nFolders) + 1
|
||||||
};
|
};
|
||||||
_Db.Insert(relation);
|
Connection.Insert(relation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GiveFakeUsersAccessToInstallations()
|
private static void GiveFakeUsersAccessToInstallations()
|
||||||
{
|
{
|
||||||
foreach (var ui in User2Installation) // remove existing relations
|
foreach (var ui in User2Installation) // remove existing relations
|
||||||
_Db.Delete(ui);
|
Connection.Delete(ui);
|
||||||
|
|
||||||
var nbInstallations = NbInstallations;
|
var nbInstallations = NbInstallations;
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ public static partial class Db
|
||||||
UserId = user.Id,
|
UserId = user.Id,
|
||||||
InstallationId = Random.Shared.Next(nbInstallations) + 1
|
InstallationId = Random.Shared.Next(nbInstallations) + 1
|
||||||
};
|
};
|
||||||
_Db.Insert(relation);
|
Connection.Insert(relation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,64 +1,49 @@
|
||||||
using InnovEnergy.App.Backend.Model;
|
using InnovEnergy.App.Backend.Model;
|
||||||
using InnovEnergy.Lib.Utils;
|
using InnovEnergy.Lib.Utils;
|
||||||
using SQLite;
|
|
||||||
|
|
||||||
namespace InnovEnergy.App.Backend.Database;
|
namespace InnovEnergy.App.Backend.Database;
|
||||||
|
|
||||||
public static partial class Db
|
public static partial class Db
|
||||||
{
|
{
|
||||||
private static TableQuery<Folder> Folders => _Db.Table<Folder>();
|
public static IEnumerable<Folder> GetChildFolders(this Folder parent)
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
return Folders.Where(f => f.ParentId == parent.Id);
|
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);
|
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);
|
return parent.Traverse(GetChildFolders);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean IsDescendantOf(Folder folder, Int64 ancestorFolderId)
|
public static Boolean IsDescendantOf(this Folder folder, Int64 ancestorFolderId)
|
||||||
{
|
{
|
||||||
return Ancestors(folder)
|
return Ancestors(folder)
|
||||||
.Any(u => u.Id == ancestorFolderId);
|
.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);
|
return IsDescendantOf(folder, ancestor.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<Folder> Ancestors(Folder child)
|
private static IEnumerable<Folder> Ancestors(this Folder child)
|
||||||
{
|
{
|
||||||
return child.Unfold(GetParent);
|
return child.Unfold(GetParent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Folder? GetParent(Folder f)
|
public static Folder? GetParent(this Folder f)
|
||||||
{
|
{
|
||||||
return IsRoot(f)
|
return IsRoot(f)
|
||||||
? null
|
? null
|
||||||
: GetFolderById(f.ParentId);
|
: 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
|
return f.ParentId == 0; // root has ParentId 0 by definition
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,15 +5,7 @@ namespace InnovEnergy.App.Backend.Database;
|
||||||
|
|
||||||
public static partial class Db
|
public static partial class Db
|
||||||
{
|
{
|
||||||
|
public static IEnumerable<Folder> Ancestors(this Installation installation)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
var parentFolder = GetParent(installation);
|
var parentFolder = GetParent(installation);
|
||||||
|
|
||||||
|
@ -22,29 +14,29 @@ public static partial class Db
|
||||||
: Ancestors(parentFolder);
|
: Ancestors(parentFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Folder? GetParent(Installation installation)
|
public static Folder? GetParent(this Installation installation)
|
||||||
{
|
{
|
||||||
return IsRoot(installation)
|
return IsRoot(installation)
|
||||||
? null
|
? null
|
||||||
: GetFolderById(installation.ParentId);
|
: 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
|
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);
|
return Create(installation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean UpdateInstallation(Installation installation)
|
public static Boolean UpdateInstallation(this Installation installation)
|
||||||
{
|
{
|
||||||
return Update(installation);
|
return Update(installation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean DeleteInstallation(Installation installation)
|
public static Boolean DeleteInstallation(this Installation installation)
|
||||||
{
|
{
|
||||||
User2Installation.Delete(i => i.InstallationId == installation.Id);
|
User2Installation.Delete(i => i.InstallationId == installation.Id);
|
||||||
|
|
||||||
|
|
|
@ -7,67 +7,57 @@ using System.Text.RegularExpressions;
|
||||||
using InnovEnergy.App.Backend.Model;
|
using InnovEnergy.App.Backend.Model;
|
||||||
using InnovEnergy.App.Backend.Utils;
|
using InnovEnergy.App.Backend.Utils;
|
||||||
using InnovEnergy.Lib.Utils;
|
using InnovEnergy.Lib.Utils;
|
||||||
using SQLite;
|
|
||||||
|
|
||||||
|
|
||||||
namespace InnovEnergy.App.Backend.Database;
|
namespace InnovEnergy.App.Backend.Database;
|
||||||
|
|
||||||
|
|
||||||
public static partial class Db
|
public static partial class Db
|
||||||
{
|
{
|
||||||
private static TableQuery<User> Users => _Db.Table<User>();
|
public static IEnumerable<User> GetChildUsers(this User parent)
|
||||||
|
|
||||||
public static Int32 NbUsers => Users.Count();
|
|
||||||
|
|
||||||
public static IEnumerable<User> GetChildUsers(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);
|
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)
|
return Ancestors(user)
|
||||||
.Any(u => u.Id == ancestor.Id);
|
.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);
|
return child.Unfold(GetParent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User? GetParent(User u)
|
public static User? GetParent(this User u)
|
||||||
{
|
{
|
||||||
return IsRoot(u)
|
return IsRoot(u)
|
||||||
? null
|
? null
|
||||||
: GetUserById(u.ParentId);
|
: 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
|
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);
|
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);
|
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);
|
var folder = GetFolderById(folderId);
|
||||||
if (folder is null)
|
if (folder is null)
|
||||||
|
|
|
@ -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();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue