Adds music play history persistence and reporting endpoints

This commit is contained in:
BinaryAlley committed 2026-10-19 22:16:09 +03:00
1 parent 9919965a73
commit 009a0c8156
55 files changed
+6426 -3

No files matched your search

@@ -0,0 +1,95 @@
#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="MusicPlayRecordEntity"/> entity.
/// </summary>
public class MusicPlayRecordConfiguration : IEntityTypeConfiguration<MusicPlayRecordEntity>
{
/// <summary>
/// Configures the <see cref="MusicPlayRecordEntity"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<MusicPlayRecordEntity> builder)
{
builder.ToTable("MusicPlayRecords");
builder.HasKey(playRecord => playRecord.Id);
builder.Property(playRecord => playRecord.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(playRecord => playRecord.UserId)
.IsRequired()
.HasColumnOrder(1);
builder.Property(playRecord => playRecord.TrackId)
.IsRequired()
.HasColumnOrder(2);
builder.Property(playRecord => playRecord.LibraryId)
.IsRequired()
.HasColumnOrder(3);
builder.Property(playRecord => playRecord.AlbumId)
.IsRequired()
.HasColumnOrder(4);
builder.Property(playRecord => playRecord.ArtistId)
.IsRequired()
.HasColumnOrder(5);
builder.Property(playRecord => playRecord.Status)
.IsRequired()
.HasConversion<string>()
.HasMaxLength(20)
.HasColumnOrder(6);
builder.Property(playRecord => playRecord.LastPositionMilliseconds)
.IsRequired()
.HasColumnOrder(7);
builder.Property(playRecord => playRecord.ListenedDurationMilliseconds)
.IsRequired()
.HasColumnOrder(8);
builder.Property(playRecord => playRecord.EndedOnUtc)
.HasColumnOrder(9);
// The version is the optimistic concurrency token: every update increments it, and an update whose loaded version no longer matches
// the stored one affects no row and fails, instead of reverting a play that another report already completed or stopped.
builder.Property(playRecord => playRecord.Version)
.IsRequired()
.IsConcurrencyToken()
.HasColumnOrder(10);
// Audit.
builder.Property(playRecord => playRecord.CreatedOnUtc)
.IsRequired()
.HasColumnOrder(11);
builder.Property(playRecord => playRecord.CreatedBy)
.IsRequired()
.HasColumnOrder(12);
builder.Property(playRecord => playRecord.UpdatedOnUtc)
.HasDefaultValue(null)
.HasColumnOrder(13);
builder.Property(playRecord => playRecord.UpdatedBy)
.HasDefaultValue(null)
.HasColumnOrder(14);
// The referenced entities are kept by Id only, without foreign keys, so a play survives the deletion of the track it references and the
// statistics of the listening history still count it.
// Enables the chronological reads of the plays of a user, which the statistics are built from.
builder.HasIndex(playRecord => new { playRecord.UserId, playRecord.CreatedOnUtc });
// Enables the reads of the plays of a track, which the per track statistics are built from.
builder.HasIndex(playRecord => playRecord.TrackId);
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,57 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lumina.DataAccess.Common.Migrations
{
/// <inheritdoc />
public partial class AddsMusicPlayHistory : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "MusicPlayRecords",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
UserId = table.Column<Guid>(type: "TEXT", nullable: false),
TrackId = table.Column<Guid>(type: "TEXT", nullable: false),
LibraryId = table.Column<Guid>(type: "TEXT", nullable: false),
AlbumId = table.Column<Guid>(type: "TEXT", nullable: false),
ArtistId = table.Column<Guid>(type: "TEXT", nullable: false),
Status = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
LastPositionMilliseconds = table.Column<long>(type: "INTEGER", nullable: false),
ListenedDurationMilliseconds = table.Column<long>(type: "INTEGER", nullable: false),
EndedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
Version = table.Column<long>(type: "INTEGER", nullable: false),
CreatedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
CreatedBy = table.Column<Guid>(type: "TEXT", nullable: false),
UpdatedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
UpdatedBy = table.Column<Guid>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_MusicPlayRecords", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_MusicPlayRecords_TrackId",
table: "MusicPlayRecords",
column: "TrackId");
migrationBuilder.CreateIndex(
name: "IX_MusicPlayRecords_UserId_CreatedOnUtc",
table: "MusicPlayRecords",
columns: new[] { "UserId", "CreatedOnUtc" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "MusicPlayRecords");
}
}
}
@@ -960,6 +960,80 @@ namespace Lumina.DataAccess.Common.Migrations
b.ToTable("MusicLibraryScanItemMetadata", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary.MusicPlayRecordEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<Guid>("AlbumId")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<Guid>("ArtistId")
.HasColumnType("TEXT")
.HasColumnOrder(5);
b.Property<Guid>("CreatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(12);
b.Property<DateTime>("CreatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(11);
b.Property<DateTime?>("EndedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(9);
b.Property<long>("LastPositionMilliseconds")
.HasColumnType("INTEGER")
.HasColumnOrder(7);
b.Property<Guid>("LibraryId")
.HasColumnType("TEXT")
.HasColumnOrder(3);
b.Property<long>("ListenedDurationMilliseconds")
.HasColumnType("INTEGER")
.HasColumnOrder(8);
b.Property<string>("Status")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("TEXT")
.HasColumnOrder(6);
b.Property<Guid>("TrackId")
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<Guid?>("UpdatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(14);
b.Property<DateTime?>("UpdatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(13);
b.Property<Guid>("UserId")
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<long>("Version")
.IsConcurrencyToken()
.HasColumnType("INTEGER")
.HasColumnOrder(10);
b.HasKey("Id");
b.HasIndex("TrackId");
b.HasIndex("UserId", "CreatedOnUtc");
b.ToTable("MusicPlayRecords", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary.TrackContributorEntity", b =>
{
b.Property<Guid>("Id")