public void SaveSettings(SettingsViewModel settings) { Settings formSettings = new Settings { AlertsEnabled = settings.AlertsEnabled, ContactIntervalMinutes = settings.ContactInterval, RefreshIntervalMinutes = settings.RefreshInterval }; List<Email> emails = new List<Email>(); string[] emailList = settings.Emails.Split(','); foreach (var email in emailList) { email.Trim(); emails.Add(new Email { EmailAddress = email.Trim() }); } List<Phone> phones = new List<Phone>(); string[] phoneList = settings.Phones.Split(','); foreach (var phone in phoneList) { phones.Add(new Phone { PhoneNumber = phone.Trim() }); } try { _settingsRepo.SaveSettings(formSettings, emails, phones, settings.Deltas); } catch (Exception err) { return; } }
public void SaveSettings(Settings formSettings, List<Email> emails, List<Phone> phones, Dictionary<string, int> deltas) { var dbFactory = new OrmLiteConnectionFactory( _connectionString, SqlServerOrmLiteDialectProvider.Instance); using (IDbConnection dbConnection = dbFactory.OpenDbConnection()) { // Update settings table Settings settings = dbConnection.Single<Settings>("SELECT TOP 1 * FROM settings"); dbConnection.Delete(settings); settings.ContactIntervalMinutes = formSettings.ContactIntervalMinutes; settings.AlertsEnabled = formSettings.AlertsEnabled; settings.RefreshIntervalMinutes = formSettings.RefreshIntervalMinutes; dbConnection.Save(settings); // Delete removed phone numbers, add new ones List<Phone> existingPhones = dbConnection.Select<Phone>("SELECT PhoneNumber FROM phone"); List<Phone> newPhones = phones.Except(existingPhones).ToList(); List<Phone> oldPhones = existingPhones.Except(phones).ToList(); if (oldPhones.Any()) { foreach (var phone in oldPhones) { dbConnection.Delete(phone); } } if (newPhones.Any()) dbConnection.InsertAll(newPhones); // Delete removed emails, add new ones List<Email> existingEmails = dbConnection.Select<Email>("SELECT EmailAddress FROM email"); List<Email> newEmails = emails.Except(existingEmails).ToList(); List<Email> oldEmails = existingEmails.Except(emails).ToList(); if (oldEmails.Any()) { foreach (var email in oldEmails) { dbConnection.Delete(email); } } if (newEmails.Any()) dbConnection.InsertAll(newEmails); // Update deltas List<Source> existingSources = dbConnection.Select<Source>("SELECT * FROM Source"); foreach (var source in existingSources) { source.Delta = deltas[source.Name]; } dbConnection.SaveAll(existingSources); dbConnection.Close(); } }