Пример #1
0
        public async Task <IEnumerable <TransactionModel> > GetAsync(TransactionQuery transactionQuery)
        {
            List <TransactionModel> result;
            var filterBuilder = Builders <TransactionEntity> .Filter;
            FilterDefinition <TransactionEntity> filter = filterBuilder.Empty;

            if (!string.IsNullOrEmpty(transactionQuery.FromAddress))
            {
                FilterDefinition <TransactionEntity> filterFrom = filterBuilder.Eq(x => x.From, transactionQuery.FromAddress);
                filter = filter & filterFrom;
            }

            if (!string.IsNullOrEmpty(transactionQuery.ToAddress))
            {
                FilterDefinition <TransactionEntity> filterTo = filterBuilder.Eq(x => x.To, transactionQuery.ToAddress);

                filter = transactionQuery.FromAddress == transactionQuery.ToAddress ? filter | filterTo : filter & filterTo;
            }

            if (transactionQuery.StartDate.HasValue)
            {
                int unixTime = transactionQuery.StartDate.Value.GetUnixTime();
                FilterDefinition <TransactionEntity> filterStartDate = filterBuilder.Gte(x => x.BlockTimestamp, (uint)unixTime);
                filter = filter & filterStartDate;
            }

            if (transactionQuery.EndDate.HasValue)
            {
                int unixTime = transactionQuery.EndDate.Value.GetUnixTime();
                FilterDefinition <TransactionEntity> filterEndDate = filterBuilder.Lte(x => x.BlockTimestamp, (uint)unixTime);
                filter = filter & filterEndDate;
            }

            var sort = Builders <TransactionEntity> .Sort.Descending(x => x.BlockNumber);

            MongoDB.Driver.
            IFindFluent <TransactionEntity, TransactionEntity> search = _collection.Find(filter);

            result = new List <TransactionModel>();

            if (!transactionQuery.ShouldSkipSorting)
            {
                search = search.Sort(sort);
            }

            transactionQuery.Start = transactionQuery.Start.HasValue ? transactionQuery.Start : 0;
            transactionQuery.Count = transactionQuery.Count.HasValue && transactionQuery.Count != 0 ? transactionQuery.Count : (int)await search.CountAsync();

            result = new List <TransactionModel>(transactionQuery.Count.Value);
            search = search.Skip(transactionQuery.Start).Limit(transactionQuery.Count);

            await search.ForEachAsync(transactionEntity =>
            {
                TransactionModel transactionModel = _mapper.Map <TransactionModel>(transactionEntity);
                result.Add(transactionModel);
            });

            return(result);
        }
Пример #2
0
        public async Task <IEnumerable <InternalMessageModel> > GetAllByFilterAsync(InternalMessageQuery internalMessageQuery)
        {
            List <InternalMessageModel> result = new List <InternalMessageModel>();
            var filterBuilder = Builders <InternalMessageEntity> .Filter;
            var filter        = Builders <InternalMessageEntity> .Filter.Empty;

            if (!string.IsNullOrEmpty(internalMessageQuery.FromAddress))
            {
                FilterDefinition <InternalMessageEntity> filterFrom = filterBuilder.Eq(x => x.FromAddress, internalMessageQuery.FromAddress);
                filter = filter & filterFrom;
            }

            if (!string.IsNullOrEmpty(internalMessageQuery.ToAddress))
            {
                FilterDefinition <InternalMessageEntity> filterTo = filterBuilder.Eq(x => x.ToAddress, internalMessageQuery.ToAddress);

                filter = internalMessageQuery.FromAddress == internalMessageQuery.ToAddress ? filter | filterTo : filter & filterTo;
            }

            if (internalMessageQuery.StartBlock.HasValue)
            {
                FilterDefinition <InternalMessageEntity> filterStartBlock =
                    filterBuilder.Gte(x => x.BlockNumber, internalMessageQuery.StartBlock.Value);
                filter = filter & filterStartBlock;
            }

            if (internalMessageQuery.StopBlock.HasValue)
            {
                FilterDefinition <InternalMessageEntity> filterEndBlock =
                    filterBuilder.Lte(x => x.BlockNumber, internalMessageQuery.StopBlock.Value);
                filter = filter & filterEndBlock;
            }

            var sort = Builders <InternalMessageEntity> .Sort
                       .Descending(x => x.BlockNumber)
                       .Ascending(x => x.TransactionHash)
                       .Ascending(x => x.MessageIndex);

            MongoDB.Driver.IFindFluent <InternalMessageEntity, InternalMessageEntity> search = _collection.Find(filter);
            search = search.Sort(sort);

            internalMessageQuery.Start = internalMessageQuery.Start.HasValue ? internalMessageQuery.Start : 0;
            internalMessageQuery.Count = internalMessageQuery.Count.HasValue && internalMessageQuery.Count != 0 ? internalMessageQuery.Count : (int)await search.CountAsync();

            search = search.Skip(internalMessageQuery.Start).Limit(internalMessageQuery.Count);
            result = new List <InternalMessageModel>(internalMessageQuery.Count.Value);
            IAsyncCursor <InternalMessageEntity> cursor = await search.ToCursorAsync();

            await cursor.ForEachAsync((internalMessage) =>
            {
                InternalMessageModel internalMessageModel = _mapper.Map <InternalMessageModel>(internalMessage);
                result.Add(internalMessageModel);
            });

            return(result);
        }