btcpayserver/BTCPayServer/Services/SettingsRepository.cs
Andrew Camilleri 39b5462809
Remove only dependency on Dbriize (TextSearch in new invoice column) (#2029)
* Remove only dependency on Dbriize (TextSearch in new invoice column)

* Switch to table for invoice text search

* Adding missing using after rebase

* Removing database migration in preparation for refresh

* Database Migration: Adding InvoiceSearchData

* Refactoring InvoicesRepository to make AddToTextSearch static and non-async

Operation as async is too expensive for simple filtering and AddRange

* Renaming InvoiceQuery property to Take

More inline with what property does by convention, Take is used in conjuction with Skip

* Refactoring SettingsRepository so update of settings can happen in another context

* Adding DbMigrationsHostedService that performs long running data migrations

* Commenting special placing of MigrationStartupTask

* Simplifying code and leaving comment on expected flow

* Resolving problems after merge

* Database Migration: Refreshing database migration, ensuring no unintended changes on ModelSnapshot

Co-authored-by: rockstardev <rockstardev@users.noreply.github.com>
Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
2020-12-28 19:10:53 +09:00

83 lines
2.6 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Data;
using BTCPayServer.Events;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace BTCPayServer.Services
{
public class SettingsRepository : ISettingsRepository
{
private readonly ApplicationDbContextFactory _ContextFactory;
private readonly EventAggregator _EventAggregator;
public SettingsRepository(ApplicationDbContextFactory contextFactory, EventAggregator eventAggregator)
{
_ContextFactory = contextFactory;
_EventAggregator = eventAggregator;
}
public async Task<T> GetSettingAsync<T>(string name = null)
{
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);
}
}
public async Task UpdateSetting<T>(T obj, string name = null)
{
using (var ctx = _ContextFactory.CreateContext())
{
var settings = UpdateSettingInContext<T>(ctx, obj, name);
try
{
await ctx.SaveChangesAsync();
}
catch (DbUpdateException)
{
ctx.Entry(settings).State = EntityState.Added;
await ctx.SaveChangesAsync();
}
}
_EventAggregator.Publish(new SettingsChanged<T>()
{
Settings = obj
});
}
public SettingData UpdateSettingInContext<T>(ApplicationDbContext ctx, T obj, string name = null)
{
name ??= obj.GetType().FullName;
var settings = new SettingData();
settings.Id = name;
settings.Value = Serialize(obj);
ctx.Attach(settings);
ctx.Entry(settings).State = EntityState.Modified;
return settings;
}
private T Deserialize<T>(string value)
{
return JsonConvert.DeserializeObject<T>(value);
}
private string Serialize<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
public async Task<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default)
{
return (await _EventAggregator.WaitNext<SettingsChanged<T>>(cancellationToken)).Settings;
}
}
}