82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Application;
|
|
using DigitalData.Core.Abstraction.Application.DTO;
|
|
using EnvelopeGenerator.Application.Contracts.Services;
|
|
using EnvelopeGenerator.Application.DTOs;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Application.Contracts.Repositories;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EnvelopeGenerator.Application.Services;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Obsolete("Use MediatR")]
|
|
public class ConfigService : ReadService<IConfigRepository, ConfigDto, Config, int>, IConfigService
|
|
{
|
|
private static readonly Guid DefaultConfigCacheId = Guid.NewGuid();
|
|
|
|
private readonly IMemoryCache _cache;
|
|
|
|
private readonly ILogger<ConfigService> _logger;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
/// <param name="mapper"></param>
|
|
/// <param name="memoryCache"></param>
|
|
/// <param name="logger"></param>
|
|
public ConfigService(IConfigRepository repository, IMapper mapper, IMemoryCache memoryCache, ILogger<ConfigService> logger) : base(repository, mapper)
|
|
{
|
|
_cache = memoryCache;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<ConfigDto>> ReadFirstAsync()
|
|
{
|
|
var config = await _repository.ReadFirstAsync();
|
|
return config is null
|
|
? Result.Fail<ConfigDto>().Notice(LogLevel.Error, Flag.DataIntegrityIssue, "There is no configuration in DB.")
|
|
: Result.Success(_mapper.Map<ConfigDto>(config));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the default configuration asynchronously.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 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.
|
|
/// </remarks>
|
|
/// <returns>
|
|
/// A task that represents the asynchronous read operation. The task result contains the default configuration as a <see cref="ConfigDto"/>.
|
|
/// </returns>
|
|
/// <exception cref="InvalidOperationException">
|
|
/// Thrown when the default configuration cannot be found.
|
|
/// </exception>
|
|
public async Task<ConfigDto> 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!;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<string> ReadDefaultSignatureHost() => (await ReadDefaultAsync()).SignatureHost;
|
|
} |