public bool storeWorker(Worker worker) { DB_Context context = new DB_Context(); context.workers.Add(worker); return(context.SaveChanges() != 0); }
public async Task <List <DBCurrency> > loadCurrenciesAsync() { try { DB_Context context = new DB_Context(); List <DBCurrency> Currencies = new List <DBCurrency>(); //ToListAsync-convert from the DbSet<Currency> to List<Currency> Currencies = await context.currencies.ToListAsync(); //check if it's not empty otherwise we need to charge the list from the webSite using the Api. if (Currencies.Any() && CheckIfUpdate(Currencies)) { return(Currencies); } else { //using the instance we get the access to the webSite by the api. var instance = new CurrencyLayerDotNet.CurrencyLayerApi(); //await - says that i will we give the cpu to anyone who wants and i will wait. //here we invoke the function to get the list of initilais with their full name and resotore them using the Models. //Models.CurrencyListModel - have "terms" "privacy" "currencies". var CurrenciesList = await instance.Invoke <CurrencyLayerDotNet.Models.CurrencyListModel>("list").ConfigureAwait(false); var CurrenciesValues = await instance.Invoke <CurrencyLayerDotNet.Models.LiveModel>("live").ConfigureAwait(false); if (CurrenciesList.Success == true) { Currencies = CurrenciesList.quotes.Select(t => new DBCurrency() { Initials = t.Key, FullName = t.Value }).ToList(); // "BND": "Brunei Dollar", Currencies = UpdateValueToCurrenciesByNames(Currencies, CurrenciesValues); //this used to remove all the currencies form the data base and to insert the new currencies. foreach (var t in context.currencies) { context.currencies.Remove(t); } context.currencies.AddRange(Currencies); await context.SaveChangesAsync(); return(Currencies); } else { return(null); } } } catch (Exception e) { throw; } }
//history information save for last X weeks , for all kinds of coins. public async void pushCurrenciesHistory() { DB_Context context = new DB_Context(); List <History> Currencies = new List <History>(); //if (context.historicalCurrencies == null) // return; Currencies = await context.historicalCurrencies.ToListAsync(); Dictionary <string, string> _date = new Dictionary <string, string>(); DateTime dt = DateTime.Now; //DateTime.ParseExact(DateTime.Now.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture); if (Currencies.Any()) //The table isn't empty. { return; //Don't need to download again the hostori... } var instance = new CurrencyLayerDotNet.CurrencyLayerApi(); //The url request for the history. var init_fullName = await instance.Invoke <CurrencyLayerDotNet.Models.CurrencyListModel>("list").ConfigureAwait(false); Dictionary <string, string> converter = init_fullName.quotes; int count = 0; for (int i = 0; i < 10; i++) { _date.Add("date", dt.ToString("yyyy-MM-dd"));// "YYYY-MM-DD")); var CurrenciesList = await instance.Invoke <CurrencyLayerDotNet.Models.HistoryModel>("historical", _date).ConfigureAwait(false); _date.Clear(); DateTime shareDate = dt; dt.AddDays(-7); //check the values for all the weeks. Dictionary <string, string> items = CurrenciesList.quotes; foreach (KeyValuePair <string, string> entry in items) { // do something with entry.Value or entry.Key double value = Double.Parse(entry.Value); string inital = entry.Key.Substring(3); string fullName = converter[inital]; string flag = "UI/Images/" + entry.Key.Substring(3) + ".png"; string date = dt.Ticks.ToString(); Currencies.Add(new History() { Id = count++, Initials = inital, Date = date, Flag = flag, Value = value, FullName = fullName }); } context.historicalCurrencies.AddRange(Currencies); await context.SaveChangesAsync(); } }
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 supported countries countries public async Task <List <Country> > getCountriesAsync() { List <Country> Countries; using (DB_Context context = new DB_Context()) { Countries = await context.Countries.ToListAsync(); } //check if the countries exists in the local DB if (Countries.Any()) { return(Countries); } else //download and save countries { var instance = new CurrencyLayerDotNet.CurrencyLayerApi(); var CountriesList = await instance.Invoke <CurrencyLayerDotNet.Models.CurrencyListModel>("list"); if (CountriesList.Success == true) { Countries = CountriesList.quotes.Select(t => new Country() { Code = t.Key, Name = t.Value }).ToList(); using (DB_Context context = new DB_Context()) { context.Countries.AddRange(Countries); await context.SaveChangesAsync(); } return(Countries); } else { return(null); } } }
public async Task <Dictionary <DateTime, double> > loadCurrenciesHistory(string initial) { try { pushCurrenciesHistory(); DB_Context context = new DB_Context(); List <History> Currencies = new List <History>(); Currencies = await context.historicalCurrencies.ToListAsync(); Dictionary <DateTime, double> d = new Dictionary <DateTime, double>(); foreach (History element in Currencies) { if (element.Initials.Equals(initial)) { d.Add(new DateTime(long.Parse(element.Date)), element.Value); } } return(d); } catch (Exception ex) { throw; } }
public List <Worker> loadWorkers() { DB_Context context = new DB_Context(); return(context.workers.ToList()); }
//get historical rates public async Task <List <HistoryDTO> > getHRatesAsync(string code) { List <HistoryDTO> historyRates; Currencies lastHistory; Currencies firstHistory; DateTime lastHistoryDate; DateTime firstHistoryDate; using (DB_Context context = new DB_Context()) { //fill the gap from now to the first date we have firstHistory = await context.CurrenciesByDate.OrderByDescending(t => t.date).FirstOrDefaultAsync(); if (firstHistory != null && !checkIfInTheSameDay(DateTime.Now, firstHistory.date)) { firstHistoryDate = firstHistory.date; await getRTRatesAsync(); var instance = new CurrencyLayerDotNet.CurrencyLayerApi(); List <Country> Countries = await getCountriesAsync().ConfigureAwait(false); DateTime end = DateTime.Now.AddDays(-1); for (DateTime start = firstHistoryDate; !checkIfInTheSameDay(start, end); start = start.AddDays(1)) { var HRate = await instance.Invoke <HistoryModel>("historical", new Dictionary <string, string> { { "date", start.ToString("yyyy-MM-dd") } }).ConfigureAwait(false); Currencies currencies = new Currencies { date = start, CurrenciesList = ConverterHRate(Countries, HRate, firstHistory) }; firstHistory = currencies; context.CurrenciesByDate.Add(currencies); } //save changes await context.SaveChangesAsync(); } //fill the gap from the last date we have to a year //gets the last date from DB lastHistory = await context.CurrenciesByDate.OrderBy(t => t.date).FirstOrDefaultAsync(); //if no currecies in the DB so make the first on to be the real time one if (lastHistory == null) { //lastHistoryDate = DateTime.Now.AddDays(-1); lastHistory = await getRTRatesAsync().ConfigureAwait(false); } lastHistoryDate = lastHistory.date.AddDays(-1); //fil the gap from the last date we have to a year if (lastHistoryDate.AddYears(1) > DateTime.Now.AddDays(1)) { var instance = new CurrencyLayerDotNet.CurrencyLayerApi(); List <Country> Countries = await getCountriesAsync().ConfigureAwait(false); // DateTime end = DateTime.Now.AddDays(-1); for (DateTime start = lastHistoryDate; !checkIfInTheSameDay(start.AddYears(1), lastHistoryDate); start = start.AddDays(-1)) { // string d = start.ToString("yyyy-MM-dd"); var HRate = await instance.Invoke <CurrencyLayerDotNet.Models.HistoryModel>("historical", new Dictionary <string, string> { { "date", start.ToString("yyyy-MM-dd") } }).ConfigureAwait(false); Currencies currencies = new Currencies { date = start, CurrenciesList = ConverterHRate(Countries, HRate, lastHistory) }; lastHistory = currencies; context.CurrenciesByDate.Add(currencies); } //save changes await context.SaveChangesAsync(); } //get the historical dates historyRates = await context.CurrenciesByDate.OrderBy(t => t.date).Select(t => new HistoryDTO() { Currency = t.CurrenciesList.FirstOrDefault(x => x.IssuedCountryCode == code), date = t.date }).ToListAsync(); } return(historyRates); }
//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); } }