/// <summary> /// Check and Insert data for the given currency into database /// </summary> /// <param name="context"></param> /// <param name="currency"></param> /// <param name="request"></param> /// <param name="data"></param> private void UpdateDataForCurrency(ExchangeRateContext context, string currency, DateTime[] dateList, dynamic data) { // Get from time and to time and strip their time parts var dates = dateList.ToList(); // Retrieve the ExchangeRate data from database // Then filter with requested currency and request time var rates = context.ExchangeRates .Where(er => er.Currency == currency && dates.Contains(er.Date)) .Select(er => er.Date) .Distinct(); // Remove days that already has data of requested currency in the database from the date array foreach (var rate in rates) { dates.Remove(rate.Date); } // Get data for days that don't have data in the database foreach (var dateTime in dates) { // Insert data into the context InsertExchangeRate(context, dateTime, currency, data); } }
/// <summary> /// Proceed DataRequest /// </summary> /// <param name="request"></param> private void ProcessRequest(DateTime[] dateList) { // Create DB context using (ExchangeRateContext context = new ExchangeRateContext()) { // Load data from remote service dynamic data = ExchangeRateClient.ReadFromRemoteService(dateList); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead })) { // Insert all tracked currencies data into the context foreach (string currency in _trackedCurrencies) { UpdateDataForCurrency(context, currency, dateList, data); } // Save context context.SaveChanges(); // Complete the Transaction scope.Complete(); } } }
public CurrencyPurchasesController(CurrencyPurchaseContext context, ExchangeRatesController currencyController, ExchangeRateContext exchangeRateContext) { _context = context; _currencyController = currencyController; _exchangeRateContext = exchangeRateContext; }
/// <summary> /// Get Exchange Rate data for Date range [from, to] /// </summary> /// <param name="currency">requested Currency</param> /// <param name="from">start date</param> /// <param name="to">end date</param> /// <returns></returns> public IEnumerable <Models.ExchangeRate> GetExchangeRates(string currency, DateTime from, DateTime to) { // Create ExchangeRate database context using (ExchangeRateContext context = new ExchangeRateContext()) { // Get data of given currency from start date to end date // and order by Date var result = context.ExchangeRates .Where(er => er.Date >= from && er.Date <= to && er.Currency == currency) .OrderBy(er => er.Date) .ToList(); Dictionary <DateTime, dynamic> rates = null; // If there are data returned if (result.Count() > 0) { // Get all the dates between start date and end date DateTime[] dateArray = DateTimeHelper.GetDateTimeArray(from, to); // Calculate the missing dates from the data read from database var existDate = result.Select(er => er.Date); var nonExistDates = dateArray.Where(d => !existDate.Contains(d)); // Queue a request for pulling data of missing to DataBackgroundWorker DataBackgroundWorker.Instance.QueueRequest(nonExistDates.ToArray()); // Get data of missing dates from remoteService rates = ExchangeRateClient.ReadFromRemoteService(nonExistDates); } // If no data returned at all else { // get all data from remote service rates = ExchangeRateClient.ReadFromRemoteService(from, to); // Queue a request for pulling data from Start date to End date to DataBackgroundWorker DataBackgroundWorker.Instance.QueueRequest(DateTimeHelper.GetDateTimeArray(from, to)); } // Parse the result and add them into result collection // Rate of non-exist data is set with -1 // and the Exchange Rate object will be eliminated from the final result result.AddRange(rates.Select(kp => { return(new ExchangeRate { Date = kp.Key, Currency = currency, Rate = kp.Value != null ? kp.Value["rates"][currency] : -1 }); }).Where(ar => ar.Rate != -1)); // Sort by Date and return the result return(result.OrderBy(er => er.Date)); } }
/// <summary> /// Enqueue a list of RequestMessage with the given date list /// </summary> /// <param name="dateList"></param> public void EnqueueRequest(DateTime[] dateList) { using (var context = new ExchangeRateContext()) { // Create the RequestMessage RequestMessage message = new RequestMessage { // Serialize the date list DateList = Serialize(dateList) }; // Add it into database context.RequestMessages.Add(message); context.SaveChanges(); } }
/// <summary> /// Insert a single EnchangeRate record to the database context /// </summary> /// <param name="context"></param> /// <param name="date"></param> /// <param name="currency"></param> /// <param name="data"></param> private void InsertExchangeRate(ExchangeRateContext context, DateTime date, string currency, dynamic data) { try { if (data[date] != null && data[date]["rates"].ContainsKey(currency)) { // Create new Exchange Rate var newRate = new ExchangeRate() { Currency = currency, Date = date, Rate = data[date]["rates"][currency] }; // Add the new ExchangeRate into the context context.ExchangeRates.Add(newRate); } } catch (Exception ex) { // Do nothing fail data will be retrieve later } }
/// <summary> /// Dequeue the earliest unproceeded RequestMessage and return its date list /// /// No request found will result to an empty date list /// </summary> /// <param name="uniqueToken"></param> /// <returns></returns> public DateTime[] DequeueRequest(string uniqueToken) { using (var context = new ExchangeRateContext()) { // Execute stored procedure to dequeue the expected RequestMessage var result = context.DequeueMessage(uniqueToken).ToArray(); DateTime[] dateList = null; if (result.Count() == 0) { dateList = new DateTime[0]; } else { // Get the message var message = result.First(); // Deserialize its date list dateList = Deserialize <DateTime[]>(message.DateList); // Fetch all proceeded messages from the context var deleted = context.RequestMessages.Where(m => m.Token != null); // Delete them all foreach (var deletedMessage in deleted) { context.RequestMessages.Remove(deletedMessage); } context.SaveChanges(); } // Return result return(dateList); } }
public CurrencyExchangeTransactionsRepository(ExchangeRateContext context) { _context = context; }
public ExchangeRatesController(ExchangeRateContext context) { _context = context; }
public CacheManager() { _context = new ExchangeRateContext(); }
public RatesRepository(ExchangeRateContext context) { _context = context; }
public ExchangeRateJob(ExchangeRateContext exchangeRateContext, ILogger <ExchangeRateJob> logger) { _logger = logger; _exchangeRateContext = exchangeRateContext; }