Files
Nemera/src/Lumina.Infrastructure/Core/Plugins/PluginSettingsStore.cs
T

54 lines
2.5 KiB
C#

#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Mapping.Plugins;
using Lumina.Plugins.Contracts.Core.Plugins;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Infrastructure.Core.Plugins;
/// <summary>
/// Reads the settings persisted by the host for a plugin.
/// </summary>
internal sealed class PluginSettingsStore : IPluginSettingsStore
{
private readonly IUnitOfWork _unitOfWork;
private readonly ILogger<PluginSettingsStore> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="PluginSettingsStore"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
/// <param name="logger">Injected logger used to report the failures to read the plugin settings.</param>
public PluginSettingsStore(IUnitOfWork unitOfWork, ILogger<PluginSettingsStore> logger)
{
_unitOfWork = unitOfWork;
_logger = logger;
}
/// <summary>
/// Gets the settings persisted by the host for the plugin identified by <paramref name="pluginId"/>.
/// </summary>
/// <param name="pluginId">The unique identifier of the plugin whose settings are read.</param>
/// <param name="cancellationToken">Cancellation token that can be used to cancel the operation.</param>
/// <returns>The persisted settings keyed by setting key, or <see langword="null"/> when no settings were persisted for the plugin.</returns>
public async Task<IReadOnlyDictionary<string, string>?> GetSettingsAsync(Guid pluginId, CancellationToken cancellationToken)
{
Result<PluginEntity?> getPluginResult = await _unitOfWork.PluginRepository.GetByIdAsync(pluginId, cancellationToken).ConfigureAwait(false);
if (getPluginResult.IsFailure)
{
_logger.LogWarning("Failed to read the settings of the plugin with Id '{PluginId}': {Error}", pluginId, getPluginResult.FirstError.Description);
return null;
}
return getPluginResult.Value?.ToSettings();
}
}