using InnovEnergy.App.Backend.Model; using InnovEnergy.Lib.Utils; namespace InnovEnergy.App.Backend.Database; public static partial class Db { public static IEnumerable GetChildFolders(this Folder parent) { return Folders.Where(f => f.ParentId == parent.Id); } public static IEnumerable GetChildInstallations(this Folder parent) { return Installations.Where(f => f.ParentId == parent.Id); } public static IEnumerable GetDescendantFolders(this Folder parent) { return parent.Traverse(GetChildFolders); } public static Boolean IsDescendantOf(this Folder folder, Int64 ancestorFolderId) { return Ancestors(folder) .Any(u => u.Id == ancestorFolderId); } public static Boolean IsDescendantOf(this Folder folder, Folder ancestor) { return IsDescendantOf(folder, ancestor.Id); } private static IEnumerable Ancestors(this Folder child) { return child.Unfold(GetParent); } public static Folder? GetParent(this Folder f) { return IsRoot(f) ? null : GetFolderById(f.ParentId); } public static Boolean IsRoot(this 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); } }