コード例 #1
0
        public void Validate(IEnumerable <OperationContainer> operations)
        {
            ArgumentGuard.NotNull(operations, nameof(operations));

            _localIdTracker.Reset();

            int operationIndex = 0;

            try
            {
                foreach (var operation in operations)
                {
                    ValidateOperation(operation);

                    operationIndex++;
                }
            }
            catch (JsonApiException exception)
            {
                foreach (var error in exception.Errors)
                {
                    error.Source.Pointer = $"/atomic:operations[{operationIndex}]" + error.Source.Pointer;
                }

                throw;
            }
        }
コード例 #2
0
        /// <inheritdoc />
        public virtual async Task <IList <OperationContainer> > ProcessAsync(IList <OperationContainer> operations,
                                                                             CancellationToken cancellationToken)
        {
            if (operations == null)
            {
                throw new ArgumentNullException(nameof(operations));
            }

            _localIdValidator.Validate(operations);
            _localIdTracker.Reset();

            var results = new List <OperationContainer>();

            await using var transaction = await _operationsTransactionFactory.BeginTransactionAsync(cancellationToken);

            try
            {
                foreach (var operation in operations)
                {
                    operation.SetTransactionId(transaction.TransactionId);

                    await transaction.BeforeProcessOperationAsync(cancellationToken);

                    var result = await ProcessOperation(operation, cancellationToken);

                    results.Add(result);

                    await transaction.AfterProcessOperationAsync(cancellationToken);
                }

                await transaction.CommitAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (JsonApiException exception)
            {
                foreach (var error in exception.Errors)
                {
                    error.Source.Pointer = $"/atomic:operations[{results.Count}]" + error.Source.Pointer;
                }

                throw;
            }
            catch (Exception exception)
            {
                throw new JsonApiException(new Error(HttpStatusCode.InternalServerError)
                {
                    Title  = "An unhandled error occurred while processing an operation in this request.",
                    Detail = exception.Message,
                    Source =
                    {
                        Pointer = $"/atomic:operations[{results.Count}]"
                    }
                }, exception);
            }

            return(results);
        }
コード例 #3
0
        /// <inheritdoc />
        public virtual async Task <IList <OperationContainer> > ProcessAsync(IList <OperationContainer> operations, CancellationToken cancellationToken)
        {
            ArgumentGuard.NotNull(operations, nameof(operations));

            _localIdValidator.Validate(operations);
            _localIdTracker.Reset();

            var results = new List <OperationContainer>();

            await using IOperationsTransaction transaction = await _operationsTransactionFactory.BeginTransactionAsync(cancellationToken);

            try
            {
                foreach (OperationContainer operation in operations)
                {
                    operation.SetTransactionId(transaction.TransactionId);

                    await transaction.BeforeProcessOperationAsync(cancellationToken);

                    OperationContainer result = await ProcessOperationAsync(operation, cancellationToken);

                    results.Add(result);

                    await transaction.AfterProcessOperationAsync(cancellationToken);
                }

                await transaction.CommitAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (JsonApiException exception)
            {
                foreach (Error error in exception.Errors)
                {
                    error.Source.Pointer = $"/atomic:operations[{results.Count}]" + error.Source.Pointer;
                }

                throw;
            }
#pragma warning disable AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
            catch (Exception exception)
#pragma warning restore AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
            {
                throw new JsonApiException(new Error(HttpStatusCode.InternalServerError)
                {
                    Title  = "An unhandled error occurred while processing an operation in this request.",
                    Detail = exception.Message,
                    Source =
                    {
                        Pointer = $"/atomic:operations[{results.Count}]"
                    }
                }, exception);
            }

            return(results);
        }
コード例 #4
0
        public void Validate(IEnumerable <OperationContainer> operations)
        {
            _localIdTracker.Reset();

            int operationIndex = 0;

            try
            {
                foreach (var operation in operations)
                {
                    if (operation.Kind == OperationKind.CreateResource)
                    {
                        DeclareLocalId(operation.Resource);
                    }
                    else
                    {
                        AssertLocalIdIsAssigned(operation.Resource);
                    }

                    foreach (var secondaryResource in operation.GetSecondaryResources())
                    {
                        AssertLocalIdIsAssigned(secondaryResource);
                    }

                    if (operation.Kind == OperationKind.CreateResource)
                    {
                        AssignLocalId(operation);
                    }

                    operationIndex++;
                }
            }
            catch (JsonApiException exception)
            {
                foreach (var error in exception.Errors)
                {
                    error.Source.Pointer = $"/atomic:operations[{operationIndex}]" + error.Source.Pointer;
                }

                throw;
            }
        }