Bläddra i källkod

Added "reset" flag to IConfiguration.SaveAll() to limit data duplication

Frank van den Bos 1 år sedan
förälder
incheckning
5a7c31650b

+ 4 - 3
InABox.Core/Configuration/GlobalConfiguration.cs

@@ -100,7 +100,7 @@ namespace InABox.Configuration
             new Client<GlobalSettings>().Save(setting, "", (o, e) => { });
         }
 
-        public override void SaveAll(Dictionary<string, T> objs)
+        public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
         {
             foreach (var (section, obj) in objs)
             {
@@ -108,8 +108,9 @@ namespace InABox.Configuration
             }
 
             var client = new Client<GlobalSettings>();
-            var data = client.Load(
-                new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
+            var data = reset 
+                ? new Dictionary<string, GlobalSettings>()
+                : client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
                     .And(x => x.Key).InList(objs.Keys.ToArray())).ToDictionary(x => x.Key, x => x);
 
             var settings = new List<GlobalSettings>(objs.Count);

+ 2 - 2
InABox.Core/Configuration/IConfiguration.cs

@@ -20,7 +20,7 @@ namespace InABox.Configuration
         /// as if <see cref="Save(T)"/> was called for each element in <paramref name="objs"/>
         /// </remarks>
         /// <param name="objs"></param>
-        void SaveAll(Dictionary<string, T> objs);
+        void SaveAll(Dictionary<string, T> objs, bool reset = false);
 
         void Delete(Action<Exception?>? callback = null);
     }
@@ -41,7 +41,7 @@ namespace InABox.Configuration
         public abstract T Load(bool useCache = true);
 
         public abstract void Save(T obj);
-        public abstract void SaveAll(Dictionary<string, T> objs);
+        public abstract void SaveAll(Dictionary<string, T> objs, bool reset = false);
 
         public abstract void Delete(Action<Exception?>? callback);
     }

+ 4 - 2
InABox.Core/Configuration/LocalConfiguration.cs

@@ -115,10 +115,12 @@ namespace InABox.Configuration
             ConfigurationCache.Add(ConfigurationCacheType.Local, Section, obj);
         }
 
-        public override void SaveAll(Dictionary<string, T> objs)
+        public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
         {
             var filename = EnsureFileName();
-            var config = LoadCurrentConfig(filename);
+            var config = reset 
+                ? new Dictionary<string,T>() 
+                : LoadCurrentConfig(filename);
 
             foreach(var (section, obj) in objs)
             {

+ 4 - 3
InABox.Core/Configuration/UserConfiguration.cs

@@ -101,7 +101,7 @@ namespace InABox.Configuration
             new Client<UserSettings>().Save(setting, "", (o, e) => { });
         }
 
-        public override void SaveAll(Dictionary<string, T> objs)
+        public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
         {
             foreach (var (section, obj) in objs)
             {
@@ -109,8 +109,9 @@ namespace InABox.Configuration
             }
 
             var client = new Client<UserSettings>();
-            var data = client.Load(
-                new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
+            var data = reset 
+                ? new Dictionary<string,UserSettings>() 
+                : client.Load(new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
                     .And(x => x.Key).InList(objs.Keys.ToArray())
                     .And(x => x.UserID).IsEqualTo(ClientFactory.UserID))
                 .ToDictionary(x => x.Key, x => x);