105 lines
2.9 KiB
C#
105 lines
2.9 KiB
C#
|
namespace InnovEnergy.API.DataModel;
|
||
|
|
||
|
public static class FolderExtensions
|
||
|
{
|
||
|
public static IEnumerable<Path> 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<Path> DescendantFolders(this Folder folder) => folder.GetDescendantFolders();
|
||
|
|
||
|
public static IEnumerable<Path> 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<Path> DescendantUsers(this Folder folder)
|
||
|
{
|
||
|
IEnumerable<User> 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<Path> 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<VrmInstallation>()
|
||
|
.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();
|
||
|
|
||
|
}
|