Beispiel #1
0
        private IMongoQueryable <Actor> BuildQuery
        (
            DbGetActorFilterRequest request
        )
        {
            var query = DbContext.Db.GetCollection <Actor>
                            (Defaults.ActorCollectionName)
                        .AsQueryable();

            if (!string.IsNullOrWhiteSpace(request.Contains))
            {
                query = query.WhereText(request.Contains);
            }

            if (request.Archived.HasValue)
            {
                query = query.Where(x => x.IsArchived == request.Archived);
            }

            if (request.ActorRoleMask != 0)
            {
                var roles     = new HashSet <ActorRole>();
                var checkList = EnumerationClass.GetAll <ActorRole>();
                foreach (var item in checkList)
                {
                    if (request.ActorRoleMask.Contains(item))
                    {
                        roles.Add(item);
                    }
                }
                query = query.Where(x => roles.Contains(x.Role));
            }

            return(query);
        }
Beispiel #2
0
        public async Task <ListResponse <Actor> > Handle
            (DbGetActorFilterRequest request,
            CancellationToken cancellationToken)
        {
            try
            {
                Logger.LogInformation(LogEvent.DatabaseRequest,
                                      "Request={Request}.", request.ToDictionary());

                var validation = new DbGetActorFilterRequestValidator()
                                 .Validate(request);
                if (!validation.IsValid)
                {
                    Logger.LogWarning
                        (LogEvent.DatabaseRequestArgumentError,
                        "Database request validation" +
                        " error. Error={Error}.", validation.Errors);
                    return(ErrorResult("Database request validation error"));
                }

                var db         = DbContext.Db;
                var query      = BuildQuery(request);
                var totalCount = await query.CountAsync()
                                 .ConfigureAwait(false);

                if (totalCount == 0)
                {
                    return(new ListResponse <Actor>(new List <Actor>(),
                                                    totalCount));
                }

                query = ApplyPagination(query, request);
                var dbActorList = await query
                                  .ToListAsync()
                                  .ConfigureAwait(false);

                if (dbActorList is null)
                {
                    Logger.LogWarning
                        (LogEvent.DatabaseEmptyResponse,
                        "Database Actors null response.");
                    return(ErrorResult("Database Actors null response."));
                }

                var result = new ListResponse <Actor>(dbActorList, totalCount);
                return(result);
            }
            catch (Exception e)
            {
                Logger.LogWarning(LogEvent.DatabaseExceptionError, e,
                                  "Database exception error. Error={Error}.", e.Message);
                return(ErrorResult("Database exception error"));
            }
        }
Beispiel #3
0
 private IMongoQueryable <Actor> ApplyPagination(
     IMongoQueryable <Actor> query, DbGetActorFilterRequest request)
 {
     if (request.Offset > 0)
     {
         query = query.Skip(request.Offset);
     }
     if (request.Limit > 0)
     {
         query = query.Take(request.Limit);
     }
     return(query);
 }
        public static Dictionary <string, object> ToDictionary
            (this DbGetActorFilterRequest from)
        {
            if (from is null)
            {
                return(null);
            }

            return(new Dictionary <string, object>
            {
                { nameof(from.ActorRoleMask), from.ActorRoleMask },
                { nameof(from.Archived), from.Archived },
                { nameof(from.Contains), from.Contains },
                { nameof(from.Limit), from.Limit },
                { nameof(from.Offset), from.Offset },
            });
        }