using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.Abstraction.Application.DTO;
using EnvelopeGenerator.Application.Interfaces.Services;
using EnvelopeGenerator.Application.Dto;
using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Application.Interfaces.Repositories;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
namespace EnvelopeGenerator.Application.Services;
///
///
///
[Obsolete("Use MediatR")]
public class ConfigService : ReadService, IConfigService
{
private static readonly Guid DefaultConfigCacheId = Guid.NewGuid();
private readonly IMemoryCache _cache;
private readonly ILogger _logger;
///
///
///
///
///
///
///
public ConfigService(IConfigRepository repository, IMapper mapper, IMemoryCache memoryCache, ILogger logger) : base(repository, mapper)
{
_cache = memoryCache;
_logger = logger;
}
///
///
///
///
public async Task> ReadFirstAsync()
{
var config = await _repository.ReadFirstAsync();
return config is null
? Result.Fail().Notice(LogLevel.Error, Flag.DataIntegrityIssue, "There is no configuration in DB.")
: Result.Success(_mapper.Map(config));
}
///
/// Reads the default configuration asynchronously.
///
///
/// The configuration is cached in memory upon the first retrieval. If the configuration is updated,
/// the application needs to be restarted for the changes to take effect as the memory cache will not be updated automatically.
///
///
/// A task that represents the asynchronous read operation. The task result contains the default configuration as a .
///
///
/// Thrown when the default configuration cannot be found.
///
public async Task ReadDefaultAsync()
{
var config = await _cache.GetOrCreateAsync(DefaultConfigCacheId, _ => ReadFirstAsync().ThenAsync(
Success: config => config,
Fail: (mssg, ntc) =>
{
_logger.LogNotice(ntc);
throw new InvalidOperationException("Default configuration cannot find.");
})
);
return config!;
}
///
///
///
///
public async Task ReadDefaultSignatureHost() => (await ReadDefaultAsync()).SignatureHost;
}