public async Task <int> SyncDatabaseAsync(Subplatform subplatform) { InitNonExistingRepo(); // Validation if (subplatform.Settings.FirstOrDefault(ss => ss.SettingName == Setting.Platform.DAYS_TO_KEEP_RECORDS) is null) { throw new Exception("Subplatform has no set period to keep records!"); } if (subplatform.Settings.FirstOrDefault(ss => ss.SettingName == Setting.Platform.SOURCE_API_URL) is null) { throw new Exception("Subplatform has no set API url!"); } // Injects api seed data APICalls restClient = new APICalls { API_URL = subplatform.Settings.FirstOrDefault(ss => ss.SettingName == Setting.Platform.SOURCE_API_URL) .Value }; // Add themes and keywords to dictionary Dictionary <string, string[]> ThemesAndKeywords = new Dictionary <string, string[]>(); ItemRepo.ReadThemes().Where(k => k.SubPlatforms.Contains(subplatform)).ToList().ForEach(t => { ThemesAndKeywords.Add(t.Name, t.Keywords.Select(k => k.Name).ToArray()); }); if (ThemesAndKeywords.Count > 0) { // Call API with request List <JClass> requestedRecords = new List <JClass>(); try { requestedRecords.AddRange(restClient.RequestRecords( since: DateTime.Now.AddDays(-int.Parse(subplatform.Settings .First(s => s.SettingName.Equals(Setting.Platform.DAYS_TO_KEEP_RECORDS)).Value)), themes: ThemesAndKeywords)); } catch (Exception e) { Console.WriteLine(e.GetType().Name + ": " + e.Message); if (e.InnerException != null) { Console.WriteLine("Inner Exception: " + e.InnerException); } throw e; } // Link items to subplatform requestedRecords.ForEach(r => r.Subplatforms.Add(subplatform)); //Convert JClass to Record and persist to database List <Record> newRecords = JClassToRecord(requestedRecords); // Persist items tgo database RecordRepo.CreateRecords(newRecords); } // Save pending changes return(await UowManager.SaveAsync()); }