/// <inheritdoc />
        public Task <Unit> Handle(CreateRangeRequest <T> request, CancellationToken cancellationToken, RequestHandlerDelegate <Unit> next)
        {
            if (request == default)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (next == default)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (!_options.UseScopedLogging)
            {
                return(Handle());
            }

            using (request.Scope)
            {
                return(Handle());
            }

            async Task <Unit> Handle()
            {
                var start = new CreateRangeNotification <T>(request.Models, request.Logger);
                await _mediator.Publish(start, cancellationToken).ConfigureAwait(false);

                var response = await next().ConfigureAwait(false);

                var finish = new CreateRangeNotification(request.Logger);
                await _mediator.Publish(finish, cancellationToken).ConfigureAwait(false);

                return(response);
            }
        }
示例#2
0
        public async Task <IActionResult> PostAssets(
            [SwaggerParameter("The Assets", Required = true), FromBody, Required] JsonElement element,
            CancellationToken cancellationToken)
        {
            if (element.ValueKind != JsonValueKind.Array)
            {
                ModelState.AddModelError(nameof(element), "Models must be an array");
                return(BadRequest(ModelState));
            }

            if (element.GetArrayLength() == 0)
            {
                ModelState.AddModelError(nameof(element), "Models cannot be empty");
                return(BadRequest(ModelState));
            }

            var models = element.EnumerateArray().Select(x => x.ToModel <Asset>()).ToList();

            if (models.Any(x => x.Id != default))
            {
                ModelState.AddModelError(nameof(element), "Ids must be empty");
                return(BadRequest(ModelState));
            }

            var userId = Guid.Parse(User.FindFirstValue(Sub));

            models.ForEach(model => model.CreatedBy = userId);
            var request = new CreateRangeRequest <Asset>(nameof(MongoDB), models, _logger);
            await _mediator.Send(request, cancellationToken).ConfigureAwait(false);

            var notification = new CreateRangeNotification <Asset, Guid>(models.ToDictionary(x => x.Id));
            await _mediator.Publish(notification, cancellationToken).ConfigureAwait(false);

            return(Ok(models));
        }