private bool CheckIfUpdate(List <DBCurrency> currencies) { DB_Context context = new DB_Context(); DBCurrency tmp = currencies.First(); DateTime time = DateTime.Now.ToLocalTime(); //NEED TO CHANGE if (tmp.Date.AddHours(3) > DateTime.Now && (IsItTheSameDay(tmp.Date))) { return(true); } context.Dispose(); return(false); }
//get Real time rates public async Task <Currencies> getRTRatesAsync() { DB_Context context = new DB_Context(); Currencies DbRates = await context.CurrenciesByDate.OrderByDescending(t => t.date) .Include("CurrenciesList") //prevent "lazy" loading .FirstOrDefaultAsync() .ConfigureAwait(false); //if the rates in the DB are updated if (DbRates != null && isUpdatedInLastHour(DbRates.date)) { context.Dispose(); return(DbRates); } else { var instance = new CurrencyLayerDotNet.CurrencyLayerApi(); List <Country> Countries = await getCountriesAsync().ConfigureAwait(false); List <Currency> CurrenciesList; var RTRates = await instance.Invoke <CurrencyLayerDotNet.Models.LiveModel>("live").ConfigureAwait(false); //error in gettings data from the web if (RTRates == null) { if (DbRates == null) { throw new Exception("error"); } context.Dispose(); return(DbRates); } //convert UNIX time to normal time DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); dateTime = dateTime.AddSeconds(RTRates.Timestamp).ToLocalTime(); //check if the rt rates are realy updated if (DbRates != null && dateTime == DbRates.date) { context.Dispose(); return(DbRates); } //if the db is empty if (DbRates == null) { CurrenciesList = FirstEntryConverter(Countries, RTRates); } //the db is not empty else { CurrenciesList = Converter(Countries, RTRates, DbRates); //we save one rate per day there for delete the one thats not updated if (checkIfInTheSameDay(DateTime.Now, DbRates.date)) { context.CurrenciesByDate.Remove(DbRates); } } //create the currecy oblect to return Currencies newCurrencies = new Currencies { CurrenciesList = CurrenciesList, date = dateTime }; context.CurrenciesByDate.Add(newCurrencies); await context.SaveChangesAsync().ConfigureAwait(false); context.Dispose(); return(newCurrencies); } }