mirror of
https://github.com/aljazceru/BTCPayServerPlugins.git
synced 2025-12-17 07:34:24 +01:00
initial commit
This commit is contained in:
46
Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatService.cs
Normal file
46
Plugins/BTCPayServer.Plugins.FixedFloat/FixedFloatService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Abstractions.Contracts;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace BTCPayServer.Plugins.FixedFloat
|
||||
{
|
||||
public class FixedFloatService
|
||||
{
|
||||
private readonly ISettingsRepository _settingsRepository;
|
||||
private readonly IStoreRepository _storeRepository;
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
public FixedFloatService(ISettingsRepository settingsRepository, IStoreRepository storeRepository, IMemoryCache memoryCache)
|
||||
{
|
||||
_settingsRepository = settingsRepository;
|
||||
_storeRepository = storeRepository;
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
public async Task<FixedFloatSettings> GetFixedFloatForStore(string storeId)
|
||||
{
|
||||
var k = $"{nameof(FixedFloatSettings)}_{storeId}";
|
||||
return await _memoryCache.GetOrCreateAsync(k, async _ =>
|
||||
{
|
||||
var res = await _storeRepository.GetSettingAsync<FixedFloatSettings>(storeId,
|
||||
nameof(FixedFloatSettings));
|
||||
if (res is not null) return res;
|
||||
res = await _settingsRepository.GetSettingAsync<FixedFloatSettings>(k);
|
||||
|
||||
if (res is not null)
|
||||
{
|
||||
await SetFixedFloatForStore(storeId, res);
|
||||
}
|
||||
|
||||
await _settingsRepository.UpdateSetting<FixedFloatSettings>(null, k);
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task SetFixedFloatForStore(string storeId, FixedFloatSettings fixedFloatSettings)
|
||||
{
|
||||
var k = $"{nameof(FixedFloatSettings)}_{storeId}";
|
||||
await _storeRepository.UpdateSetting(storeId, nameof(FixedFloatSettings), fixedFloatSettings);
|
||||
_memoryCache.Set(k, fixedFloatSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user