using static InnovEnergy.API.Result; namespace InnovEnergy.API.DataModel; public static class PathExtensions { public static Boolean IsRoot(this Path path) => path.Parent is null; public static Installation? Installation(this Path? path) => path?.Element as Installation; public static Folder? Folder (this Path? path) => path?.Element as Folder; public static User? User (this Path? path) => path?.Element as User; public static UserParent? UserParent (this Path? path) => path?.Element as UserParent; public static Boolean IsInstallation(this Path? path) => path?.Element is Installation; public static Boolean IsFolder (this Path? path) => path?.Element is Folder; public static Boolean IsUser (this Path? path) => path?.Element is User; public static Boolean Exists(this Path? path) => path?.Element is not null; public static IEnumerable Children(this Path? path) => from child in path?.Element.GetChildren() select new Path(path, child); public static Result MoveTo(this Path sourcePath, Path targetPath) { var add = targetPath.AddChild(sourcePath.Element); if (add.Succeeded) { var delete = sourcePath.Delete(); if (delete.Succeeded) return Success($"moved {sourcePath} to {targetPath}"); var revert = targetPath.Delete(); if (revert.Failed) throw new Exception($"failed to move {sourcePath} to {targetPath}"); } return Failure($"failed to move {sourcePath} to {targetPath}"); } public static Result Delete(this Path path) { var child = path.Element; var parent = path.Parent?.Element; if (parent is Folder folder) { if (child is Folder f) { if (!f.IsEmpty()) return Failure($"Cannot delete folder '{f.Name}'.\nFolder is not empty."); if (folder.Remove(f)) return Success($"Deleted folder {path}"); } if (child is User u) { if (folder.Remove(u)) return Success($"Deleted user {path}"); } if (child is Installation i) { if (!i.IsEmpty()) return Failure($"Cannot delete installation '{i.Name}'.\nInstallation has active users."); if (folder.Remove(i)) return Success($"deleted installation {path}"); } } else if (parent is Installation installation && child is User user) { installation.Remove(user); return Success($"deleted user {path}"); } return Failure($"cannot delete {path}"); } public static Result AddChild(this Path path, DataElement child) { var parent = path.Element; if (parent.GetChildren().Any(c => c.Name == child.Name)) return Failure($"{path} already has an element with name {child.Name}"); if (parent is Folder folder) { if (child is Folder f) { folder.Add(f); return Success($"added folder {child.Name} to folder {path}"); } if (child is User u) { folder.Add(u); return Success($"added user {child.Name} to folder {path}"); } if (child is Installation i) { folder.Add(i); return Success($"added installation {child.Name} to folder {path}"); } } else if (parent is Installation installation && child is User user) { installation.Add(user); return Success($"added user {child.Name} to installation {path}"); } return Failure($"cannot add {child} to {path}"); } public static Path? GetDescendant(this Path path, params String[] stringPath) { return path.GetDescendant((IEnumerable)stringPath); } public static Path? GetDescendant(this Path path, IEnumerable stringPath) { foreach (var childName in stringPath) { var child = path .Element .GetChildren() .SingleOrDefault(c => c.Name == childName); if (child is null) return null; path = new Path(path, child); } return path; } // public static Path? GetDescendant(this Path path, String stringPath) // { // while (!stringPath.IsNullOrWhiteSpace()) // { // var childName = stringPath.UntilFirst('|'); // // var child = path // .Head // .GetChildren() // .SingleOrDefault(c => c.Name == childName); // // if (child is null) return null; // // stringPath = stringPath.AfterFirst('|'); // path = new Path(path, child); // } // // return path; // } }