//Insert record public static String putIntoDB(string countryAddr) { //Assumption: Only data form PL if (!countryAddr.Equals(CountryJsonLink.addrPL)) { Logger.log.Error("DB can only contain data from Poland :("); return("DB can only contain data from Poland :("); } JsonObject countryPL = GetJSON.GetData(countryAddr); //Create new data object var todayPoland = new HistoricalData { Date = DateTime.Now, Name = countryPL.countrydata[0].info.title, Total_active_cases = countryPL.countrydata[0].total_active_cases - countryPL.countrydata[0].total_new_cases_today, //Take from yesterday Total_new_cases_today = countryPL.countrydata[0].total_new_cases_today }; //Perform on DB using (var db = new HistoricalDataDBContext()) { var record = db.Datas.OrderByDescending(n => n.Id).FirstOrDefault(); //If Table == empty add first if (record == null) { db.Datas.Add(todayPoland); db.SaveChanges(); return(""); } //Insert only if data from the following day does not exist if (!record.Date.Date.Equals(DateTime.Now.Date)) { db.Datas.Add(todayPoland); db.SaveChanges(); } } return(""); }
public static DataBaseProcessing.HistoricalDataDBContext getData() { var db = new HistoricalDataDBContext(); return(db); }