From c8a0f9fa7ac5cfbef68751a68ff2f57c971ec70a Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 26 Nov 2015 12:05:37 -0800 Subject: [PATCH] Fixed: Saving settings changes --- .../Configuration/ConfigServiceFixture.cs | 10 ++++------ .../Configuration/ConfigRepository.cs | 16 +++++++++++++++- src/NzbDrone.Core/Configuration/ConfigService.cs | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs b/src/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs index c472b1324..6ce968612 100644 --- a/src/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs +++ b/src/NzbDrone.Core.Test/Configuration/ConfigServiceFixture.cs @@ -53,7 +53,7 @@ public void get_value_with_out_persist_should_not_store_default_value() private void AssertUpsert(string key, object value) { - Mocker.GetMock().Verify(c => c.Upsert(It.Is(v => v.Key == key.ToLowerInvariant() && v.Value == value.ToString()))); + Mocker.GetMock().Verify(c => c.Upsert(key.ToLowerInvariant(), value.ToString())); } [Test] @@ -66,16 +66,14 @@ public void config_properties_should_write_and_read_using_same_key() var keys = new List(); var values = new List(); - Mocker.GetMock().Setup(c => c.Upsert(It.IsAny())).Callback(config => + Mocker.GetMock().Setup(c => c.Upsert(It.IsAny(), It.IsAny())).Callback((key, value) => { - keys.Add(config.Key); - values.Add(config); + keys.Add(key); + values.Add(new Config { Key = key, Value = value }); }); - Mocker.GetMock().Setup(c => c.All()).Returns(values); - foreach (var propertyInfo in allProperties) { object value = null; diff --git a/src/NzbDrone.Core/Configuration/ConfigRepository.cs b/src/NzbDrone.Core/Configuration/ConfigRepository.cs index 1f35f0b99..c9092c7e5 100644 --- a/src/NzbDrone.Core/Configuration/ConfigRepository.cs +++ b/src/NzbDrone.Core/Configuration/ConfigRepository.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Core.Configuration public interface IConfigRepository : IBasicRepository { Config Get(string key); - + Config Upsert(string key, string value); } public class ConfigRepository : BasicRepository, IConfigRepository @@ -23,5 +23,19 @@ public Config Get(string key) { return Query.Where(c => c.Key == key).SingleOrDefault(); } + + public Config Upsert(string key, string value) + { + var dbValue = Get(key); + + if (dbValue == null) + { + return Insert(new Config {Key = key, Value = value}); + } + + dbValue.Value = value; + + return Update(dbValue); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Configuration/ConfigService.cs b/src/NzbDrone.Core/Configuration/ConfigService.cs index 31b0936cb..476f25422 100644 --- a/src/NzbDrone.Core/Configuration/ConfigService.cs +++ b/src/NzbDrone.Core/Configuration/ConfigService.cs @@ -376,7 +376,7 @@ private void SetValue(string key, string value) key = key.ToLowerInvariant(); _logger.Trace("Writing Setting to database. Key:'{0}' Value:'{1}'", key, value); - _repository.Upsert(new Config {Key = key, Value = value}); + _repository.Upsert(key, value); ClearCache(); }