2017-09-27 07:18:09 +02:00
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using System.Linq;
|
2019-08-22 13:44:06 +02:00
|
|
|
|
using System.Threading;
|
2017-09-27 07:18:09 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2019-08-22 13:44:06 +02:00
|
|
|
|
using BTCPayServer.Events;
|
2017-09-27 07:18:09 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Services
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class SettingsRepository
|
|
|
|
|
{
|
|
|
|
|
private ApplicationDbContextFactory _ContextFactory;
|
2019-08-22 13:44:06 +02:00
|
|
|
|
private readonly EventAggregator _EventAggregator;
|
|
|
|
|
|
|
|
|
|
public SettingsRepository(ApplicationDbContextFactory contextFactory, EventAggregator eventAggregator)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
_ContextFactory = contextFactory;
|
2019-08-22 13:44:06 +02:00
|
|
|
|
_EventAggregator = eventAggregator;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-27 07:18:09 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public async Task<T> GetSettingAsync<T>()
|
|
|
|
|
{
|
|
|
|
|
var name = typeof(T).FullName;
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var data = await ctx.Settings.Where(s => s.Id == name).FirstOrDefaultAsync();
|
|
|
|
|
if (data == null)
|
|
|
|
|
return default(T);
|
|
|
|
|
return Deserialize<T>(data.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-27 07:18:09 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public async Task UpdateSetting<T>(T obj)
|
|
|
|
|
{
|
|
|
|
|
var name = obj.GetType().FullName;
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var settings = new SettingData();
|
|
|
|
|
settings.Id = name;
|
|
|
|
|
settings.Value = Serialize(obj);
|
|
|
|
|
ctx.Attach(settings);
|
|
|
|
|
ctx.Entry(settings).State = EntityState.Modified;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException)
|
|
|
|
|
{
|
|
|
|
|
ctx.Entry(settings).State = EntityState.Added;
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-22 13:44:06 +02:00
|
|
|
|
_EventAggregator.Publish(new SettingsChanged<T>()
|
2018-04-18 09:07:16 +02:00
|
|
|
|
{
|
2019-08-22 13:44:06 +02:00
|
|
|
|
Settings = obj
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-27 07:18:09 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private T Deserialize<T>(string value)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(value);
|
|
|
|
|
}
|
2017-09-27 07:18:09 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private string Serialize<T>(T obj)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.SerializeObject(obj);
|
|
|
|
|
}
|
2019-08-22 13:44:06 +02:00
|
|
|
|
|
|
|
|
|
public async Task<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default)
|
2018-04-18 09:07:16 +02:00
|
|
|
|
{
|
2019-08-22 13:44:06 +02:00
|
|
|
|
return (await _EventAggregator.WaitNext<SettingsChanged<T>>(cancellationToken)).Settings;
|
2018-04-18 09:07:16 +02:00
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-27 07:18:09 +02:00
|
|
|
|
}
|