SocialPub/PrivaPub/Services/AppConfigurationService.cs
2023-02-19 00:43:43 +01:00

37 lines
996 B
C#

using Microsoft.Extensions.Options;
using MongoDB.Entities;
using PrivaPub.Models;
using PrivaPub.StaticServices;
namespace PrivaPub.Services
{
public class AppConfigurationService
{
public AppConfiguration AppConfiguration;
readonly DbEntities _dbEntities;
AppConfiguration _appConfigurationFromFile;
public AppConfigurationService(DbEntities dbEntities, IOptionsMonitor<AppConfiguration> appConfiguration)
{
_dbEntities = dbEntities;
_appConfigurationFromFile = appConfiguration.CurrentValue;
}
public async Task Init()
{
var dbAppConfiguration = await _dbEntities.AppConfiguration.ExecuteFirstAsync();
if (dbAppConfiguration != default)
{
if (AppConfiguration == default || AppConfiguration.LastUpdateDate != dbAppConfiguration.LastUpdateDate)
AppConfiguration = dbAppConfiguration;
return;
}
dbAppConfiguration = _appConfigurationFromFile;
await dbAppConfiguration.SaveAsync();
AppConfiguration = dbAppConfiguration;
}
}
}