public BalanceDto AddAmount(AdjustAmount adjustAmount) { var latestTransaction = transactions.Where(t => t.Value.CardId == adjustAmount.CardId).OrderByDescending(x => x.Value.Date).FirstOrDefault(); if (latestTransaction.Value == null) { return(null); } var newTransactionId = Guid.NewGuid(); var newTransaction = new Transaction() { TransactionId = newTransactionId, CardId = adjustAmount.CardId, Amount = adjustAmount.Amount, Date = DateTime.Now, Balance = latestTransaction.Value.Balance + adjustAmount.Amount }; if (transactions.TryAdd(newTransactionId.ToString(), newTransaction)) { return(GetBalance(adjustAmount.CardId)); } else { return(null); } }
public async Task AddMember_Database_Call_Is_Made_Once() { // Arrange var cardId = "ByDJ0lbYcPkzp2Ja"; var newAmount = new AdjustAmount { CardId = cardId, Amount = 10 }; var newBalance = new BalanceDto() { Balance = 10 }; var transactionRepo = new Mock <ITransactionsRepository>(); transactionRepo.Setup(x => x.AddAmount(newAmount)).Returns(newBalance); // Act var transactionService = new TransactionService(transactionRepo.Object); var result = transactionService.AddAmount(newAmount); // Assert transactionRepo.Verify(x => x.AddAmount(newAmount), Times.Once); }
public async Task AddAmount_Controller_Action_Returns_BadRequest_If_Amendment_Fails() { // Arrange var cardId = "ByDJ0lbYcPkzp2Ja"; var newAmount = new AdjustAmount { CardId = cardId, Amount = 10 }; var newBalance = new BalanceDto() { Balance = 10 }; var transactionService = new Mock <ITransactionService>(); transactionService.Setup(x => x.AddAmount(It.IsAny <AdjustAmount>())).Returns(() => null); // Act var controller = new TransactionController(transactionService.Object); var result = (StatusCodeResult)controller.AddAmount(newAmount); // Assert Assert.Equal(StatusCodes.Status400BadRequest, result.StatusCode); }
public async Task AddAmount_Controller_Action_Returns_Latest_Balance_When_Amount_Added() { // Arrange var cardId = "ByDJ0lbYcPkzp2Ja"; var newAmount = new AdjustAmount { CardId = cardId, Amount = 10 }; var newBalance = new BalanceDto() { Balance = 10 }; var transactionService = new Mock <ITransactionService>(); transactionService.Setup(x => x.AddAmount(newAmount)).Returns(newBalance); // Act var controller = new TransactionController(transactionService.Object); var result = controller.AddAmount(newAmount); var resultMember = (CreatedAtActionResult)result; // Assert Assert.Equal(newBalance, resultMember.Value); }
public ActionResult AddAmount([FromBody] AdjustAmount adjustAmount) { var response = _transactionService.AddAmount(adjustAmount); if (response == null) { return(BadRequest()); } return(CreatedAtAction(nameof(GetLatestBalance), new { CardId = adjustAmount.CardId }, response)); }
public BalanceDto AddAmount(AdjustAmount adjustAmount) { var lastestBalance = new BalanceDto(); try { _executers.ExecuteCommand(_connStr, conn => { var addAmount = conn.Query <AdjustAmount>(_commandText.AddAmountToCard, new { TransactionId = Guid.NewGuid(), CardId = adjustAmount.CardId, Amount = adjustAmount.Amount }); }); lastestBalance = GetBalance(adjustAmount.CardId); } catch (SqlException ex) when(ex.Number == 2627 | ex.Number == 547) { return(null); } return(lastestBalance); }
public BalanceDto AddAmount(AdjustAmount adjustAmount) { var addAmount = _transactionsRepo.AddAmount(adjustAmount); return(addAmount); }