Update test plugin with more data related functionality

This commit is contained in:
Kukks 2022-07-11 11:29:57 +02:00
parent 316d39f24c
commit a64b8fb310
No known key found for this signature in database
GPG Key ID: 8E5530D9D1C93097
2 changed files with 46 additions and 8 deletions

View File

@ -19,6 +19,7 @@ namespace BTCPayServer.Plugins.Test
{
services.AddSingleton<IUIExtension>(new UIExtension("TestExtensionNavExtension", "header-nav"));
services.AddHostedService<ApplicationPartsLogger>();
services.AddHostedService<TestPluginMigrationRunner>();
services.AddSingleton<TestPluginService>();
services.AddSingleton<TestPluginDbContextFactory>();
services.AddDbContext<TestPluginDbContext>((provider, o) =>
@ -27,13 +28,5 @@ namespace BTCPayServer.Plugins.Test
factory.ConfigureBuilder(o);
});
}
public override void Execute(IApplicationBuilder applicationBuilder, IServiceProvider applicationBuilderApplicationServices)
{
base.Execute(applicationBuilder, applicationBuilderApplicationServices);
applicationBuilderApplicationServices.GetService<TestPluginDbContextFactory>().CreateContext().Database.Migrate();
applicationBuilderApplicationServices.GetService<TestPluginService>().AddTestDataRecord().GetAwaiter().GetResult();
}
}
}

View File

@ -0,0 +1,45 @@
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Plugins.Test.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
namespace BTCPayServer.Plugins.Test;
public class TestPluginMigrationRunner:IHostedService
{
public class TestPluginDataMigrationHistory
{
public bool UpdatedSomething { get; set; }
}
private readonly TestPluginDbContextFactory _testPluginDbContextFactory;
private readonly ISettingsRepository _settingsRepository;
private readonly TestPluginService _testPluginService;
public TestPluginMigrationRunner(TestPluginDbContextFactory testPluginDbContextFactory, ISettingsRepository settingsRepository, TestPluginService testPluginService)
{
_testPluginDbContextFactory = testPluginDbContextFactory;
_settingsRepository = settingsRepository;
_testPluginService = testPluginService;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
var settings = await _settingsRepository.GetSettingAsync<TestPluginDataMigrationHistory>() ??
new TestPluginDataMigrationHistory();
await using var ctx = _testPluginDbContextFactory.CreateContext();
await ctx.Database.MigrateAsync(cancellationToken: cancellationToken);
if (!settings.UpdatedSomething)
{
await _testPluginService.AddTestDataRecord();
settings.UpdatedSomething = true;
await _settingsRepository.UpdateSetting(settings);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}