|
@@ -1,7 +1,6 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
-using InABox.Clients;
|
|
|
using InABox.Core;
|
|
|
|
|
|
namespace InABox.Configuration
|
|
@@ -33,36 +32,39 @@ namespace InABox.Configuration
|
|
|
|
|
|
public class UserConfiguration<T> : Configuration<T> where T : IUserConfigurationSettings, new()
|
|
|
{
|
|
|
- public UserConfiguration(string section = "") : base(section)
|
|
|
+ private IConfigurationProvider<UserSettings> Provider;
|
|
|
+
|
|
|
+ public UserConfiguration(string section = "") : this(section, ClientConfigurationProvider<UserSettings>.Provider)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserConfiguration(string section, IConfigurationProvider<UserSettings> provider) : base(section)
|
|
|
{
|
|
|
+ Provider = provider;
|
|
|
}
|
|
|
|
|
|
private UserSettings NewSettings(string? section = null)
|
|
|
{
|
|
|
- return new UserSettings { Section = typeof(T).Name, Key = section ?? Section, UserID = ClientFactory.UserID };
|
|
|
+ return new UserSettings { Section = typeof(T).Name, Key = section ?? Section, UserID = Provider.UserID };
|
|
|
}
|
|
|
|
|
|
private UserSettings GetSettings()
|
|
|
{
|
|
|
UserSettings result = null;
|
|
|
- if (ClientFactory.ClientType != null)
|
|
|
- {
|
|
|
- var client = new Client<UserSettings>();
|
|
|
+ var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
|
|
|
+ .And(x => x.Key).IsEqualTo(Section)
|
|
|
+ .And(x => x.UserID).IsEqualTo(Provider.UserID);
|
|
|
|
|
|
- var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
|
|
|
- .And(x => x.Key).IsEqualTo(Section)
|
|
|
- .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
|
|
|
|
|
|
+ result = Provider.Query(filter, Columns.All<UserSettings>())
|
|
|
+ .ToObjects<UserSettings>().FirstOrDefault();
|
|
|
|
|
|
- result = client.Query(filter, Columns.All<UserSettings>())
|
|
|
- .ToObjects<UserSettings>().FirstOrDefault();
|
|
|
- }
|
|
|
return result ?? NewSettings();
|
|
|
}
|
|
|
|
|
|
private string GetKey(string? section = null)
|
|
|
{
|
|
|
- return string.Format("{0}.{1}", section ?? Section, ClientFactory.UserID);
|
|
|
+ return string.Format("{0}.{1}", section ?? Section, Provider.UserID);
|
|
|
}
|
|
|
|
|
|
public override T Load(bool useCache = true)
|
|
@@ -97,12 +99,12 @@ namespace InABox.Configuration
|
|
|
{
|
|
|
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);
|
|
|
+ .And(x => x.UserID).IsEqualTo(Provider.UserID);
|
|
|
if (sections != null)
|
|
|
{
|
|
|
filter.And(x => x.Key).InList(sections.AsArray());
|
|
|
}
|
|
|
- var data = new Client<UserSettings>().Query(
|
|
|
+ var data = Provider.Query(
|
|
|
filter,
|
|
|
Columns.None<UserSettings>().Add(x => x.Key, x => x.Contents),
|
|
|
new SortOrder<UserSettings>(x => x.Key)
|
|
@@ -127,7 +129,7 @@ namespace InABox.Configuration
|
|
|
|
|
|
setting.Contents = Serialization.Serialize(obj);
|
|
|
|
|
|
- new Client<UserSettings>().Save(setting, "", (o, e) => { });
|
|
|
+ Provider.Save(setting);
|
|
|
}
|
|
|
|
|
|
public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
|
|
@@ -137,13 +139,12 @@ namespace InABox.Configuration
|
|
|
ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(section), obj);
|
|
|
}
|
|
|
|
|
|
- var client = new Client<UserSettings>();
|
|
|
var data = reset
|
|
|
? new Dictionary<string,UserSettings>()
|
|
|
- : client.Query(
|
|
|
+ : Provider.Query(
|
|
|
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),
|
|
|
+ .And(x => x.UserID).IsEqualTo(Provider.UserID),
|
|
|
Columns.All<UserSettings>())
|
|
|
.ToObjects<UserSettings>()
|
|
|
.ToDictionary(x => x.Key, x => x);
|
|
@@ -159,47 +160,38 @@ namespace InABox.Configuration
|
|
|
setting.Contents = contents;
|
|
|
settings.Add(setting);
|
|
|
}
|
|
|
- client.Save(settings, "", (o, e) => { });
|
|
|
+ Provider.Save(settings);
|
|
|
}
|
|
|
|
|
|
public override void Delete(Action<Exception?>? callback = null)
|
|
|
{
|
|
|
ConfigurationCache.Clear<T>(ConfigurationCacheType.User, GetKey());
|
|
|
|
|
|
- var client = new Client<UserSettings>();
|
|
|
-
|
|
|
var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
|
|
|
.And(x => x.Key).IsEqualTo(Section)
|
|
|
- .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
|
|
|
+ .And(x => x.UserID).IsEqualTo(Provider.UserID);
|
|
|
|
|
|
UserSettings? result;
|
|
|
try
|
|
|
{
|
|
|
- result = client.Load(filter).FirstOrDefault();
|
|
|
+ result = Provider.Load(filter).FirstOrDefault();
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
- Logger.Send(LogType.Error, ClientFactory.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
|
|
|
+ Logger.Send(LogType.Error, Provider.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
|
|
|
result = null;
|
|
|
}
|
|
|
|
|
|
if (result != null)
|
|
|
{
|
|
|
- if(callback != null)
|
|
|
- {
|
|
|
- client.Delete(result, "", (o, e) => { callback(e); });
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- client.Delete(result, "");
|
|
|
- }
|
|
|
+ Provider.Delete(result, callback);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override string[] Sections()
|
|
|
{
|
|
|
- var result = new Client<UserSettings>().Query(
|
|
|
- new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
|
|
|
+ var result = Provider.Query(
|
|
|
+ new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(Provider.UserID),
|
|
|
Columns.None<UserSettings>().Add(x => x.Key),
|
|
|
new SortOrder<UserSettings>(x => x.Key)
|
|
|
).Rows.Select(r => r.Get<UserSettings, string>(c => c.Key)).Distinct().ToArray();
|