Exemplo n.º 1
0
        public async Task ChangeOrCreateCommission([FromBody] ChangeCommissionRequest request)
        {
            var currencyId = (await _context.Currencies.FirstOrDefaultAsync(c => c.Name == request.CurrencyName)).Id;

            var userId = request.UserName == null
                ? null
                : (await _context.Users.FirstOrDefaultAsync(c => c.UserName == request.UserName)).Id;

            var commission = await _context.Commissions.FirstOrDefaultAsync(c =>
                                                                            c.OperationType == request.Type && c.CurrencyId == currencyId && c.UserId == userId);

            var dto = request.CommissionDto;

            if (commission == null)
            {
                commission =
                    CommissionManager.CreateCommission(currencyId, dto, request.Type, userId);
                await _context.AddAsync(commission);
            }
            else
            {
                commission.Rate          = dto.Rate;
                commission.Value         = dto.Value;
                commission.MaxCommission = dto.MaximalCommission;
                commission.MinCommission = dto.MinimalCommission;
                commission.Type          = dto.Type;
                commission.MaxValue      = dto.MaxOperationValue;
            }

            await _context.SaveChangesAsync();
        }
        public async Task InsertWalletAsync_DuplicateId_ThrowsArgumentException()
        {
            //// Arrange

            // Setup In-Memory Database at desired state

            DbContextOptions <WalletContext> dbContextOptions = new DbContextOptionsBuilder <WalletContext>()
                                                                .UseInMemoryDatabase(databaseName: "InsertWalletAsync_DuplicateId_ThrowsArgumentException")
                                                                .Options;

            WalletEntry originalWalletEntry =
                new WalletEntry {
                Id = "IAmDuplicate", EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
            };

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                await context.AddAsync(originalWalletEntry);

                context.SaveChanges();
            }

            // Initialize Entry

            WalletEntry duplicateEntry = new WalletEntry {
                Id = "IAmDuplicate", EventTime = DateTime.UtcNow, Amount = 10, BalanceBefore = 0
            };

            //// Act / Assert

            using (WalletContext context = new WalletContext(dbContextOptions))
            {
                IWalletRepository walletRepository = new WalletRepository(context);
                await Assert.ThrowsAsync <ArgumentException>(() =>
                                                             walletRepository.InsertWalletEntryAsync(duplicateEntry)
                                                             );
            }
        }