Introduce Ancestors, GetParent

This commit is contained in:
ig 2023-03-09 11:12:56 +01:00
parent 99ef91b7be
commit 8935353e38
1 changed files with 14 additions and 3 deletions

View File

@ -30,14 +30,25 @@ public partial class Db
public Boolean IsParentOfChild(User parent, User child) public Boolean IsParentOfChild(User parent, User child)
{ {
return child return Ancestors(child)
.Unfold(GetParent)
.Any(u => u.Id == parent.Id); .Any(u => u.Id == parent.Id);
} }
private IEnumerable<User> Ancestors(User child)
{
return child.Unfold(GetParent);
}
public User? GetParent(User u) public User? GetParent(User u)
{ {
return GetUserById(u.ParentId); return IsRoot(u)
? null
: GetUserById(u.ParentId);
}
public static Boolean IsRoot(User u)
{
return u.ParentId == 0; // root has ParentId 0 by definition
} }
public User? GetUserByEmail(String email) => Users.FirstOrDefault(u => u.Email == email); public User? GetUserByEmail(String email) => Users.FirstOrDefault(u => u.Email == email);