namespace InnovEnergy.API.DataModel; public static class FolderExtensions { public static IEnumerable DescendantUserParents(this Folder folder) { foreach (var path in folder.GetDescendantFolders()) { yield return path; var f = path.Folder()!; foreach (var installation in f.Installations) yield return new Path(path, installation); } } public static IEnumerable DescendantFolders(this Folder folder) => folder.GetDescendantFolders(); public static IEnumerable DescendantInstallations(this Folder folder) { return from path in folder.DescendantFolders() let f = path.Element as Folder from installation in f.Installations select new Path(path, installation); } public static IEnumerable DescendantUsers(this Folder folder) { IEnumerable GetUsers(Folder f) => f .Installations .SelectMany(i => i.Users) .Concat(f.Users); return from path in folder.DescendantFolders() from user in GetUsers(path.Folder()) select new Path(path, user); } public static Path? FindDescendantUser(this Folder folder, String userName) { return folder .DescendantUsers() .FirstOrDefault(f => f.Element.Name == userName); } private static IEnumerable GetDescendantFolders(this Folder folder, Path? parent = null) { var path = new Path(parent, folder); yield return path; var descendantFolders = from f in folder.Folders from descendant in f.GetDescendantFolders(path) select descendant; foreach (var descendant in descendantFolders) yield return descendant; } public static Boolean Remove(this Folder folder, Installation installation) { return folder.Installations.Remove(installation); } public static void Add(this Folder folder, Installation installation) { folder.Installations.Add(installation); } public static Boolean Remove(this Folder folder, Folder child) { return folder.Folders.Remove(child); } public static void Add(this Folder folder, Folder child) { folder.Folders.Add(child); } public static VrmInstallation GetOrCreateVrmInstallation(this Folder folder, String uniqueId) { var vrmInstallation = folder .Installations .OfType() .FirstOrDefault(i => i.UniqueId == uniqueId); if (vrmInstallation is not null) return vrmInstallation; // installation exist, return it vrmInstallation = new VrmInstallation(); folder.Installations.Add(vrmInstallation); return vrmInstallation; } public static Boolean IsEmpty(this Folder folder) => !folder.Folders.Any() && !folder.Installations.Any(); }