Browse Source

Added optional parameter to load a specific set of sections from a configuration.

Kenric Nugteren 1 year ago
parent
commit
8c165df470

+ 7 - 2
InABox.Core/Configuration/GlobalConfiguration.cs

@@ -75,11 +75,16 @@ namespace InABox.Configuration
             return result;
         }
 
-        public override Dictionary<string, T> LoadAll()
+        public override Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null)
         {
             var result = new Dictionary<string, T>();
+            var filter = new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name);
+            if(sections != null)
+            {
+                filter.And(x => x.Key).InList(sections.AsArray());
+            }
             var data = new Client<GlobalSettings>().Query(
-                new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name),
+                filter,
                 new Columns<GlobalSettings>(x => x.Key, x => x.Contents),
                 new SortOrder<GlobalSettings>(x => x.Key)
             );

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

@@ -9,7 +9,7 @@ namespace InABox.Configuration
         public string[] Sections();
 
         T Load(bool useCache = true);
-        Dictionary<string, T> LoadAll();
+        Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null);
 
         void Save(T obj);
         /// <summary>
@@ -36,7 +36,7 @@ namespace InABox.Configuration
 
         public abstract string[] Sections();
 
-        public abstract Dictionary<string, T> LoadAll();
+        public abstract Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null);
 
         public abstract T Load(bool useCache = true);
 

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

@@ -96,9 +96,12 @@ namespace InABox.Configuration
             return result;
         }
 
-        public override Dictionary<string, T> LoadAll()
+        public override Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null)
         {
-            return LoadCurrentConfig(EnsureFileName());
+            var config = LoadCurrentConfig(EnsureFileName());
+            return sections != null
+                ? sections.ToDictionary(x => x, x => config.GetValueOrDefault(x))
+                : config;
         }
 
         public override void Save(T obj)

+ 8 - 2
InABox.Core/Configuration/UserConfiguration.cs

@@ -77,11 +77,17 @@ namespace InABox.Configuration
             return result;
         }
 
-        public override Dictionary<string, T> LoadAll()
+        public override Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null)
         {
             var result = new Dictionary<string, T>();
+            var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
+                .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
+            if (sections != null)
+            {
+                filter.And(x => x.Key).InList(sections.AsArray());
+            }
             var data = new Client<UserSettings>().Query(
-                new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
+                filter,
                 new Columns<UserSettings>(x => x.Key, x => x.Contents),
                 new SortOrder<UserSettings>(x => x.Key)
             );