Exemple #1
0
        static void Main(string[] args)
        {
            DbContextOptionsBuilder dbContextOptionsBuilder = new DbContextOptionsBuilder();
            var connection = dbContextOptionsBuilder
                             .UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=SpaceWeb;Trusted_Connection=True");

            SpaceDbContext spaceDbContext = new SpaceDbContext(connection.Options);
            ExchangeRateToUsdCurrentRepository exchangeRateToUsdCurrentRepository =
                new ExchangeRateToUsdCurrentRepository(spaceDbContext);
            ExchangeAccountHistoryRepository exchangeAccountHistoryRepository =
                new ExchangeAccountHistoryRepository(spaceDbContext);
            ExchangeRateToUsdHistoryRepository exchangeRateToUsdHistoryRepository =
                new ExchangeRateToUsdHistoryRepository(spaceDbContext);

            var configExpression    = new MapperConfigurationExpression();
            var mapperConfiguration = new MapperConfiguration(configExpression);
            var mapper = new Mapper(mapperConfiguration);

            var contextAccessor = new HttpContextAccessor();

            TransactionBankRepository transactionBankRepository = new TransactionBankRepository(spaceDbContext);
            var          bankAccountRepository = new BankAccountRepository(spaceDbContext, mapper, contextAccessor, transactionBankRepository);
            var          userRepository        = new UserRepository(spaceDbContext, bankAccountRepository);
            IUserService userService           = new UserService(userRepository, contextAccessor);



            var currencyService =
                new CurrencyService(userService,
                                    exchangeRateToUsdCurrentRepository,
                                    exchangeAccountHistoryRepository,
                                    exchangeRateToUsdHistoryRepository,
                                    mapper);

            var currentDate = DateTime.Now;

            while (true)
            {
                if ((currentDate.Minute % 3) == 0)
                {
                    currencyService.MoveCurrentExchangesDbToHistoryDb(exchangeRateToUsdCurrentRepository, exchangeRateToUsdHistoryRepository, mapper);
                    currencyService.DeleteCurrentExchRatesFromDb(exchangeRateToUsdCurrentRepository);
                    currencyService.PutCurrentExchangeRatesToDb(
                        exchangeRateToUsdCurrentRepository,
                        currencyService.GetExchangeRates());
                    Console.Write($"Current exchanges update for History DB at {currentDate}");
                    Thread.Sleep(3 * 59 * 1000); //  59 - because updating exchanges takes ~ 1 second and timer gets a displacement.
                }
                currentDate = DateTime.Now;
                currentDate = currentDate.AddSeconds(-currentDate.Second);
            }
        }
        public void MoveCurrentExchangesDbToHistoryDb(ExchangeRateToUsdCurrentRepository _exchangeRateToUsdCurrentRepository,
                                                      ExchangeRateToUsdHistoryRepository _exchangeRateToUsdHistoryRepository, Mapper _mapper)
        {
            var exchCurrentRates = _exchangeRateToUsdCurrentRepository.GetAll();

            foreach (var exchCurrRate in exchCurrentRates)
            {
                //var exchRateHistory = _mapper.Map<ExchangeRateToUsdHistory>(exchCurrRate);
                //exchRateHistory.ExchRateDate = DateTime.Now;
                //_exchangeRateToUsdHistoryRepository.Save(exchRateHistory);

                var exchRateHistory = new ExchangeRateToUsdHistory
                {
                    Currency     = exchCurrRate.Currency,
                    TypeOfExch   = exchCurrRate.TypeOfExch,
                    ExchRate     = exchCurrRate.ExchRate,
                    ExchRateDate = GetDateWithNullSecAndMillisec()
                };
                _exchangeRateToUsdHistoryRepository.Save(exchRateHistory);
            }
        }
 public BankController(TransactionBankRepository transactionBankRepository,
                       QuestionaryRepository questionaryRepository,
                       IMapper mapper,
                       IUserService userService,
                       BanksCardRepository banksCardRepository,
                       ICurrencyService currencyService,
                       ExchangeRateToUsdHistoryRepository exchangeRateToUsdHistoryRepository,
                       IWebHostEnvironment hostEnvironment,
                       ITransactionService transactionService,
                       IBankPresentation bankPresentation)
 {
     _transactionBankRepository = transactionBankRepository;
     _questionaryRepository     = questionaryRepository;
     _mapper = mapper;
     _banksCardRepository = banksCardRepository;
     _userService         = userService;
     _currencyService     = currencyService;
     _exchangeRateToUsdHistoryRepository = exchangeRateToUsdHistoryRepository;
     _hostEnvironment    = hostEnvironment;
     _transactionService = transactionService;
     _bankPresentation   = bankPresentation;
 }