-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters