Exemplo n.º 1
0
        public void Get_PersonFound_ReturnsParentsTree()
        {
            // Arrange
            var foundParentsTree = new ParentsTree
            {
                Id       = 87,
                Name     = "Homer",
                LastName = "Simpson",
                Parents  = new List <ParentsTree>
                {
                    new ParentsTree
                    {
                        Id       = 34,
                        Name     = "Abraham",
                        LastName = "Simpson"
                    }
                }
            };

            _repositoryMock.Setup(r => r.GetParentsTree(87)).Returns(foundParentsTree);

            // Act
            var result = _controller.Get(87) as OkObjectResult;

            // Assert
            Assert.AreEqual(200, result.StatusCode);
            Assert.AreEqual(foundParentsTree, result.Value);
        }
Exemplo n.º 2
0
 public IActionResult Get(long id)
 {
     try
     {
         ParentsTree parentsTree = _peopleRepository.GetParentsTree(id);
         return(parentsTree != null ? (IActionResult)Ok(parentsTree) : NoContent());
     }
     catch (Exception ex)
     {
         return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
     }
 }
Exemplo n.º 3
0
        public static IEnumerable <ParentsTree> GetParents(this ParentsTree root)
        {
            var parentTrees = new Stack <ParentsTree>(new[] { root });

            while (parentTrees.Any())
            {
                ParentsTree parentTree = parentTrees.Pop();
                yield return(parentTree);

                foreach (var p in parentTree.Parents)
                {
                    parentTrees.Push(p);
                }
            }
        }
Exemplo n.º 4
0
        public ParentsTree GetParentsTree(long id)
        {
            ParentsTree parentsTree = null;
            // With this MATCH we will get all paths throw IS_PARENT_OF that end with the Person with the filtered {Id}
            // Variable r will contain a list of Relations starting from the top parent, i.e. Person1 -> Person2 -> PersonN -> Child
            // So using head function we will always take just the first relation from the list (i.e. Person1-> Person2)
            // Then, this statement will return a Paret, Child pair on each record.
            string statementTemplate   = "MATCH ()-[r:IS_PARENT_OF*]->(c:Person) WHERE ID(c) = {Id} RETURN endNode(head(r)) as child, startNode(head(r)) as parent order by length(r)";
            var    statementParameters = new Dictionary <string, object> {
                { "Id", id }
            };

            using (var session = _neo4jDriver.Session())
            {
                var result = session.Run(statementTemplate, statementParameters);
                // We will use this dictionary to allocate each Person just once on memory, since the same Person can appear more than once on the result (as child and as parent)
                var people = new Dictionary <long, ParentsTree>();
                foreach (var record in result)
                {
                    ParentsTree child;
                    ParentsTree parent;
                    if (!people.TryGetValue(record["child"].As <INode>().Id, out child))
                    {
                        // If the child is not found in the dictionary we will create the person (ParentsTree) and will add it to the dictionary
                        child = record["child"].As <INode>().ConvertToParentsTree();
                        people.Add(child.Id, child);
                        if (parentsTree == null)
                        {
                            // We will assign the first child we find to the result as the query is ordered by the length of relations list.
                            parentsTree = child;
                        }
                    }
                    if (!people.TryGetValue(record["parent"].As <INode>().Id, out parent))
                    {
                        // If the parent is not found in the dictionary we will create the person (ParentsTree) and will add it to the dictionary
                        parent = record["parent"].As <INode>().ConvertToParentsTree();
                        people.Add(parent.Id, parent);
                    }
                    child.Parents.Add(parent);
                }
            }
            return(parentsTree);
        }