Пример #1
0
        public static Dictionary <string, object> ToDictionary <TEntity>
            (this DbListRequest <TEntity> from) where TEntity : class
        {
            if (from is null)
            {
                return(null);
            }

            return(new Dictionary <string, object>
            {
                { nameof(from.Ids), String.Join(',', from.Ids) }
            });
        }
Пример #2
0
        public async Task <ListResponse <Actor> > Handle
            (DbListRequest <Actor> request, CancellationToken cancellationToken)
        {
            try
            {
                Logger.LogInformation(LogEvent.DatabaseRequest,
                                      "Request={Request}.", request.ToDictionary());

                var validation = new DbListRequestValidator <Actor>()
                                 .Validate(request);
                if (!validation.IsValid)
                {
                    Logger.LogWarning
                        (LogEvent.DatabaseRequestArgumentError,
                        "Request validation error. Error={Error}.",
                        validation.Errors);
                    return(ErrorResult("Request validation error."));
                }

                var db     = DbContext.Db;
                var result = await db.GetCollection <Actor>
                                 (Defaults.ActorCollectionName)
                             .AsQueryable()
                             .Where(t => request.Ids.Contains(t.Id))
                             .ToListAsync()
                             .ConfigureAwait(false);

                if (result is null)
                {
                    Logger.LogWarning(LogEvent.DatabaseEmptyResponse,
                                      "Empty response.");
                    return(ErrorResult("Empty response"));
                }

                return(new ListResponse <Actor>(result, result.Count));
            }
            catch (Exception e)
            {
                Logger.LogError(LogEvent.DatabaseExceptionError, e,
                                "Exception error. Error={Error}", e.Message);
                return(ErrorResult("Exception error"));
            }
        }