Exemplo n.º 1
0
        /// <inheritdoc />
        public Task <List <T> > Handle(GetRangeRequest <T> request, CancellationToken cancellationToken, RequestHandlerDelegate <List <T> > 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 <List <T> > Handle()
            {
                var start = new GetRangeNotification(request.Logger);
                await _mediator.Publish(start, cancellationToken).ConfigureAwait(false);

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

                var finish = new GetRangeNotification <T>(response, request.Logger);
                await _mediator.Publish(finish, cancellationToken).ConfigureAwait(false);

                return(response);
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetAssets(
            [SwaggerParameter("The query options", Required = true), Required] ODataQueryOptions <Asset> options,
            CancellationToken cancellationToken)
        {
            var queryRequest = new QueryRequest <Asset>(nameof(MongoDB));
            var query        = await _mediator.Send(queryRequest, cancellationToken).ConfigureAwait(false);

            try
            {
                query = (IQueryable <Asset>)options.ApplyTo(query);
            }
            catch (ODataException e)
            {
                ModelState.AddModelError(nameof(options), e.Message);
                return(BadRequest(ModelState));
            }

            if (!User.IsInRole("Admin"))
            {
                query = query.Where(x => x.CreatedBy == Guid.Parse(User.FindFirstValue(Sub)));
            }

            var getRangeRequest = new GetRangeRequest <Asset>(nameof(MongoDB), query, _logger);
            var response        = await _mediator.Send(getRangeRequest, cancellationToken).ConfigureAwait(false);

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

            return(Ok(response));
        }
        public Task Handle(GetRangeNotification <T, TId> notification, CancellationToken cancellationToken)
        {
            if (notification == default)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            foreach (var(key, value) in notification.KeyValuePairs)
            {
                _cache.SetCacheEntry(key, value, _options);
            }

            var values = notification.KeyValuePairs.Select(x => new KeyValuePair <RedisKey, RedisValue>(x.Key.ToString(), Serialize(x.Value)));

            return(_database.StringSetAsync(values.ToArray(), flags: FireAndForget));
        }