ConfigProvider now uses PetaPoco

pull/7/merge
kay.one 14 years ago
parent b1130855cf
commit 190ce6b142

@ -1,4 +1,5 @@
using AutoMoq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Providers.Core;
@ -13,52 +14,81 @@ namespace NzbDrone.Core.Test
public class ConfigProviderTest : TestBase
{
[Test]
public void Overwrite_existing_value()
public void Add_new_value_to_database()
{
const string key = "MY_KEY";
const string value = "MY_VALUE";
//Arrange
var config = new Config {Key = key, Value = value};
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
//Act
mocker.Resolve<ConfigProvider>().SetValue(key, value);
//Assert
mocker.Resolve<ConfigProvider>().GetValue(key, "").Should().Be(value);
}
[Test]
public void Get_value_from_database()
{
const string key = "MY_KEY";
const string value = "MY_VALUE";
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
mocker.GetMock<IRepository>()
.Setup(r => r.Single<Config>(key))
.Returns(config);
db.Insert(new Config { Key = key, Value = value });
//Act
mocker.Resolve<ConfigProvider>().SetValue(key, value);
var result = mocker.Resolve<ConfigProvider>().GetValue(key, "");
//Assert
mocker.GetMock<IRepository>().Verify(c => c.Update(config));
mocker.GetMock<IRepository>().Verify(c => c.Add(It.IsAny<Config>()), Times.Never());
result.Should().Be(value);
}
[Test]
public void Add_new_value()
public void Get_value_should_return_default_when_no_value()
{
const string key = "MY_KEY";
const string value = "MY_VALUE";
//Arrange
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
mocker.GetMock<IRepository>()
.Setup(r => r.Single<Config>(It.IsAny<string>()))
.Returns<Config>(null)
.Verifiable();
//Act
mocker.Resolve<ConfigProvider>().SetValue(key, value);
var result = mocker.Resolve<ConfigProvider>().GetValue(key, value);
//Assert
mocker.GetMock<IRepository>().Verify();
mocker.GetMock<IRepository>().Verify(r => r.Update(It.IsAny<Config>()), Times.Never());
mocker.GetMock<IRepository>().Verify(r => r.Add(It.Is<Config>(c => c.Key == key && c.Value == value)),
Times.Once());
result.Should().Be(value);
}
Assert.Pass();
[Test]
public void New_value_should_update_old_value()
{
const string key = "MY_KEY";
const string originalValue = "OLD_VALUE";
const string newValue = "NEW_VALUE";
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
db.Insert(new Config { Key = key, Value = originalValue });
//Act
mocker.Resolve<ConfigProvider>().SetValue(key, newValue);
var result = mocker.Resolve<ConfigProvider>().GetValue(key, "");
//Assert
result.Should().Be(newValue);
db.Fetch<Config>().Should().HaveCount(1);
}
}
}

@ -126,8 +126,6 @@ namespace NzbDrone.Core.Test.Framework
private static void ForceMigration(IRepository repository)
{
repository.All<Series>().Count();
repository.All<Episode>().Count();
repository.All<EpisodeFile>().Count();
repository.All<QualityProfile>().Count();
repository.All<History>().Count();

@ -45,6 +45,12 @@ namespace NzbDrone.Core.Datastore.Migrations
.WithNullableColumn("GrabDate", DbType.DateTime);
db.CreateTable("Config")
.WithNotNullableColumn("Key", DbType.String).Unique()
.WithNotNullableColumn("Value", DbType.String);
}
}

@ -5,6 +5,7 @@ using Ninject;
using NLog;
using NzbDrone.Core.Model;
using NzbDrone.Core.Repository;
using PetaPoco;
using SubSonic.Repository;
namespace NzbDrone.Core.Providers.Core
@ -12,17 +13,18 @@ namespace NzbDrone.Core.Providers.Core
public class ConfigProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IRepository _repository;
private readonly IDatabase _database;
[Inject]
public ConfigProvider(IRepository repository)
public ConfigProvider(IDatabase database)
{
_repository = repository;
_database = database;
}
public IList<Config> All()
{
return _repository.All<Config>().ToList();
return _database.Fetch<Config>();
}
public ConfigProvider()
@ -129,7 +131,7 @@ namespace NzbDrone.Core.Providers.Core
public virtual String SabHost
{
get { return GetValue("SabHost", "localhost", true); }
get { return GetValue("SabHost", "localhost"); }
set { SetValue("SabHost", value); }
}
@ -164,7 +166,7 @@ namespace NzbDrone.Core.Providers.Core
public virtual String SabTvCategory
{
get { return GetValue("SabTvCategory", "TV", false); }
get { return GetValue("SabTvCategory", "TV"); }
set { SetValue("SabTvCategory", value); }
}
@ -178,7 +180,7 @@ namespace NzbDrone.Core.Providers.Core
public virtual String SabDropDirectory
{
get { return GetValue("SabTvDropDirectory", "", false); }
get { return GetValue("SabTvDropDirectory"); }
set { SetValue("SabTvDropDirectory", value); }
}
@ -230,7 +232,7 @@ namespace NzbDrone.Core.Providers.Core
public virtual string SeasonFolderFormat
{
get { return GetValue("Sorting_SeasonFolderFormat", "Season %s", false); }
get { return GetValue("Sorting_SeasonFolderFormat", "Season %s"); }
set { SetValue("Sorting_SeasonFolderFormat", value); }
}
@ -261,31 +263,29 @@ namespace NzbDrone.Core.Providers.Core
private string GetValue(string key)
{
return GetValue(key, String.Empty, false);
return GetValue(key, String.Empty);
}
private bool GetValueBoolean(string key, bool defaultValue = false)
{
return Convert.ToBoolean(GetValue(key, defaultValue, false));
return Convert.ToBoolean(GetValue(key, defaultValue));
}
private int GetValueInt(string key, int defaultValue = 0)
{
return Convert.ToInt16(GetValue(key, defaultValue, false));
return Convert.ToInt16(GetValue(key, defaultValue));
}
public virtual string GetValue(string key, object defaultValue, bool makePermanent)
public virtual string GetValue(string key, object defaultValue)
{
string value;
var dbValue = _repository.Single<Config>(key);
var dbValue = _database.SingleOrDefault<Config>(key);
if (dbValue != null && !String.IsNullOrEmpty(dbValue.Value))
return dbValue.Value;
Logger.Debug("Unable to find config key '{0}' defaultValue:'{1}'", key, defaultValue);
if (makePermanent)
SetValue(key, defaultValue.ToString());
value = defaultValue.ToString();
return value;
@ -310,20 +310,16 @@ namespace NzbDrone.Core.Providers.Core
Logger.Debug("Writing Setting to file. Key:'{0}' Value:'{1}'", key, value);
var dbValue = _repository.Single<Config>(key);
var dbValue = _database.SingleOrDefault<Config>("WHERE KEY=@0", key);
if (dbValue == null)
{
_repository.Add(new Config
{
Key = key,
Value = value
});
_database.Insert(new Config { Key = key, Value = value });
}
else
{
dbValue.Value = value;
_repository.Update(dbValue);
_database.Update(dbValue);
}
}
}

@ -23,9 +23,9 @@ namespace NzbDrone.Core.Providers.ExternalNotification
{
const string header = "NzbDrone [TV] - Grabbed";
if (Convert.ToBoolean(_configProvider.GetValue("XbmcEnabled", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcEnabled", false)))
{
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnGrab", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnGrab", false)))
{
_logger.Trace("Sending Notification to XBMC");
_xbmcProvider.Notify(header, message);
@ -41,21 +41,21 @@ namespace NzbDrone.Core.Providers.ExternalNotification
{
const string header = "NzbDrone [TV] - Downloaded";
if (Convert.ToBoolean(_configProvider.GetValue("XbmcEnabled", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcEnabled", false)))
{
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnDownload", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnDownload", false)))
{
_logger.Trace("Sending Notification to XBMC");
_xbmcProvider.Notify(header, message);
}
if (Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnDownload", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnDownload", false)))
{
_logger.Trace("Sending Update Request to XBMC");
_xbmcProvider.Update(seriesId);
}
if (Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnDownload", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnDownload", false)))
{
_logger.Trace("Sending Clean DB Request to XBMC");
_xbmcProvider.Clean();
@ -69,19 +69,19 @@ namespace NzbDrone.Core.Providers.ExternalNotification
{
const string header = "NzbDrone [TV] - Renamed";
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnRename", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnRename", false)))
{
_logger.Trace("Sending Notification to XBMC");
_xbmcProvider.Notify(header, message);
}
if (Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnRename", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnRename", false)))
{
_logger.Trace("Sending Update Request to XBMC");
_xbmcProvider.Update(seriesId);
}
if (Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnRename", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnRename", false)))
{
_logger.Trace("Sending Clean DB Request to XBMC");
_xbmcProvider.Clean();

@ -8,9 +8,7 @@ using NLog;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using PetaPoco;
using SubSonic.Repository;
using TvdbLib.Data;
namespace NzbDrone.Core.Providers
@ -18,17 +16,15 @@ namespace NzbDrone.Core.Providers
public class SeriesProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IRepository _repository;
private readonly ConfigProvider _configProvider;
private readonly TvDbProvider _tvDbProvider;
private readonly IDatabase _database;
private readonly QualityProvider _qualityProvider;
[Inject]
public SeriesProvider(ConfigProvider configProviderProvider, IRepository repository, TvDbProvider tvDbProviderProvider, IDatabase database, QualityProvider qualityProvider)
public SeriesProvider(ConfigProvider configProviderProvider, TvDbProvider tvDbProviderProvider, IDatabase database, QualityProvider qualityProvider)
{
_configProvider = configProviderProvider;
_repository = repository;
_tvDbProvider = tvDbProviderProvider;
_database = database;
_qualityProvider = qualityProvider;
@ -100,7 +96,7 @@ namespace NzbDrone.Core.Providers
repoSeries.Monitored = true; //New shows should be monitored
repoSeries.QualityProfileId = qualityProfileId;
if (qualityProfileId == 0)
repoSeries.QualityProfileId = Convert.ToInt32(_configProvider.GetValue("DefaultQualityProfile", "1", true));
repoSeries.QualityProfileId = Convert.ToInt32(_configProvider.GetValue("DefaultQualityProfile", "1"));
repoSeries.SeasonFolder = _configProvider.UseSeasonFolder;
@ -127,30 +123,29 @@ namespace NzbDrone.Core.Providers
public virtual void DeleteSeries(int seriesId)
{
Logger.Warn("Deleting Series [{0}]", seriesId);
var series = _repository.Single<Series>(seriesId);
var series = GetSeries(seriesId);
Logger.Warn("Deleting Series [{0}]", series.Title);
using (var tran = _database.GetTransaction())
{
//Delete History, Files, Episodes, Seasons then the Series
//Delete Files, Episodes, Seasons then the Series
//Can't use providers because episode provider needs series provider - Cyclic Dependency Injection, this will work
Logger.Debug("Deleting History Items from DB for Series: {0}", series.Title);
_database.Delete<History>("WHERE SeriesId=@0", seriesId);
//Delete History Items for any episodes that belong to this series
Logger.Debug("Deleting History Items from DB for Series: {0}", series.SeriesId);
var episodes = series.Episodes.Select(e => e.EpisodeId).ToList();
episodes.ForEach(e => _repository.DeleteMany<History>(h => h.EpisodeId == e));
Logger.Debug("Deleting EpisodeFiles from DB for Series: {0}", series.Title);
_database.Delete<EpisodeFile>("WHERE SeriesId=@0", seriesId);
//Delete all episode files from the DB for episodes in this series
Logger.Debug("Deleting EpisodeFiles from DB for Series: {0}", series.SeriesId);
_repository.DeleteMany(series.EpisodeFiles);
Logger.Debug("Deleting Episodes from DB for Series: {0}", series.Title);
_database.Delete<Episode>("WHERE SeriesId=@0", seriesId);
//Delete all episodes for this series from the DB
Logger.Debug("Deleting Episodes from DB for Series: {0}", series.SeriesId);
_repository.DeleteMany(series.Episodes);
Logger.Debug("Deleting Series from DB {0}", series.Title);
_database.Delete<Series>("WHERE SeriesId=@0", seriesId);
//Delete the Series
Logger.Debug("Deleting Series from DB {0}", series.Title);
_repository.Delete<Series>(seriesId);
Logger.Info("Successfully deleted Series [{0}]", series.Title);
Logger.Info("Successfully deleted Series [{0}]", seriesId);
tran.Complete();
}
}
public virtual bool SeriesPathExists(string cleanPath)

@ -25,10 +25,10 @@ namespace NzbDrone.Core.Providers
public virtual void Notify(string header, string message)
{
//Get time in seconds and convert to ms
var time = Convert.ToInt32(_configProvider.GetValue("XbmcDisplayTime", "3", true)) * 1000;
var time = Convert.ToInt32(_configProvider.GetValue("XbmcDisplayTime", "3")) * 1000;
var command = String.Format("ExecBuiltIn(Notification({0},{1},{2}))", header, message, time);
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotificationImage", false, true)))
if (Convert.ToBoolean(_configProvider.GetValue("XbmcNotificationImage", false)))
{
//Todo: Get the actual port that NzbDrone is running on...
var serverInfo = String.Format("http://{0}:{1}", Environment.MachineName, "8989");
@ -37,7 +37,7 @@ namespace NzbDrone.Core.Providers
command = String.Format("ExecBuiltIn(Notification({0},{1},{2}, {3}))", header, message, time, imageUrl);
}
foreach (var host in _configProvider.GetValue("XbmcHosts", "localhost:80", true).Split(','))
foreach (var host in _configProvider.GetValue("XbmcHosts", "localhost:80").Split(','))
{
Logger.Trace("Sending Notifcation to XBMC Host: {0}", host);
SendCommand(host, command);
@ -46,14 +46,14 @@ namespace NzbDrone.Core.Providers
public virtual void Update(int seriesId)
{
foreach (var host in _configProvider.GetValue("XbmcHosts", "localhost:80", true).Split(','))
foreach (var host in _configProvider.GetValue("XbmcHosts", "localhost:80").Split(','))
{
Logger.Trace("Sending Update DB Request to XBMC Host: {0}", host);
var xbmcSeriesPath = GetXbmcSeriesPath(host, seriesId);
//If the path is not found & the user wants to update the entire library, do it now.
if (String.IsNullOrEmpty(xbmcSeriesPath) &&
Convert.ToBoolean(_configProvider.GetValue("XbmcFullUpdate", false, true)))
Convert.ToBoolean(_configProvider.GetValue("XbmcFullUpdate", false)))
{
//Update the entire library
Logger.Trace("Series [{0}] doesn't exist on XBMC host: {1}, Updating Entire Library", seriesId, host);
@ -68,7 +68,7 @@ namespace NzbDrone.Core.Providers
public virtual void Clean()
{
foreach (var host in _configProvider.GetValue("XbmcHosts", "localhost:80", true).Split(','))
foreach (var host in _configProvider.GetValue("XbmcHosts", "localhost:80").Split(','))
{
Logger.Trace("Sending DB Clean Request to XBMC Host: {0}", host);
var command = String.Format("ExecBuiltIn(CleanLibrary(video))");
@ -78,8 +78,8 @@ namespace NzbDrone.Core.Providers
private string SendCommand(string host, string command)
{
var username = _configProvider.GetValue("XbmcUsername", String.Empty, true);
var password = _configProvider.GetValue("XbmcPassword", String.Empty, true);
var username = _configProvider.GetValue("XbmcUsername", String.Empty);
var password = _configProvider.GetValue("XbmcPassword", String.Empty);
var url = String.Format("http://{0}/xbmcCmds/xbmcHttp?command={1}", host, command);
if (!String.IsNullOrEmpty(username))

@ -1,11 +1,11 @@
using SubSonic.SqlGeneration.Schema;
using PetaPoco;
using SubSonic.SqlGeneration.Schema;
namespace NzbDrone.Core.Repository
{
[SubSonicTableNameOverride("Config")]
[PrimaryKey("Key", autoIncrement = false)]
public class Config
{
[SubSonicPrimaryKey]
public string Key { get; set; }
public string Value { get; set; }

@ -7,6 +7,7 @@ using SubSonic.SqlGeneration.Schema;
namespace NzbDrone.Core.Repository
{
[PrimaryKey("SeriesId", autoIncrement = false)]
public class Series
{
[SubSonicPrimaryKey(false)]

@ -148,20 +148,20 @@ namespace NzbDrone.Web.Controllers
var model = new NotificationSettingsModel
{
XbmcEnabled = Convert.ToBoolean(_configProvider.GetValue("XbmcEnabled", false, true)),
XbmcNotifyOnGrab = Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnGrab", false, true)),
XbmcNotifyOnDownload = Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnDownload", false, true)),
XbmcNotifyOnRename = Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnRename", false, true)),
XbmcNotificationImage = Convert.ToBoolean(_configProvider.GetValue("XbmcNotificationImage", false, true)),
XbmcDisplayTime = Convert.ToInt32(_configProvider.GetValue("XbmcDisplayTime", 3, true)),
XbmcUpdateOnDownload = Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnDownload ", false, true)),
XbmcUpdateOnRename = Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnRename", false, true)),
XbmcFullUpdate = Convert.ToBoolean(_configProvider.GetValue("XbmcFullUpdate", false, true)),
XbmcCleanOnDownload = Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnDownload", false, true)),
XbmcCleanOnRename = Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnRename", false, true)),
XbmcHosts = _configProvider.GetValue("XbmcHosts", "localhost:80", true),
XbmcUsername = _configProvider.GetValue("XbmcUsername", String.Empty, true),
XbmcPassword = _configProvider.GetValue("XbmcPassword", String.Empty, true)
XbmcEnabled = Convert.ToBoolean(_configProvider.GetValue("XbmcEnabled", false)),
XbmcNotifyOnGrab = Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnGrab", false)),
XbmcNotifyOnDownload = Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnDownload", false)),
XbmcNotifyOnRename = Convert.ToBoolean(_configProvider.GetValue("XbmcNotifyOnRename", false)),
XbmcNotificationImage = Convert.ToBoolean(_configProvider.GetValue("XbmcNotificationImage", false)),
XbmcDisplayTime = Convert.ToInt32(_configProvider.GetValue("XbmcDisplayTime", 3)),
XbmcUpdateOnDownload = Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnDownload ", false)),
XbmcUpdateOnRename = Convert.ToBoolean(_configProvider.GetValue("XbmcUpdateOnRename", false)),
XbmcFullUpdate = Convert.ToBoolean(_configProvider.GetValue("XbmcFullUpdate", false)),
XbmcCleanOnDownload = Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnDownload", false)),
XbmcCleanOnRename = Convert.ToBoolean(_configProvider.GetValue("XbmcCleanOnRename", false)),
XbmcHosts = _configProvider.GetValue("XbmcHosts", "localhost:80"),
XbmcUsername = _configProvider.GetValue("XbmcUsername", String.Empty),
XbmcPassword = _configProvider.GetValue("XbmcPassword", String.Empty)
};
return View("Index", model);
@ -240,7 +240,7 @@ namespace NzbDrone.Web.Controllers
{
var profiles = _qualityProvider.GetAllProfiles().ToList();
var defaultQualityQualityProfileId =
Convert.ToInt32(_configProvider.GetValue("DefaultQualityProfile", profiles[0].QualityProfileId, true));
Convert.ToInt32(_configProvider.GetValue("DefaultQualityProfile", profiles[0].QualityProfileId));
var selectList = new SelectList(profiles, "QualityProfileId", "Name");
return new QualityModel { DefaultQualityProfileId = defaultQualityQualityProfileId, QualityProfileSelectList = selectList };

Loading…
Cancel
Save