public async Task <IActionResult> PutCustomer(string id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <DepositResponse> Handle(DepositCommand command, CancellationToken cancellationToken)
        {
            var response = new DepositResponse();

            using (var trans = _context.Database.BeginTransaction())
            {
                try
                {
                    var wallet = _context.Wallets.Where
                                     (s => s.WalletTypeId == command.WalletTypeId)
                                 .FirstOrDefault();
                    decimal  BalanceBefore = wallet.Balance;
                    DateTime TransDate     = DateTime.Now;
                    if (wallet != null)
                    {
                        wallet.Balance = wallet.Balance + command.Amount;
                    }
                    await _context.SaveChangesAsync();

                    Transaction transaction = new Transaction
                    {
                        Amount            = command.Amount,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransDate         = TransDate,
                        TransactionTypeId = command.TransactionTypeId,
                        WalletDesId       = wallet.WalletId,
                        TransType         = TransactionTypeEnum.DEPOSIT.ToString(),
                        Fee = command.Fee
                    };
                    _context.Transactions.Add(transaction);
                    await _context.SaveChangesAsync();

                    TransactionLog transactionLog = new TransactionLog()
                    {
                        AccountId         = wallet.AccountId,
                        BalanceAfter      = wallet.Balance,
                        BalanceBefore     = BalanceBefore,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransactionDate   = TransDate,
                        TransactionId     = transaction.TransactionId,
                        TransactionTypeId = transaction.TransactionTypeId,
                    };
                    _context.TransactionLogs.Add(transactionLog);
                    await _context.SaveChangesAsync();

                    trans.Commit();
                    response.Data.Balance       = wallet.Balance;
                    response.Data.TransactionId = transaction.TransactionId;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    response.Code    = ErrorCode.GetError(ErrorCode.SystemError).Key;
                    response.Message = ErrorCode.GetError(ErrorCode.SystemError).Value;
                    Logger.Error(ex);
                }
            }
            return(await Task.FromResult(response));
        }
Пример #3
0
        public Task <LoginResponse> Handle(LoginCommand command, CancellationToken cancellationToken)
        {
            var response = new LoginResponse();

            try
            {
                var account = _context.Accounts
                              .Where(s => s.PinCode == command.PinCode)
                              .FirstOrDefault();
                if (account != null)
                {
                    var traceLogin = new TraceLogin
                    {
                        AccountId      = account.AccountId,
                        CountLoginFail = 0,
                        IsLoginSuccess = true,
                        TimeLogin      = DateTime.Now
                    };
                    _context.TraceLogin.Add(traceLogin);
                    _context.SaveChangesAsync();
                    response.Data.AccountName = account.AccountName;
                }
                else
                {
                    var traceLogin = _context.TraceLogin.LastOrDefault();
                    if (traceLogin != null)
                    {
                        traceLogin.CountLoginFail = traceLogin.CountLoginFail + 1;
                        if (traceLogin.CountLoginFail == 3)
                        {
                            account        = _context.Accounts.FirstOrDefault();
                            account.isLock = true;
                        }
                        _context.SaveChangesAsync();
                    }

                    response.Code    = ErrorCode.GetError(ErrorCode.PinWrong).Key;
                    response.Message = ErrorCode.GetError(ErrorCode.PinWrong).Value;
                }
            }
            catch (Exception ex)
            {
                response.Code    = ErrorCode.GetError(ErrorCode.SystemError).Key;
                response.Message = ErrorCode.GetError(ErrorCode.SystemError).Value;
                Logger.Error(ex);
            }
            return(Task.FromResult(response));
        }
Пример #4
0
        public async Task EditAsync_ShouldEditACard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var initCards = SampleData.CREDITCARDS;
                await db.AddRangeAsync(initCards);

                await db.SaveChangesAsync();

                IRepository <CreditCard> repository = new DbCreditCardRepository(db);

                var expectedCards = await repository.GetAllAsync();

                var cardToEdit = expectedCards.First();
                expectedCards.Remove(cardToEdit);

                cardToEdit.Balance -= 1.00M;
                expectedCards.Add(cardToEdit);

                // Act
                await repository.EditAsync(cardToEdit);

                var result = await repository.GetAllAsync();

                // Assert

                var actualCards = Assert.IsAssignableFrom <List <CreditCard> >(result);
                Assert.Equal(
                    expectedCards.OrderBy(x => x.Id).Select(x => (id: x.Id, number: x.Number, pin: x.Pin, balance: x.Balance, isValid: x.IsValid)),
                    actualCards.OrderBy(x => x.Id).Select(x => (id: x.Id, number: x.Number, pin: x.Pin, balance: x.Balance, isValid: x.IsValid)));
            }
        }
Пример #5
0
        public async Task AddAsync_ShouldAddANewAction()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedDbAction = ACTION_NOT_IN_SEEDING_ACTIONS;
                var initActions      = ACTION_RESULTS;
                if (initActions.Exists(a => a.Id == expectedDbAction.Id))
                {
                    throw new InvalidOperationException($"Seeding actions already contain the action that is not supposed to be there: {expectedDbAction}");
                }
                await db.AddRangeAsync(initActions);

                await db.SaveChangesAsync();

                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);

                var expectedActions = await repository.GetAllAsync();

                expectedActions.Add(expectedDbAction);

                // Act
                await repository.AddAsync(expectedDbAction);

                var result = await repository.GetAllAsync();

                // Assert

                var actualActions = Assert.IsAssignableFrom <List <UserActionResult> >(result);
                Assert.Equal(
                    expectedActions.OrderBy(x => x.Id).Select(x => (id: x.Id, cid: x.CreditCardId, time: x.TimeStamp, operationCode: x.OperationCode, withdrawal: x.WithdrawalAmount)),
                    actualActions.OrderBy(x => x.Id).Select(x => (id: x.Id, cid: x.CreditCardId, time: x.TimeStamp, operationCode: x.OperationCode, withdrawal: x.WithdrawalAmount)));
            }
        }
Пример #6
0
        public async Task AddAsync_ShouldAddANewCard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedDbCard = SampleData.CARD_NOT_ON_THE_LIST;
                var initCards      = SampleData.CREDITCARDS;
                if (initCards.Exists(cc => cc.Id == expectedDbCard.Id || cc.Number == expectedDbCard.Number))
                {
                    throw new InvalidOperationException($"Seeding cards already contain the card that is not supposed to be there: {expectedDbCard}");
                }
                await db.AddRangeAsync(initCards);

                await db.SaveChangesAsync();

                IRepository <CreditCard> repository = new DbCreditCardRepository(db);

                var expectedCards = await repository.GetAllAsync();

                expectedCards.Add(expectedDbCard);

                // Act
                await repository.AddAsync(expectedDbCard);

                var result = await repository.GetAllAsync();

                // Assert

                var actualCards = Assert.IsAssignableFrom <List <CreditCard> >(result);
                Assert.Equal(
                    expectedCards.OrderBy(x => x.Id).Select(x => (id: x.Id, number: x.Number, pin: x.Pin, balance: x.Balance, isValid: x.IsValid)),
                    actualCards.OrderBy(x => x.Id).Select(x => (id: x.Id, number: x.Number, pin: x.Pin, balance: x.Balance, isValid: x.IsValid)));
            }
        }
Пример #7
0
        public async Task EditAsync_ShouldEditACard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var initCards = ACTION_RESULTS;
                await db.AddRangeAsync(initCards);

                await db.SaveChangesAsync();

                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);

                var expectedCards = await repository.GetAllAsync();

                var cardToEdit = expectedCards.First();
                expectedCards.Remove(cardToEdit);

                cardToEdit.WithdrawalAmount += 1.00M;
                expectedCards.Add(cardToEdit);

                // Act
                await repository.EditAsync(cardToEdit);

                var result = await repository.GetAllAsync();

                // Assert

                var actualCards = Assert.IsAssignableFrom <List <UserActionResult> >(result);
                Assert.Equal(
                    expectedCards.OrderBy(x => x.Id).Select(x => (id: x.Id, cid: x.CreditCardId, time: x.TimeStamp, operationCode: x.OperationCode, withdrawal: x.WithdrawalAmount)),
                    actualCards.OrderBy(x => x.Id).Select(x => (id: x.Id, cid: x.CreditCardId, time: x.TimeStamp, operationCode: x.OperationCode, withdrawal: x.WithdrawalAmount)));
            }
        }
Пример #8
0
        public async Task AddAsync_ShouldNotAddAAction()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedAction = ACTION_RESULTS.First();
                await db.AddRangeAsync(ACTION_RESULTS);

                await db.SaveChangesAsync();

                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);
                var expectedActions = await repository.GetAllAsync();

                // Act
                Func <Task> action = async() => await repository.AddAsync(expectedAction);

                // Assert
                await Assert.ThrowsAsync <ArgumentException>(action);
            }
        }
Пример #9
0
        public async Task GetBuIdAsync_ShouldReturnNoCard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                await db.AddRangeAsync(SampleData.CREDITCARDS);

                await db.SaveChangesAsync();

                CreditCard expectedDbCard           = null;
                int        cardId                   = db.CreditCards.OrderBy(x => x.Id).Last().Id + 1;
                IRepository <CreditCard> repository = new DbCreditCardRepository(db);

                // Act
                var result = await repository.GetByIdAsync(cardId);

                // Assert
                Assert.Equal(expectedDbCard, result);
            }
        }
Пример #10
0
        public async Task GetBuIdAsync_ShouldReturnNoAction()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                await db.AddRangeAsync(ACTION_RESULTS);

                await db.SaveChangesAsync();

                UserActionResult expectedDbAction = null;
                int actionId = db.ActionResults.OrderBy(x => x.Id).Last().Id + 1;
                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);

                // Act
                var result = await repository.GetByIdAsync(actionId);

                // Assert
                Assert.Equal(expectedDbAction, result);
            }
        }
Пример #11
0
        public async Task GetBuIdAsync_ShouldReturnAAction()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedAction = ACTION_RESULTS.First();
                await db.AddRangeAsync(ACTION_RESULTS);

                await db.SaveChangesAsync();

                var expectedDbAction = ACTION_RESULTS.First();
                int actionId         = expectedDbAction.Id;
                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);

                // Act
                var result = await repository.GetByIdAsync(actionId);

                // Assert
                Assert.Equal(expectedDbAction, result);
            }
        }
Пример #12
0
        public async Task GetBuIdAsync_ShouldReturnACard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedCard = SampleData.CREDITCARDS.First();
                await db.AddRangeAsync(SampleData.CREDITCARDS);

                await db.SaveChangesAsync();

                var expectedDbCard = await db.CreditCards.Where(x => x.Number == expectedCard.Number).FirstAsync();

                int cardId = expectedDbCard.Id;
                IRepository <CreditCard> repository = new DbCreditCardRepository(db);

                // Act
                var result = await repository.GetByIdAsync(cardId);

                // Assert
                Assert.Equal(expectedDbCard, result);
            }
        }
Пример #13
0
        public async Task AddAsync_ShouldNotAddACard()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedCard = SampleData.CREDITCARDS.First();
                await db.AddRangeAsync(SampleData.CREDITCARDS);

                await db.SaveChangesAsync();

                var expectedDbCard = await db.CreditCards.Where(x => x.Number == expectedCard.Number).FirstAsync();

                IRepository <CreditCard> repository = new DbCreditCardRepository(db);
                var expectedCards = await repository.GetAllAsync();

                // Act
                Func <Task> action = async() => await repository.AddAsync(expectedDbCard);

                // Assert
                await Assert.ThrowsAsync <ArgumentException>(action);
            }
        }
Пример #14
0
        public async Task GetAllAsync_ShouldReturnSeedingActions()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var expectedResults = ACTION_RESULTS;
                await db.AddRangeAsync(expectedResults);

                await db.SaveChangesAsync();

                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);

                // Act
                var result = await repository.GetAllAsync();

                // Assert
                var actualMessages = Assert.IsAssignableFrom <List <UserActionResult> >(result);
                Assert.Equal(
                    expectedResults.OrderBy(x => x.Id).Select(x => (id: x.Id, cid: x.CreditCardId, time: x.TimeStamp, operationCode: x.OperationCode, withdrawal: x.WithdrawalAmount)),
                    actualMessages.OrderBy(x => x.Id).Select(x => (id: x.Id, cid: x.CreditCardId, time: x.TimeStamp, operationCode: x.OperationCode, withdrawal: x.WithdrawalAmount)));
            }
        }
Пример #15
0
        public async Task EditAsync_ShouldNotEdit()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var cardToEdit = ACTION_NOT_IN_SEEDING_ACTIONS;
                var initCards  = ACTION_RESULTS;
                if (initCards.Exists(cc => cc.Id == cardToEdit.Id))
                {
                    throw new InvalidOperationException($"Seeding cards already contain the card that is not supposed to be there: {cardToEdit}");
                }
                await db.AddRangeAsync(initCards);

                await db.SaveChangesAsync();

                IRepository <UserActionResult> repository = new DBUserActionResultRepository(db);

                // Act
                Func <Task> action = async() => await repository.EditAsync(cardToEdit);

                // Assert
                await Assert.ThrowsAsync <ArgumentException>(action);
            }
        }
Пример #16
0
        public async Task EditAsync_ShouldNotEdit()
        {
            using (var db = new ATMContext(TestOptions.TestDbContextOptions <ATMContext>()))
            {
                // Arrange
                var cardToEdit = SampleData.CARD_NOT_ON_THE_LIST;
                var initCards  = SampleData.CREDITCARDS;
                if (initCards.Exists(cc => cc.Id == cardToEdit.Id || cc.Number == cardToEdit.Number))
                {
                    throw new InvalidOperationException($"Seeding cards already contain the card that is not supposed to be there: {cardToEdit}");
                }
                await db.AddRangeAsync(initCards);

                await db.SaveChangesAsync();

                IRepository <CreditCard> repository = new DbCreditCardRepository(db);

                // Act
                Func <Task> action = async() => await repository.EditAsync(cardToEdit);

                // Assert
                await Assert.ThrowsAsync <ArgumentException>(action);
            }
        }
        public async Task <WithdrawResponse> Handle(WithdrawCommand command, CancellationToken cancellationToken)
        {
            var response = new WithdrawResponse();

            using (var trans = _context.Database.BeginTransaction())
            {
                try
                {
                    // hạn mức tối đa 5.000.000
                    if (command.Amount > 5000000)
                    {
                        response.Code    = ErrorCode.GetError(ErrorCode.OutRange).Key;
                        response.Message = ErrorCode.GetError(ErrorCode.OutRange).Value;
                        return(await Task.FromResult(response));
                    }


                    var wallet = _context.Wallets.Where
                                     (s => s.WalletTypeId == command.WalletTypeId)
                                 .FirstOrDefault();
                    // trong tk còn ít nhất 50.000
                    if (wallet.Balance < command.Amount - 50000 - command.Fee)
                    {
                        response.Code    = ErrorCode.GetError(ErrorCode.AmountNotEnough).Key;
                        response.Message = ErrorCode.GetError(ErrorCode.AmountNotEnough).Value;
                        return(await Task.FromResult(response));
                    }
                    decimal  BalanceBefore = wallet.Balance;
                    DateTime TransDate     = DateTime.Now;
                    if (wallet != null)
                    {
                        wallet.Balance = wallet.Balance - command.Amount - command.Fee;
                    }
                    await _context.SaveChangesAsync();

                    Transaction transaction = new Transaction
                    {
                        Amount            = command.Amount,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransDate         = TransDate,
                        TransactionTypeId = command.TransactionTypeId,
                        WalletDesId       = wallet.WalletId,
                        TransType         = TransactionTypeEnum.WITHDRAW.ToString(),
                        Fee = command.Fee
                    };
                    _context.Transactions.Add(transaction);
                    await _context.SaveChangesAsync();

                    TransactionLog transactionLog = new TransactionLog()
                    {
                        AccountId         = wallet.AccountId,
                        BalanceAfter      = wallet.Balance,
                        BalanceBefore     = BalanceBefore,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransactionDate   = TransDate,
                        TransactionId     = transaction.TransactionId,
                        TransactionTypeId = transaction.TransactionTypeId
                    };
                    _context.TransactionLogs.Add(transactionLog);
                    await _context.SaveChangesAsync();

                    trans.Commit();
                    response.Data.Balance       = wallet.Balance;
                    response.Data.TransactionId = transaction.TransactionId;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    response.Code    = ErrorCode.GetError(ErrorCode.SystemError).Key;
                    response.Message = ErrorCode.GetError(ErrorCode.SystemError).Value;
                    Logger.Error(ex);
                }
            }
            return(await Task.FromResult(response));
        }
Пример #18
0
 public async Task ChangePIN(Card card)
 {
     _context.Update(card);
     await _context.SaveChangesAsync();
 }
        public async Task <TransferResponse> Handle(TransferCommand command, CancellationToken cancellationToken)
        {
            var response = new TransferResponse();

            using (var trans = _context.Database.BeginTransaction())
            {
                try
                {
                    DateTime TransDate    = DateTime.Now;
                    var      walletSource = _context.Wallets.Where
                                                (s => s.WalletId == command.WalletSourceId)
                                            .FirstOrDefault();


                    if (walletSource.Balance < command.Amount - 50000 - command.Fee)
                    {
                        response.Code    = ErrorCode.GetError(ErrorCode.AmountNotEnough).Key;
                        response.Message = ErrorCode.GetError(ErrorCode.AmountNotEnough).Value;
                        return(await Task.FromResult(response));
                    }

                    decimal BeforeBalanceSource = walletSource.Balance;
                    if (walletSource != null)
                    {
                        walletSource.Balance = walletSource.Balance - command.Amount - command.Fee;
                    }
                    await _context.SaveChangesAsync();

                    var walletDes = _context.Wallets.Where
                                        (s => s.WalletId == command.WalletDesId)
                                    .FirstOrDefault();
                    decimal BeforeBalanceDes = walletDes.Balance;
                    if (walletDes != null)
                    {
                        walletDes.Balance = walletDes.Balance + command.Amount;
                    }
                    await _context.SaveChangesAsync();

                    Transaction transaction = new Transaction
                    {
                        Amount            = command.Amount,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransDate         = TransDate,
                        TransactionTypeId = command.TransactionTypeId,
                        WalletDesId       = walletDes.WalletId,
                        WalletSourceId    = walletSource.WalletId,
                        TransType         = TransactionTypeEnum.TRANSFER.ToString(),
                        Fee = command.Fee
                    };
                    _context.Transactions.Add(transaction);
                    await _context.SaveChangesAsync();

                    TransactionLog transactionLogSource = new TransactionLog()
                    {
                        AccountId         = walletSource.AccountId,
                        BalanceAfter      = walletSource.Balance,
                        BalanceBefore     = BeforeBalanceSource,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransactionDate   = TransDate,
                        TransactionId     = transaction.TransactionId,
                        TransactionTypeId = transaction.TransactionTypeId
                    };
                    _context.TransactionLogs.Add(transactionLogSource);
                    await _context.SaveChangesAsync();

                    TransactionLog transactionLogDes = new TransactionLog()
                    {
                        AccountId         = walletSource.AccountId,
                        BalanceAfter      = walletDes.Balance,
                        BalanceBefore     = BeforeBalanceDes,
                        Status            = TransactionStatusEnum.SUCCESS.ToString(),
                        TransactionDate   = TransDate,
                        TransactionId     = transaction.TransactionId,
                        TransactionTypeId = transaction.TransactionTypeId
                    };
                    _context.TransactionLogs.Add(transactionLogDes);
                    await _context.SaveChangesAsync();

                    response.Data.TransactionId = transaction.TransactionId;
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    response.Code    = ErrorCode.GetError(ErrorCode.SystemError).Key;
                    response.Message = ErrorCode.GetError(ErrorCode.SystemError).Value;
                    Logger.Error(ex);
                }
            }
            return(await Task.FromResult(response));
        }
Пример #20
0
 public async Task AddAsync(CreditCard entity)
 {
     _context.Add(entity);
     await _context.SaveChangesAsync();
 }
Пример #21
0
 public async Task AddAsync(UserActionResult entity)
 {
     _context.Add(entity);
     await _context.SaveChangesAsync();
 }