Esempio n. 1
0
        private async Task <ApiResult> UpdateHierarchy(
            DbContext db, ClassifierType type, ClassifierTree tree, Classifier item, CancellationToken cancellationToken)
        {
            if (type.HierarchyType == HierarchyType.Groups)
            {
                // todo: combine with InsertClassifierLinkHandler in one service

                // delete other links in same tree
                await(
                    from link in db.GetTable <DbClassifierLink>().Where(x => x.ItemUid == item.Uid)
                    join groups in db.GetTable <DbClassifierGroup>() on link.GroupUid equals groups.Uid
                    where groups.TreeUid == tree.Uid
                    select link
                    ).DeleteAsync(cancellationToken);

                // todo: check parent belongs to default tree
                if (item.ParentUid != null)
                {
                    await db.GetTable <DbClassifierLink>()
                    .Value(x => x.GroupUid, item.ParentUid)
                    .Value(x => x.ItemUid, item.Uid)
                    .InsertAsync(cancellationToken);
                }
            }
            else if (type.HierarchyType == HierarchyType.Items)
            {
                var closureTable = new ClosureTableHandler(db, type);

                // ReSharper disable once PossibleInvalidOperationException
                if (await closureTable.Update(item.Uid.Value, item.ParentUid, cancellationToken) == false)
                {
                    return(new ApiResult {
                        Success = false, Errors = closureTable.Errors
                    });
                }
            }

            return(new ApiResult());
        }