public async Task <IActionResult> CreateDbAccounts(MyDBAcctsResources acctsResource)

        {
            //throw new Exception();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //acctsResource.nodePath = HierarchyId.Parse(acctsResource.path);

            HierarchyId parentItem = HierarchyId.Parse(acctsResource.nodePath);

            //var parent = context.myDbAccts.FirstOrDefault(e => e.nodePath == HierarchyId.Parse(acctsResource.nodePath).GetAncestor(1));

            var lastItemInCurrentLevel = context.myDbAccts.Where(x => x.nodePath.GetAncestor(1) == parentItem)
                                         .OrderByDescending(x => x.nodePath)
                                         .FirstOrDefault();

            var child1Node = lastItemInCurrentLevel != null ? lastItemInCurrentLevel.nodePath : null;

            var newNode = parentItem.GetDescendant(child1Node, null);

            acctsResource.nodePath = newNode.ToString();

            var acct = mapper.Map <MyDBAcctsResources, MyDbAcct>(acctsResource);



            context.myDbAccts.Add(acct);

            //foreach (var item in purchaseResource.purchaseItems)
            //{
            //    context.purchaseItems.Add(item);

            //}


            await context.SaveChangesAsync();



            var result = mapper.Map <MyDbAcct, MyDBAcctsResources>(acct);



            return(Ok(result));
        }
Exemple #2
0
 public SqlHierarchyId GetDescendant(SqlHierarchyId child1, SqlHierarchyId child2) => new SqlHierarchyId(_imp.GetDescendant(
                                                                                                             child1.IsNull ? default(HierarchyId?) : child1._imp,
                                                                                                             child2.IsNull ? default(HierarchyId?) : child2._imp));