Skip to content

Commit

Permalink
fix channel serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed May 4, 2024
1 parent cd39e67 commit 7502d1f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 96 deletions.
53 changes: 0 additions & 53 deletions BTCPayApp.Core/Data/DatabaseConfigProvider.cs

This file was deleted.

47 changes: 47 additions & 0 deletions BTCPayApp.Core/DatabaseConfigProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Text.Json;
using BTCPayApp.Core.Contracts;
using BTCPayApp.Core.Data;
using Microsoft.EntityFrameworkCore;

namespace BTCPayApp.Core;

public class DatabaseConfigProvider: IConfigProvider
{
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;

public DatabaseConfigProvider(IDbContextFactory<AppDbContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}

public async Task<T?> Get<T>(string key)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
var config = await dbContext.Settings.FindAsync(key);
if (typeof(T) == typeof(byte[]))
return (T?) (config?.Value as object);
return config is null ? default : JsonSerializer.Deserialize<T>(config.Value);
}

public async Task Set<T>(string key, T? value)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
if (value is null)
{
try
{

dbContext.Settings.Remove(new Setting() {Key = key});
await dbContext.SaveChangesAsync();
}
catch (DbUpdateException e)
{
}
return;
}

var newValue = typeof(T) == typeof(byte[])? value as byte[]:JsonSerializer.SerializeToUtf8Bytes(value);
await dbContext.Upsert(new Setting() {Key = key, Value = newValue}).RunAsync();

}
}
44 changes: 1 addition & 43 deletions BTCPayApp.Core/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json;
using BTCPayApp.CommonServer;
using BTCPayApp.CommonServer;
using BTCPayApp.Core.Attempt2;
using BTCPayApp.Core.Contracts;
using BTCPayApp.Core.Data;
Expand Down Expand Up @@ -45,47 +44,6 @@ public static IServiceCollection ConfigureBTCPayAppCore(this IServiceCollection
}
}

public class DatabaseConfigProvider: IConfigProvider
{
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;

public DatabaseConfigProvider(IDbContextFactory<AppDbContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}

public async Task<T?> Get<T>(string key)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
var config = await dbContext.Settings.FindAsync(key);
if (typeof(T) == typeof(byte[]))
return (T?) (config?.Value as object);
return config is null ? default : JsonSerializer.Deserialize<T>(config.Value);
}

public async Task Set<T>(string key, T? value)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
if (value is null)
{
try
{

dbContext.Settings.Remove(new Setting() {Key = key});
await dbContext.SaveChangesAsync();
}
catch (DbUpdateException e)
{
}
return;
}

var newValue = JsonSerializer.SerializeToUtf8Bytes(value);
await dbContext.Upsert(new Setting() {Key = key, Value = newValue}).RunAsync();

}
}

public class AppDatabaseMigrator: IHostedService
{
private readonly ILogger<AppDatabaseMigrator> _logger;
Expand Down

0 comments on commit 7502d1f

Please sign in to comment.