using InnovEnergy.App.Backend.Model; using InnovEnergy.Lib.Utils; using SQLite; namespace InnovEnergy.App.Backend.Database; public static partial class Db { private static TableQuery Folders => _Db.Table(); 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 GetChildFolders(Folder parent) { return Folders.Where(f => f.ParentId == parent.Id); } public static IEnumerable GetChildInstallations(Folder parent) { return Installations.Where(f => f.ParentId == parent.Id); } public static IEnumerable GetDescendantFolders(Folder parent) { return parent.Traverse(GetChildFolders); } public static Boolean IsDescendantOf(Folder folder, Int64 ancestorFolderId) { return Ancestors(folder) .Any(u => u.Id == ancestorFolderId); } public static Boolean IsDescendantOf(Folder folder, Folder ancestor) { return IsDescendantOf(folder, ancestor.Id); } private static IEnumerable Ancestors(Folder child) { return child.Unfold(GetParent); } public static Folder? GetParent(Folder f) { return IsRoot(f) ? null : GetFolderById(f.ParentId); } public static Boolean IsRoot(Folder f) { return f.ParentId == 0; // root has ParentId 0 by definition } public static Int64 CreateFolder(Folder folder) { return Create(folder); } public static Boolean UpdateFolder(Folder folder) { // TODO: no circles in path return Update(folder); } // These should not be necessary, just Update folder/installation with new parentId // public Boolean ChangeParent(Installation child, Int64 parentId) // { // child.ParentId = parentId; // return UpdateInstallation(child); // } // // public Boolean ChangeParent(Folder child, Int64 parentId) // { // child.ParentId = parentId; // return UpdateFolder(child); // } public static Boolean DeleteFolder(Folder folder) { // Delete direct children User2Folder .Delete(f => f.FolderId == folder.Id); Installations.Delete(i => i.ParentId == folder.Id); // recursion Folders.Where(f => f.ParentId == folder.Id) .ForEach(DeleteFolder); return Delete(folder); } }