Adds persistence for plugins and library metadata provider configurations

This commit is contained in:
BinaryAlley committed 2026-08-12 11:03:14 -07:00
1 parent f33817f2d2
commit f71e72a948
16 files changed
+3381

No files matched your search

@@ -0,0 +1,82 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Plugins;
/// <summary>
/// Repository for the metadata provider configurations of media libraries.
/// </summary>
internal sealed class LibraryMetadataProviderConfigurationRepository : ILibraryMetadataProviderConfigurationRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="LibraryMetadataProviderConfigurationRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public LibraryMetadataProviderConfigurationRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Gets all the metadata provider configurations of the media library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="LibraryMetadataProviderConfigurationEntity"/>, or an error.</returns>
public async Task<ErrorOr<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>> GetByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken)
{
return await _luminaDbContext.LibraryMetadataProviderConfigurations
.Where(configuration => configuration.LibraryId == libraryId)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the metadata provider configuration of the media library identified by <paramref name="libraryId"/> for the plugin identified by <paramref name="pluginId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose metadata provider configuration is retrieved.</param>
/// <param name="pluginId">The Id of the plugin whose metadata provider configuration is retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either the <see cref="LibraryMetadataProviderConfigurationEntity"/>, or an error.</returns>
public async Task<ErrorOr<LibraryMetadataProviderConfigurationEntity?>> GetByLibraryAndPluginIdAsync(Guid libraryId, Guid pluginId, CancellationToken cancellationToken)
{
return await _luminaDbContext.LibraryMetadataProviderConfigurations
.FirstOrDefaultAsync(configuration => configuration.LibraryId == libraryId && configuration.PluginId == pluginId, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Inserts a metadata provider configuration into the storage medium, or updates it when it already exists.
/// </summary>
/// <param name="configuration">The metadata provider configuration to insert or update.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<ErrorOr<Updated>> UpsertAsync(LibraryMetadataProviderConfigurationEntity configuration, CancellationToken cancellationToken)
{
LibraryMetadataProviderConfigurationEntity? existingConfiguration = await _luminaDbContext.LibraryMetadataProviderConfigurations
.FirstOrDefaultAsync(repositoryConfiguration => repositoryConfiguration.LibraryId == configuration.LibraryId && repositoryConfiguration.PluginId == configuration.PluginId, cancellationToken).ConfigureAwait(false);
if (existingConfiguration is null)
{
_luminaDbContext.LibraryMetadataProviderConfigurations.Add(configuration);
}
else
{
// update only the mutable fields, preserving the Id of the existing configuration
existingConfiguration.LibraryId = configuration.LibraryId;
existingConfiguration.PluginId = configuration.PluginId;
existingConfiguration.IsEnabled = configuration.IsEnabled;
existingConfiguration.Rank = configuration.Rank;
}
return Result.Updated;
}
}
@@ -0,0 +1,98 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.SharedKernel.Common.Errors;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Plugins;
/// <summary>
/// Repository for plugins.
/// </summary>
internal sealed class PluginRepository : IPluginRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="PluginRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public PluginRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Gets a plugin by its Id.
/// </summary>
/// <param name="id">The Id of the plugin to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a <see cref="PluginEntity"/>, or an error.</returns>
public async Task<ErrorOr<PluginEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.Plugins
.FirstOrDefaultAsync(plugin => plugin.Id == id, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets all the detected plugins.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="PluginEntity"/>, or an error.</returns>
public async Task<ErrorOr<IEnumerable<PluginEntity>>> GetAllAsync(CancellationToken cancellationToken)
{
return await _luminaDbContext.Plugins
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Inserts a plugin into the storage medium, or updates it when it already exists.
/// </summary>
/// <param name="plugin">The plugin to insert or update.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<ErrorOr<Updated>> UpsertAsync(PluginEntity plugin, CancellationToken cancellationToken)
{
PluginEntity? existingPlugin = await _luminaDbContext.Plugins
.FirstOrDefaultAsync(repositoryPlugin => repositoryPlugin.Id == plugin.Id, cancellationToken).ConfigureAwait(false);
if (existingPlugin is null)
{
_luminaDbContext.Plugins.Add(plugin);
}
else
{
// update only the detection fields, preserving the stored settings and creation date
existingPlugin.Name = plugin.Name;
existingPlugin.Author = plugin.Author;
existingPlugin.Version = plugin.Version;
existingPlugin.Description = plugin.Description;
existingPlugin.LoadStatus = plugin.LoadStatus;
existingPlugin.LoadError = plugin.LoadError;
}
return Result.Updated;
}
/// <summary>
/// Updates the settings of the plugin identified by <paramref name="pluginId"/>.
/// </summary>
/// <param name="pluginId">The Id of the plugin whose settings are updated.</param>
/// <param name="settingsJson">The serialized settings of the plugin.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<ErrorOr<Updated>> UpdateSettingsAsync(Guid pluginId, string? settingsJson, CancellationToken cancellationToken)
{
PluginEntity? existingPlugin = await _luminaDbContext.Plugins
.FirstOrDefaultAsync(repositoryPlugin => repositoryPlugin.Id == pluginId, cancellationToken).ConfigureAwait(false);
if (existingPlugin is null)
return Errors.Plugins.PluginNotFound;
existingPlugin.SettingsJson = settingsJson;
return Result.Updated;
}
}
@@ -2,6 +2,7 @@
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Microsoft.EntityFrameworkCore;
#endregion
@@ -28,6 +29,8 @@ public class LuminaDbContext : DbContext
public virtual DbSet<PermissionEntity> Permissions { get; set; } = null!;
public virtual DbSet<UserPermissionEntity> UserPermissions { get; set; } = null!;
public virtual DbSet<RolePermissionEntity> RolePermissions { get; set; } = null!;
public virtual DbSet<PluginEntity> Plugins { get; set; } = null!;
public virtual DbSet<LibraryMetadataProviderConfigurationEntity> LibraryMetadataProviderConfigurations { get; set; } = null!;
/// <summary>
/// Initializes a new instance of the <see cref="LuminaDbContext"/> class.