Refactors the track slice across the stack

This commit is contained in:
BinaryAlley committed 2026-09-25 13:19:28 -07:00
1 parent e842c7baaf
commit 6bd0cf6d42
37 files changed
+853 -403

No files matched your search

@@ -157,37 +157,6 @@ public class TrackConfiguration : IEntityTypeConfiguration<TrackEntity>
j.ToTable("TrackGenres");
});
builder.OwnsMany(track => track.Ratings, ratingBuilder =>
{
ratingBuilder.ToTable("TrackRatings");
ratingBuilder.WithOwner()
.HasForeignKey("TrackId");
ratingBuilder.Property<Guid>("Id")
.ValueGeneratedOnAdd();
ratingBuilder.HasKey("Id");
ratingBuilder.Property(rating => rating.Value)
.HasColumnType("decimal(3,2)")
.IsRequired();
ratingBuilder.Property(rating => rating.MaxValue)
.HasColumnType("decimal(3,2)")
.IsRequired();
ratingBuilder.Property(rating => rating.VoteCount)
.IsRequired(false);
ratingBuilder.Property(rating => rating.Source)
.HasConversion<string>()
.HasMaxLength(50)
.IsRequired(false);
});
builder.HasMany(track => track.Contributors)
.WithOne()
.HasForeignKey(contributor => contributor.TrackId)
.OnDelete(DeleteBehavior.Cascade);
builder.OwnsMany(track => track.Moods, moodBuilder =>
{
moodBuilder.ToTable("TrackMoods");
@@ -217,6 +186,37 @@ public class TrackConfiguration : IEntityTypeConfiguration<TrackEntity>
.IsRequired();
});
builder.HasMany(track => track.Contributors)
.WithOne()
.HasForeignKey(contributor => contributor.TrackId)
.OnDelete(DeleteBehavior.Cascade);
builder.OwnsMany(track => track.Ratings, ratingBuilder =>
{
ratingBuilder.ToTable("TrackRatings");
ratingBuilder.WithOwner()
.HasForeignKey("TrackId");
ratingBuilder.Property<Guid>("Id")
.ValueGeneratedOnAdd();
ratingBuilder.HasKey("Id");
ratingBuilder.Property(rating => rating.Value)
.HasColumnType("decimal(3,2)")
.IsRequired();
ratingBuilder.Property(rating => rating.MaxValue)
.HasColumnType("decimal(3,2)")
.IsRequired();
ratingBuilder.Property(rating => rating.VoteCount)
.IsRequired(false);
ratingBuilder.Property(rating => rating.Source)
.HasConversion<string>()
.HasMaxLength(50)
.IsRequired(false);
});
builder.HasIndex(track => new { track.AlbumId, track.TrackNumber });
builder.HasIndex(track => new { track.LibraryId, track.Path })
.IsUnique(); // the same physical track file cannot appear twice in the same library
@@ -47,8 +47,10 @@ internal sealed class TrackRepository : ITrackRepository
return Errors.Music.TrackAlreadyExists;
// A track is unique within its library by its file system path, so the same file can never be registered twice in the same library.
bool doesTrackPathExist = await _luminaDbContext.Tracks.AnyAsync(repositoryTrack => repositoryTrack.LibraryId == track.LibraryId && repositoryTrack.Path == track.Path, cancellationToken: cancellationToken).ConfigureAwait(false);
if (doesTrackPathExist)
Result<IReadOnlyCollection<string>> getExistingPathsResult = await GetExistingPathsAsync(track.LibraryId, [track.Path], cancellationToken).ConfigureAwait(false);
if (getExistingPathsResult.IsFailure)
return getExistingPathsResult.Errors;
if (getExistingPathsResult.Value.Count > 0)
return Errors.Music.TrackAlreadyExists;
// Fetch existing tags and genres.
@@ -67,6 +69,27 @@ internal sealed class TrackRepository : ITrackRepository
return Result.Created;
}
/// <summary>
/// Gets the subset of <paramref name="paths"/> that is already used by a track of the library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="libraryId">The Id of the library whose tracks are searched.</param>
/// <param name="paths">The track paths to check.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the paths that are already used, or an error.</returns>
public async Task<Result<IReadOnlyCollection<string>>> GetExistingPathsAsync(Guid libraryId, IReadOnlyCollection<string> paths, CancellationToken cancellationToken)
{
if (paths.Count == 0)
return Result.From<IReadOnlyCollection<string>>([]);
// The stored paths are compared ordinally, matching the case sensitive comparison used by the unique index of the storage medium.
List<string> distinctPaths = [.. paths.Distinct(StringComparer.Ordinal)];
List<string> existingPaths = await _luminaDbContext.Tracks
.Where(repositoryTrack => repositoryTrack.LibraryId == libraryId && distinctPaths.Contains(repositoryTrack.Path))
.Select(repositoryTrack => repositoryTrack.Path)
.ToListAsync(cancellationToken).ConfigureAwait(false);
return Result.From<IReadOnlyCollection<string>>(existingPaths);
}
/// <summary>
/// Updates an existing track, replacing only the editable data that actually changed, while preserving its identity, the identity of its children, and its audit columns.
/// </summary>