Files
FeedCenter/Application/SettingsStore.cs

53 lines
1.4 KiB
C#

using FeedCenter.Data;
using FeedCenter.Options;
using System;
using System.Linq;
namespace FeedCenter;
public static class SettingsStore
{
public static object OpenDataStore()
{
return !Database.Exists ? null : new FeedCenterEntities();
}
public static string GetSettingValue(object dataStore, string name, Version _)
{
var entities = (FeedCenterEntities) dataStore;
var setting = entities?.Settings.FirstOrDefault(s => s.Name == name);
return setting?.Value;
}
public static void SetSettingValue(object dataStore, string name, Version _, string value)
{
var entities = (FeedCenterEntities) dataStore;
if (entities == null)
return;
// Try to get the setting from the database that matches the name and version
var setting = entities.Settings.FirstOrDefault(s => s.Name == name);
if (setting?.Value == value)
return;
entities.SaveChanges(() =>
{
// If there was no setting we need to create it
if (setting == null)
{
// Create the new setting
setting = new Setting { Name = name };
// Add the setting to the database
entities.Settings.Add(setting);
}
// Set the value into the setting
setting.Value = value;
});
}
}