public async Task <SearchResult <ClassifierGroup> > Handle(GetClassifierGroupList request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var type = await _classifierTypeService.Get(request.TypeCode, cancellationToken);

            using (var db = _dbContextFactory.Create())
            {
                if (type.HierarchyType == HierarchyType.Groups)
                {
                    var tree = request.TreeUid != null
                                                ? await _classifierTreeService.GetClassifierTree(/*request.CompanyUid,*/ request.TypeCode, request.TreeUid.Value, cancellationToken)
                                                : await _classifierTreeService.GetClassifierTree(/*request.CompanyUid,*/ request.TypeCode, request.TreeCode, cancellationToken);

                    if (request.FocusUid != null)
                    {
                        return(await GetGroupsByFocus(db, type, tree, request, cancellationToken));
                    }

                    return(await GetGroupsByParent(db, type, tree, request.ParentUid, request, true));
                }

                if (type.HierarchyType == HierarchyType.Items)
                {
                    if (request.FocusUid != null)
                    {
                        return(await GetItemsByFocus(db, type, request, cancellationToken));
                    }

                    return(await GetItemsByParent(db, type, request.ParentUid, request, true));
                }
            }

            return(null);
        }
Beispiel #2
0
        /*private static async Task LinkToDefaultRoot(DbContext db, ClassifierType type, Guid itemUid, CancellationToken cancellationToken)
         * {
         *      var defaultRoot = await GetDefaultRoot(db, type, cancellationToken);
         *
         *      // todo: insert default root group?
         *      if (defaultRoot != null)
         *      {
         *              await db.GetTable<DbClassifierLink>()
         *                      .Value(x => x.GroupUid, defaultRoot.Uid)
         *                      .Value(x => x.ItemUid, itemUid)
         *                      .InsertAsync(cancellationToken);
         *      }
         * }
         *
         * // todo: move to ClassifierGroupService.GetDefaultRoot()
         * private static async Task<DbClassifierGroup> GetDefaultRoot(DbContext db, ClassifierType type, CancellationToken cancellationToken)
         * {
         *      return await (
         *              from @group in db.GetTable<DbClassifierGroup>()
         *              where @group.TypeUid == type.Uid && @group.Code == ClassifierTree.DefaultCode
         *              select @group
         *      ).SingleOrDefaultAsync(cancellationToken);
         * }*/

        public virtual async Task <ApiResult> Update(Classifier item, CancellationToken cancellationToken)
        {
            var type = await _classifierTypeService.Get(item.Type, cancellationToken);

            var tree = type.HierarchyType == HierarchyType.Groups
                                ? await _classifierTreeService.GetClassifierTree(/*request.CompanyUid,*/ type.Code, ClassifierTree.DefaultCode, cancellationToken)
                                : null;

            // todo: validate fields
            // todo: move to ClassifierValidator (?)
            var metadata = await _metadataService.GetMetadata(type, cancellationToken);

            var manageFieldDataRequest = new ManageFieldDataRequest
            {
                EntityTypeCode = Classifier.TypeCode,
                // ReSharper disable once PossibleInvalidOperationException
                EntityUid = item.Uid.Value,
                Metadata  = metadata,
                Item      = item
            };

            var result = await _fieldDataRepository.Validate(manageFieldDataRequest, cancellationToken);

            if (result.Success == false)
            {
                return(result);
            }

            ApiResult updateResult;

            using (var db = _dbContextFactory.Create())
            {
                updateResult = await UpdateInternal(db, type, tree, item, cancellationToken);

                if (updateResult.Success == false)
                {
                    return(updateResult);
                }
            }

            // update fields
            result = await _fieldDataRepository.Update(manageFieldDataRequest, cancellationToken);

            if (result.Success == false)
            {
                return(result);
            }

            return(updateResult);
        }