Beispiel #1
0
        public async Task <ExportTransactionsModel> Handle(ExportTransactionsQuery request, CancellationToken cancellationToken)
        {
            var exportTransactionModel = new ExportTransactionsModel();

            IQueryable <Transaction> queryable = _context.Transactions;

            if (request.Status.HasValue)
            {
                var status = _enumMapper.MapEnum <Domain.Enums.TransactionStatus, TransactionStatus>(request.Status.Value);
                queryable = queryable.Where(x => x.Status == status);
            }

            if (request.Type.HasValue)
            {
                var type = _enumMapper.MapEnum <Domain.Enums.TransactionType, TransactionType>(request.Type.Value);
                queryable = queryable.Where(x => x.Type == type);
            }

            var transactions = await queryable.Select(x => new TransactionRecord
            {
                Id         = x.Id,
                Status     = x.Status.ToString(),
                Type       = x.Type.ToString(),
                ClientName = x.ClientName,
                Amount     = x.Amount
            }).ToListAsync(cancellationToken);

            exportTransactionModel.Content     = _csvBuilder.BuildTransactionsFile(transactions);
            exportTransactionModel.ContentType = "text/csv";
            exportTransactionModel.FileName    = "Transactions.csv";

            return(await Task.FromResult(exportTransactionModel));
        }
        public async Task <Unit> Handle(UpsertTransactionsCommand request, CancellationToken cancellationToken)
        {
            var mappedTransactions = request.Transactions.Select(x =>
                                                                 new Transaction
            {
                Id         = x.Id,
                Status     = _enumMapper.MapEnum <Domain.Enums.TransactionStatus, TransactionStatus>(x.Status),
                Type       = _enumMapper.MapEnum <Domain.Enums.TransactionType, TransactionType>(x.Type),
                ClientName = x.ClientName,
                Amount     = x.Amount
            });

            var toInsert = mappedTransactions.Where(x => !_context.Transactions.Any(entity => entity.Id == x.Id)).ToList();
            var toUpdate = mappedTransactions.Where(x => !toInsert.Any(i => i.Id == x.Id)).ToList();

            if (toInsert.Any())
            {
                await _context.Transactions.AddRangeAsync(toInsert);
            }

            if (toUpdate.Any())
            {
                _context.Transactions.UpdateRange(toUpdate);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <TransactionsEnvelope> Handle(GetTransactionsQuery request, CancellationToken cancellationToken)
        {
            IQueryable <Transaction> queryable = _context.Transactions.AsNoTracking();

            if (request.Status.HasValue)
            {
                var status = _enumMapper.MapEnum <Domain.Enums.TransactionStatus, TransactionStatus>(request.Status.Value);
                queryable = queryable.Where(x => x.Status == status);
            }

            if (request.Type.HasValue)
            {
                var type = _enumMapper.MapEnum <Domain.Enums.TransactionType, TransactionType>(request.Type.Value);
                queryable = queryable.Where(x => x.Type == type);
            }

            List <TransactionDto> transactions;

            if (request.Limit == 0)
            {
                transactions = await queryable.Skip(request.Offset)
                               .Select(x => new TransactionDto
                {
                    Id         = x.Id,
                    Status     = x.Status.ToString(),
                    Type       = x.Type.ToString(),
                    Amount     = x.Amount,
                    ClientName = x.ClientName
                }).ToListAsync(cancellationToken);
            }
            else
            {
                transactions = await queryable.Skip(request.Offset)
                               .Take(request.Limit)
                               .Select(x => new TransactionDto
                {
                    Id         = x.Id,
                    Status     = x.Status.ToString(),
                    Type       = x.Type.ToString(),
                    Amount     = x.Amount,
                    ClientName = x.ClientName
                }).ToListAsync(cancellationToken);
            }

            var totalCount = queryable.Count();

            var envelope = new TransactionsEnvelope
            {
                TotalCount   = totalCount,
                Transactions = transactions
            };

            return(envelope);
        }
        public void ThrowsIfTooBigEnumValueIsPushed() => Assert.Throws <InvalidEnumValueException>(() =>
                                                                                                   TreeDefinitionBuilder.Create("AliasA", b =>
        {
            var context = CreateContext();

            b.PushAlias("AliasA", "NodeA");
            b.PushNode("NodeA");

            EnumMapper.MapEnum(b, context, typeof(BigLongEnum));
        }));
        public void EnumsCanBeMappedMultipleTimes(Type enumType)
        {
            var context = CreateContext();

            TreeDefinitionBuilder.Create("AliasA", b =>
            {
                b.PushAlias("AliasA", "NodeA");
                b.PushNode("NodeA");

                var definitionA = EnumMapper.MapEnum(b, context, enumType);
                var definitionB = EnumMapper.MapEnum(b, context, enumType);
                Assert.Equal(definitionA, definitionB);
            });
        }
        public async Task <Unit> Handle(UpdateTransactionCommand request, CancellationToken cancellationToken)
        {
            var transaction = await _context.Transactions.FindAsync(request.Id);

            if (transaction == null)
            {
                throw new NotFoundException($"Transaction with Id {request.Id} not found.");
            }

            var mappedStatus = _enumMapper.MapEnum <Domain.Enums.TransactionStatus, TransactionStatus>(request.Status);

            if (transaction.Status != mappedStatus)
            {
                transaction.Status = mappedStatus;
                await _context.SaveChangesAsync(cancellationToken);
            }

            return(Unit.Value);
        }
        public void EnumTypesAreMappedAccordingToSchemeRules(Type enumType, BuildEnum referenceBuilder)
        {
            var context = CreateContext();

            var tree = TreeDefinitionBuilder.Create("AliasA", b =>
            {
                b.PushAlias("AliasA", "NodeA");
                b.PushNode("NodeA");

                EnumMapper.MapEnum(b, context, enumType);
            });
            var expectedTree = TreeDefinitionBuilder.Create("AliasA", b =>
            {
                b.PushAlias("AliasA", "NodeA");
                b.PushNode("NodeA");

                referenceBuilder(b);
            });

            Assert.Equal(expectedTree, tree);
        }