private async Task <ApiResult> InsertHierarchy( DbContext db, ClassifierType type, Classifier item, CancellationToken cancellationToken) { if (type.HierarchyType == HierarchyType.Groups) { // todo: validate group belongs to the same classifier // todo: validate selected group belong to default tree // what if insert classifier and no groups inserted before? /*if (request.TreeUid == null || request.ParentUid == null) * { * throw new InvalidOperationException("Classifier should belong to one of the default hierarchy group."); * }*/ // todo: should be linked to at least one main group? if (/*request.TreeUid != null &&*/ item.ParentUid != null) { // link to selected group await db.GetTable <DbClassifierLink>() .Value(x => x.GroupUid, item.ParentUid) .Value(x => x.ItemUid, item.Uid) .InsertAsync(cancellationToken); } // if group is not of default hierarchy, link to default hierarchy root /*var root = await GetRoot(db, request.ParentUid.Value, cancellationToken); * * if (root.Code != ClassifierTree.DefaultCode) * { * await LinkToDefaultRoot(db, type, itemUid, cancellationToken); * }*/ } else if (type.HierarchyType == HierarchyType.Items) { var closureTable = new ClosureTableHandler(db, type); // ReSharper disable once PossibleInvalidOperationException if (await closureTable.Insert(item.Uid.Value, item.ParentUid, cancellationToken) == false) { return(new ApiResult { Success = false, Errors = closureTable.Errors }); } } return(new ApiResult()); }
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()); }