예제 #1
0
        public void AccountBalanceDecreased_WhenUserWithdraws()
        {
            int accountId = 30;

            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;
            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context);

                context.Accounts.Add(new Account {
                    AccountId = accountId, Balance = 2000
                });
                context.SaveChanges();
            }

            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context);

                decimal balanceBefore    = accountQueriesHandler.GetAccount(accountId).Balance;
                decimal withdrawalAmount = 1000;

                accountCommandHandler.Withdraw(accountId, withdrawalAmount);

                decimal balanceAfter = accountQueriesHandler.GetAccount(accountId).Balance;

                Assert.Less(balanceAfter, balanceBefore);
            }
        }
 public AccountCommandHandlerTests()
 {
     _accountRepositoryMock = new Mock <IAccountRepository>();
     _jwtAuthServiceMock    = new Mock <IJwtAuthService>();
     _handler = new AccountCommandHandler(_accountRepositoryMock.Object, _jwtAuthServiceMock.Object);
     _accountRepositoryMock
     .Setup(x => x.GetUserByEmailAndPassword("*****@*****.**", "123"))
     .Returns(Task.FromResult(new User("Nicolas", "*****@*****.**", "123", UserRole.Admin)));
 }
예제 #3
0
        public void Setup()
        {
            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;

            systemClock               = Substitute.For <ISystemClock>();
            context                   = new BankAppDataContext(options);
            accountQueriesHandler     = new AccountQueriesHandler(context);
            accountCommandHandler     = new AccountCommandHandler(context);
            customerCommandHandler    = new CustomerCommandHandler(context);
            customerQueriesHandler    = new CustomerQueriesHandler(context);
            dispositionQueriesHandler = new DispositionQueriesHandler(context);
        }
예제 #4
0
        public AccountService()
        {
            IEventStoreConnection esConnection = EventStoreConnection.Create("ConnectTo=tcp://admin:changeit@localhost:1113");
            var conn = new EventStoreConnectionWrapper(esConnection);

            esConnection.Connected += (_, __) => { };
            //Console.WriteLine("Connected");
            esConnection.ConnectAsync().Wait();
            IStreamNameBuilder namer = new PrefixedCamelCaseStreamNameBuilder();
            IEventSerializer   ser   = new JsonMessageSerializer();

            _repo       = new StreamStoreRepository(namer, conn, ser);
            _cmdHandler = new AccountCommandHandler(_repo);

            var listener = new StreamListener("AccountAggregate", conn, namer, ser);

            _readModel = new BalanceReadModel(() => listener);
        }
예제 #5
0
        protected AccountTestsBase()
        {
            AccountId = Guid.NewGuid();
            IEventStoreConnection esConnection = EventStoreConnection.Create("ConnectTo=tcp://admin:changeit@localhost:1113");
            var conn = new EventStoreConnectionWrapper(esConnection);

            esConnection.Connected += (_, __) => { };
            esConnection.ConnectAsync().Wait();
            IStreamNameBuilder namer = new PrefixedCamelCaseStreamNameBuilder("Tests");
            IEventSerializer   ser   = new JsonMessageSerializer();

            Repo       = new StreamStoreRepository(namer, conn, ser);
            CmdHandler = new AccountCommandHandler(Repo);

            var listener = new StreamListener("AccountAggregate", conn, namer, ser);

            ReadModel = new BalanceReadModel(() => listener);
        }
예제 #6
0
        public void InterestIsCorrectlyApplied_WhenUserActivatesInterest()
        {
            int accountId = 78;

            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;
            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                context.Accounts.Add(new Account {
                    AccountId = accountId, Balance = 2000
                });
                context.SaveChanges();
            }

            using (var context = new BankAppDataContext(options))
            {
                var systemClock = Substitute.For <ISystemClock>();
                systemClock.GetCurrentTime().Returns(new DateTime(2020, 4, 4, 14, 0, 0, DateTimeKind.Utc));
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                decimal balanceBefore = accountQueriesHandler.GetAccount(accountId).Balance;

                double rate = 2;

                DateTime latestInterestDate = new DateTime(2018, 02, 02);
                var      currentDate        = new DateTime(2020, 4, 4, 14, 0, 0, DateTimeKind.Utc);

                accountCommandHandler.ApplyInterest(accountId, rate, latestInterestDate);

                decimal balanceAfter = accountQueriesHandler.GetAccount(accountId).Balance;
                double  days         = (currentDate - latestInterestDate).TotalDays;

                Assert.AreEqual(balanceAfter, Decimal.Round(balanceBefore + (decimal)((double)balanceBefore * rate / 100 / 365 * days), 2));
            }
        }
        public IActionResult DeleteAccount(int accountId)
        {
            if (accountId == null)
            {
                return(BadRequest());
            }

            try
            {
                var command = new DeleteAccountCommand(accountId);

                var commandHandler = new AccountCommandHandler(_accountRepository);

                commandHandler.Handle(command);

                return(Ok());
            }
            catch (CommandValidationException <eAccountError> ex)
            {
                return(BadRequest(ex.Error.ToString()));
            }
        }
예제 #8
0
        public void TransactionIsCreated_WhenUserAppliesInterest()
        {
            int accountId = 77;

            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;
            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                context.Accounts.Add(new Account {
                    AccountId = accountId, Balance = 2000
                });
                context.SaveChanges();
            }

            using (var context = new BankAppDataContext(options))
            {
                var systemClock = Substitute.For <ISystemClock>();
                systemClock.GetCurrentTime().Returns(new DateTime(2020, 4, 4, 14, 0, 0, DateTimeKind.Utc));
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                int allTransactionsBefore = context.Transactions.CountAsync().Result;

                double   rate = 0.02;
                DateTime latestInterestDate = new DateTime(2018, 02, 02);
                var      currentDate        = DateTime.Now;
                accountCommandHandler.ApplyInterest(accountId, rate, latestInterestDate);

                int allTransactionsAfter = context.Transactions.CountAsync().Result;

                Assert.AreEqual(allTransactionsAfter, allTransactionsBefore + 1);
            }
        }
        public IActionResult PutAccount([FromBody] AccountPutRequest accountPutRequest, [FromRoute] int accountId)
        {
            if (accountPutRequest == null || accountId == null)
            {
                return(BadRequest());
            }

            try
            {
                var command = accountPutRequest.MapToCommand(accountId);

                var commandHandler = new AccountCommandHandler(_accountRepository);

                var account = commandHandler.Handle(command);

                var response = account.MapToResponse();

                return(Ok(response));
            }
            catch (CommandValidationException <eAccountError> ex)
            {
                return(BadRequest(ex.Error.ToString()));
            }
        }
예제 #10
0
 public AccountController()
 {
     accountHandler = new AccountCommandHandler();
 }
 public DispositionCommandHandler(BankAppDataContext context)
 {
     this.context          = context;
     accountCommandHandler = new AccountCommandHandler(context);
 }