コード例 #1
0
        public async Task <ApiResult> Handle(InsertClassifierType request, CancellationToken cancellationToken)
        {
            if (request.UserUid == Guid.Empty)
            {
                throw new InvalidOperationException("User is required.");
            }

            var item = request.Item ?? throw new ArgumentNullException(nameof(request.Item));

            using (var scope = _unitOfWorkFactory.Create())
            {
                var insertTypeResult = await _classifierTypeService.Insert(item, cancellationToken);

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

                var insertFieldResult = await _metadataService.Insert(new ManageFieldMetadataRequest
                {
                    EntityTypeCode = ClassifierType.TypeCode,
                    // ReSharper disable once PossibleInvalidOperationException
                    EntityUid = insertTypeResult.Uid.Value,
                    Items     = ClassifierMetadata.GetDefaultFields()
                }, cancellationToken);

                if (insertFieldResult.Success)
                {
                    scope.Commit();
                }

                return(insertTypeResult);
            }
        }
コード例 #2
0
        public async Task <ApiResult> Register(ClassifierType item, ICollection <FieldMetadata> fields, CancellationToken cancellationToken)
        {
            var type = await _classifierTypeService.TryGet(item.Code, cancellationToken);

            // todo: compare fields of registered classifier and fields from parameters to update them in db
            if (type != null)
            {
                _logger.LogDebug("Classifier type {code} already registered.", item.Code);

                return(new ApiResult {
                    AffectedRows = 0
                });
            }

            // todo: register numerator (get default from settings)

            using (var scope = _unitOfWorkFactory.Create())
            {
                var insertTypeResult = await _classifierTypeService.Insert(item, cancellationToken);

                insertTypeResult.AssertSuccess(() => $"Failed to register classifier type \"{item.Code}\"");

                // todo: throw if empty fields or get default fields?
                if (fields != null)
                {
                    var insertFieldResult = await _metadataService.Insert(new ManageFieldMetadataRequest
                    {
                        EntityTypeCode = ClassifierType.TypeCode,
                        // ReSharper disable once PossibleInvalidOperationException
                        EntityUid = insertTypeResult.Uid.Value,
                        Items     = fields
                    }, cancellationToken);

                    insertFieldResult.AssertSuccess(() => $"Failed to register classifier type \"{item.Code}\"");
                }

                scope.Commit();

                _logger.LogInformation("Classifier type {code} successfully registered.", item.Code);

                return(new ApiResult {
                    AffectedRows = 1, Uid = insertTypeResult.Uid
                });
            }
        }