using FamilyTreeAPI.Entities; using FamilyTreeAPI.Models; using Microsoft.EntityFrameworkCore; using System.Drawing.Text; namespace FamilyTreeAPI.Repository; public partial class PersonRepository { private TreeNode PopulateItem(Person model) { TreeNode treeNode = new(); treeNode.Label = model.FirstName; treeNode.Data = model.Id.ToString(); treeNode.Key = model.Id.ToString(); treeNode.Children = new(); return treeNode; } private bool UseFamilyCriteria(FamilyCriteria criteria, Person person) { bool result = false; if (criteria.UseFather && criteria.UseMother) { result = person.FatherId.GetValueOrDefault(0) == 0 && person.MotherId.GetValueOrDefault(0) == 0; } else if (criteria.UseFather) { result = person.FatherId.GetValueOrDefault(0) == 0; } else if (criteria.UseMother) { result = person.MotherId.GetValueOrDefault(0) == 0; } return result; } private bool UseChildFamilyCriteria(FamilyCriteria criteria, int id, Person person) { bool result = false; int parentId; if (criteria.UseFather) { parentId = person.FatherId.GetValueOrDefault(0); result = parentId == id; } else if (criteria.UseMother) { parentId = person.MotherId.GetValueOrDefault(0); result = parentId == id; } return result; } private List SplitListOfTopLevel(List list, FamilyCriteria criteria) { List result = new(); Person item; for (int i = list.Count - 1; i > -1; i--) { item = list[i]; if (UseFamilyCriteria(criteria,item)) { result.Add(item); list.RemoveAt(i); } } return result; } private List GetParentId(List list,int id, FamilyCriteria criteria, Func conditionFn) { List result = new(); Person item; for (int i = list.Count - 1; i > -1; i--) { item = list[i]; if (conditionFn(criteria,id, item)) { result.Add(item); list.RemoveAt(i); } } return result; } private List> PopulateChild(FamilyCriteria criteria,int id, List childList, Func conditionFn) { Person person; TreeNode treeNode; List> list = new(); List children = GetParentId(childList,id, criteria, conditionFn); for (int i = 0; i < children.Count; i++) { person = children[i]; treeNode = PopulateItem(person); list.Add(treeNode); // get children of this one too treeNode.Children = PopulateChild(criteria, person.Id, childList, UseChildFamilyCriteria); } return list; } public async Task>> > GetFamilyTreeBy(FamilyCriteria criteria) { int statusCode = -1; string error = ""; Person person; TreeNode treeNode; List> data = new(); try { var personList = await _context.Persons.ToListAsync(); var topList = SplitListOfTopLevel(personList, criteria); for (int i = 0; i < topList.Count; i++) { person = topList[i]; treeNode = PopulateItem(person); data.Add(treeNode); treeNode.Children = PopulateChild(criteria, person.Id, personList, UseChildFamilyCriteria); } statusCode = 1; } catch (Exception ex) { error = ex.ToString(); statusCode = -1; } return new ResultModel>> { Data = data, StatusCode = statusCode, Message = error }; } }