Adds the media library artwork aggregation services, enrichers, and their provider contracts

This commit is contained in:
BinaryAlley committed 2026-10-09 01:59:00 +03:00
1 parent 00376c8313
commit 9e118864c3
33 files changed
+4621 -1118

No files matched your search

@@ -0,0 +1,78 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
#endregion
namespace Lumina.DataAccess.Common.Configuration;
/// <summary>
/// Configures the entity mapping for the <see cref="MusicArtworkEntity"/> entity.
/// </summary>
public class MusicArtworkConfiguration : IEntityTypeConfiguration<MusicArtworkEntity>
{
/// <summary>
/// Configures the <see cref="MusicArtworkEntity"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<MusicArtworkEntity> builder)
{
builder.ToTable("MusicArtwork");
builder.HasKey(musicArtwork => musicArtwork.Id);
builder.Property(musicArtwork => musicArtwork.Id)
.ValueGeneratedNever() // because EF always tries to generate the value for the Id, and because we generate it as part of the aggregate root, we need to tell EF not to generate it
.HasColumnOrder(0);
builder.Property(musicArtwork => musicArtwork.OwnerType)
.IsRequired()
.HasConversion<string>()
.HasMaxLength(50)
.HasColumnOrder(1);
builder.Property(musicArtwork => musicArtwork.OwnerId)
.IsRequired()
.HasColumnOrder(2);
builder.Property(musicArtwork => musicArtwork.ArtworkType)
.IsRequired()
.HasConversion<string>()
.HasMaxLength(50)
.HasColumnOrder(3);
builder.Property(musicArtwork => musicArtwork.Ordinal)
.IsRequired()
.HasColumnOrder(4);
builder.Property(musicArtwork => musicArtwork.FileName)
.HasMaxLength(2048)
.HasColumnOrder(5);
builder.Property(musicArtwork => musicArtwork.ContentHash)
.HasColumnOrder(6);
builder.Property(musicArtwork => musicArtwork.Status)
.IsRequired()
.HasConversion<string>()
.HasMaxLength(20)
.HasColumnOrder(7);
builder.Property(musicArtwork => musicArtwork.Provider)
.HasMaxLength(100)
.HasColumnOrder(8);
builder.Property(musicArtwork => musicArtwork.LastUpdateUtc)
.HasColumnOrder(9);
// audit
builder.Property(musicArtwork => musicArtwork.CreatedOnUtc)
.IsRequired()
.HasColumnOrder(10);
builder.Property(musicArtwork => musicArtwork.CreatedBy)
.IsRequired()
.HasColumnOrder(11);
builder.Property(musicArtwork => musicArtwork.UpdatedOnUtc)
.HasDefaultValue(null)
.HasColumnOrder(12);
builder.Property(musicArtwork => musicArtwork.UpdatedBy)
.HasDefaultValue(null)
.HasColumnOrder(13);
builder.HasIndex(musicArtwork => new { musicArtwork.OwnerType, musicArtwork.OwnerId, musicArtwork.ArtworkType, musicArtwork.Ordinal })
.IsUnique();
builder.HasIndex(musicArtwork => musicArtwork.Status);
}
}
@@ -0,0 +1,109 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Common.DataAccess.Repositories.MusicLibrary;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
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.MusicLibrary;
/// <summary>
/// Repository for the artwork of the music library items.
/// </summary>
internal sealed class MusicArtworkRepository : IMusicArtworkRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="MusicArtworkRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public MusicArtworkRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Adds a range of pieces of artwork of music library items.
/// </summary>
/// <param name="artwork">The pieces of artwork to add.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public Task<Result<Created>> InsertRangeAsync(IReadOnlyCollection<MusicArtworkEntity> artwork, CancellationToken cancellationToken)
{
if (artwork.Count > 0)
_luminaDbContext.MusicArtwork.AddRange(artwork);
return Task.FromResult<Result<Created>>(Result.Created);
}
/// <summary>
/// Gets the pieces of artwork owned by the music library item of the provided <paramref name="ownerType"/> identified by <paramref name="ownerId"/>.
/// </summary>
/// <param name="ownerType">The type of the music library item that owns the artwork.</param>
/// <param name="ownerId">The Id of the music library item that owns the artwork.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="MusicArtworkEntity"/>, or an error.</returns>
public async Task<Result<IReadOnlyList<MusicArtworkEntity>>> GetByOwnerAsync(MusicArtworkOwnerType ownerType, Guid ownerId, CancellationToken cancellationToken)
{
IReadOnlyList<MusicArtworkEntity> artwork = await _luminaDbContext.MusicArtwork
.AsNoTracking()
.Where(musicArtwork => musicArtwork.OwnerType == ownerType && musicArtwork.OwnerId == ownerId)
.OrderBy(musicArtwork => musicArtwork.ArtworkType)
.ThenBy(musicArtwork => musicArtwork.Ordinal)
.ToListAsync(cancellationToken).ConfigureAwait(false);
return Result.From(artwork);
}
/// <summary>
/// Deletes all the pieces of artwork owned by the music library item of the provided <paramref name="ownerType"/> identified by <paramref name="ownerId"/>.
/// </summary>
/// <param name="ownerType">The type of the music library item that owns the artwork.</param>
/// <param name="ownerId">The Id of the music library item that owns the artwork.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Deleted>> DeleteByOwnerAsync(MusicArtworkOwnerType ownerType, Guid ownerId, CancellationToken cancellationToken)
{
await _luminaDbContext.MusicArtwork
.Where(musicArtwork => musicArtwork.OwnerType == ownerType && musicArtwork.OwnerId == ownerId)
.ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
return Result.Deleted;
}
/// <summary>
/// Resets the artwork enrichment status of all the pieces of artwork owned by the music library items of the media library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose artwork is reset.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Updated>> ResetStatusForLibraryAsync(Guid libraryId, CancellationToken cancellationToken)
{
// The artwork table is shared by all the music library item types, so the owners of the artwork of this library are collected per type.
List<Guid> artistIds = await _luminaDbContext.Artists
.Where(artist => artist.LibraryId == libraryId)
.Select(artist => artist.Id)
.ToListAsync(cancellationToken).ConfigureAwait(false);
List<Guid> albumIds = await _luminaDbContext.Albums
.Where(album => album.LibraryId == libraryId)
.Select(album => album.Id)
.ToListAsync(cancellationToken).ConfigureAwait(false);
List<Guid> trackIds = await _luminaDbContext.Tracks
.Where(track => track.LibraryId == libraryId)
.Select(track => track.Id)
.ToListAsync(cancellationToken).ConfigureAwait(false);
await _luminaDbContext.MusicArtwork
.Where(musicArtwork => (musicArtwork.OwnerType == MusicArtworkOwnerType.Artist && artistIds.Contains(musicArtwork.OwnerId))
|| (musicArtwork.OwnerType == MusicArtworkOwnerType.Album && albumIds.Contains(musicArtwork.OwnerId))
|| (musicArtwork.OwnerType == MusicArtworkOwnerType.Track && trackIds.Contains(musicArtwork.OwnerId)))
.ExecuteUpdateAsync(setters => setters.SetProperty(musicArtwork => musicArtwork.Status, ArtworkStatus.Pending), cancellationToken).ConfigureAwait(false);
return Result.Updated;
}
}