mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 06:11:10 +00:00
Adds the media consumption history, statistics, resume, and deletion queries
This commit is contained in:
1 parent
bde7ee4b1f
commit
f777af07cf
61 files changed
+2833
-35
No files matched your search
@@ -484,20 +484,57 @@ internal sealed class BookRepository : IBookRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the display names of the authors of the books, keyed by book Id, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyDictionary<Guid, string?>>> GetAuthorsDisplayNamesByBookIdsAsync(IReadOnlyCollection<Guid> bookIds, CancellationToken cancellationToken)
|
||||
{
|
||||
List<AuthorRow> authorRows = await _luminaDbContext.BookContributors
|
||||
.Where(bookContributor => bookIds.Contains(bookContributor.BookId) && bookContributor.Role == MediaContributorRole.Author)
|
||||
.Join(_luminaDbContext.MediaContributors,
|
||||
bookContributor => bookContributor.MediaContributorId,
|
||||
contributor => contributor.Id,
|
||||
(bookContributor, contributor) => new AuthorRow(bookContributor.BookId, contributor.DisplayName))
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
List<AuthorRow> authorRows = [];
|
||||
// The lookup is split into bounded chunks, so an unpaginated history never exceeds the storage parameter limit.
|
||||
const int LOOKUP_CHUNK_SIZE = 500;
|
||||
foreach (Guid[] chunk in bookIds.Chunk(LOOKUP_CHUNK_SIZE))
|
||||
{
|
||||
authorRows.AddRange(await _luminaDbContext.BookContributors
|
||||
.Where(bookContributor => chunk.Contains(bookContributor.BookId) && bookContributor.Role == MediaContributorRole.Author)
|
||||
.Join(_luminaDbContext.MediaContributors,
|
||||
bookContributor => bookContributor.MediaContributorId,
|
||||
contributor => contributor.Id,
|
||||
(bookContributor, contributor) => new AuthorRow(bookContributor.BookId, contributor.DisplayName))
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false));
|
||||
}
|
||||
|
||||
return authorRows
|
||||
.GroupBy(row => row.BookId)
|
||||
.ToDictionary(group => group.Key, group => group.Min(row => row.DisplayName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the books identified by <paramref name="bookIds"/>, optionally together with their navigation properties.
|
||||
/// </summary>
|
||||
/// <param name="bookIds">The Ids of the books to get.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the books should be loaded together with the books themselves.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the existing books, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyList<BookEntity>>> GetByIdsAsync(IReadOnlyCollection<Guid> bookIds, bool shouldIncludeNavigationProperties, CancellationToken cancellationToken)
|
||||
{
|
||||
List<BookEntity> books = [];
|
||||
// The lookup is split into bounded chunks, so even an unpaginated history never exceeds the storage parameter limit.
|
||||
const int LOOKUP_CHUNK_SIZE = 500;
|
||||
foreach (Guid[] chunk in bookIds.Chunk(LOOKUP_CHUNK_SIZE))
|
||||
{
|
||||
IQueryable<BookEntity> query = _luminaDbContext.Books.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(book => book.Tags)
|
||||
.Include(book => book.Genres)
|
||||
.Include(book => book.ISBNs)
|
||||
.Include(book => book.Ratings)
|
||||
.Include(book => book.Contributors)
|
||||
.Include(book => book.Artwork)
|
||||
.AsSplitQuery();
|
||||
}
|
||||
books.AddRange(await query.Where(book => chunk.Contains(book.Id)).ToListAsync(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
return Result.From<IReadOnlyList<BookEntity>>(books);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The author row of a book, joining the participation of an author contributor in a book with the display name of the contributor.
|
||||
/// </summary>
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
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.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Repository for the consumption records used to build consumption statistics.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The repository only binds the filters and returns the records. Counting, grouping, ordering, display data resolution, and mapping stay in the
|
||||
/// application handlers, so the same records can serve the summary, the history lists, the all-users summary, and the tag breakdown.
|
||||
/// </remarks>
|
||||
internal sealed class ConsumptionStatisticsRepository : IConsumptionStatisticsRepository
|
||||
{
|
||||
private readonly LuminaDbContext _luminaDbContext;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConsumptionStatisticsRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
|
||||
public ConsumptionStatisticsRepository(LuminaDbContext luminaDbContext)
|
||||
{
|
||||
_luminaDbContext = luminaDbContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the consumption records of the user identified by <paramref name="userId"/>, optionally limited to a media library, a media kind, and a date range.
|
||||
/// </summary>
|
||||
/// <param name="userId">The Id of the user whose consumption records are retrieved.</param>
|
||||
/// <param name="libraryId">The Id of the media library to restrict the records to, or <see langword="null"/> for every library.</param>
|
||||
/// <param name="mediaKind">The kind of media to restrict the records to, or <see langword="null"/> for every kind.</param>
|
||||
/// <param name="fromUtc">The start of the date range, or <see langword="null"/> when the range has no start.</param>
|
||||
/// <param name="toUtc">The end of the date range, or <see langword="null"/> when the range has no end.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the consumption records of the user, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyList<MediaConsumptionRecordEntity>>> GetForUserAsync(
|
||||
Guid userId, Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc, CancellationToken cancellationToken)
|
||||
{
|
||||
// The user is part of the query rather than a check performed after the read, so the storage medium never returns a record of another user.
|
||||
IQueryable<MediaConsumptionRecordEntity> query = _luminaDbContext.MediaConsumptionRecords
|
||||
.AsNoTracking()
|
||||
.Where(record => record.UserId == userId);
|
||||
query = ApplyFilters(query, libraryId, mediaKind, fromUtc, toUtc);
|
||||
List<MediaConsumptionRecordEntity> records = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return Result.From<IReadOnlyList<MediaConsumptionRecordEntity>>(records);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the consumption records of every user, optionally limited to a media library, a media kind, and a date range.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library to restrict the records to, or <see langword="null"/> for every library.</param>
|
||||
/// <param name="mediaKind">The kind of media to restrict the records to, or <see langword="null"/> for every kind.</param>
|
||||
/// <param name="fromUtc">The start of the date range, or <see langword="null"/> when the range has no start.</param>
|
||||
/// <param name="toUtc">The end of the date range, or <see langword="null"/> when the range has no end.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the consumption records of every user, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyList<MediaConsumptionRecordEntity>>> GetForAllUsersAsync(
|
||||
Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc, CancellationToken cancellationToken)
|
||||
{
|
||||
// This is the only query that is not scoped to one user, and it exists only for the explicit admin all-users summary.
|
||||
IQueryable<MediaConsumptionRecordEntity> query = _luminaDbContext.MediaConsumptionRecords.AsNoTracking();
|
||||
query = ApplyFilters(query, libraryId, mediaKind, fromUtc, toUtc);
|
||||
List<MediaConsumptionRecordEntity> records = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return Result.From<IReadOnlyList<MediaConsumptionRecordEntity>>(records);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the optional library, media kind, and date range filters to <paramref name="query"/>.
|
||||
/// </summary>
|
||||
/// <param name="query">The query to filter.</param>
|
||||
/// <param name="libraryId">The Id of the media library to restrict the records to, or <see langword="null"/> for every library.</param>
|
||||
/// <param name="mediaKind">The kind of media to restrict the records to, or <see langword="null"/> for every kind.</param>
|
||||
/// <param name="fromUtc">The start of the date range, or <see langword="null"/> when the range has no start.</param>
|
||||
/// <param name="toUtc">The end of the date range, or <see langword="null"/> when the range has no end.</param>
|
||||
/// <returns>The filtered query.</returns>
|
||||
private static IQueryable<MediaConsumptionRecordEntity> ApplyFilters(
|
||||
IQueryable<MediaConsumptionRecordEntity> query, Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc)
|
||||
{
|
||||
// Every filter is bound in the query, so the database never materializes rows that the caller did not ask for.
|
||||
if (libraryId.HasValue)
|
||||
query = query.Where(record => record.LibraryId == libraryId.Value);
|
||||
if (mediaKind.HasValue)
|
||||
query = query.Where(record => record.MediaKind == mediaKind.Value);
|
||||
// The date range is applied to the start of the session, which is the indexed timestamp the statistics of a user are read by.
|
||||
if (fromUtc.HasValue)
|
||||
query = query.Where(record => record.StartedOnUtc >= fromUtc.Value);
|
||||
if (toUtc.HasValue)
|
||||
query = query.Where(record => record.StartedOnUtc <= toUtc.Value);
|
||||
return query;
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -106,9 +106,16 @@ internal sealed class MediaContributorRepository : IMediaContributorRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the retrieved <see cref="MediaContributorEntity"/>s, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyList<MediaContributorEntity>>> GetByIdsAsync(IReadOnlyCollection<Guid> ids, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _luminaDbContext.MediaContributors
|
||||
.Where(contributor => ids.Contains(contributor.Id))
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
List<MediaContributorEntity> contributors = [];
|
||||
// The lookup is split into bounded chunks, so an unpaginated history never exceeds the storage parameter limit.
|
||||
const int LOOKUP_CHUNK_SIZE = 500;
|
||||
foreach (Guid[] chunk in ids.Chunk(LOOKUP_CHUNK_SIZE))
|
||||
{
|
||||
contributors.AddRange(await _luminaDbContext.MediaContributors
|
||||
.Where(contributor => chunk.Contains(contributor.Id))
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false));
|
||||
}
|
||||
return Result.From<IReadOnlyList<MediaContributorEntity>>(contributors);
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,30 @@ internal sealed class MusicArtworkRepository : IMusicArtworkRepository
|
||||
return Result.From(artwork);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pieces of artwork owned by the music library items of the provided <paramref name="ownerType"/> identified by any of <paramref name="ownerIds"/>.
|
||||
/// </summary>
|
||||
/// <param name="ownerType">The type of the music library items that own the artwork.</param>
|
||||
/// <param name="ownerIds">The Ids of the music library items that own 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 the artwork of the existing owners, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyList<MusicArtworkEntity>>> GetByOwnersAsync(MusicArtworkOwnerType ownerType, IReadOnlyCollection<Guid> ownerIds, CancellationToken cancellationToken)
|
||||
{
|
||||
List<MusicArtworkEntity> artwork = [];
|
||||
// The lookup is split into bounded chunks, so a history page never exceeds the storage parameter limit.
|
||||
const int LOOKUP_CHUNK_SIZE = 500;
|
||||
foreach (Guid[] chunk in ownerIds.Chunk(LOOKUP_CHUNK_SIZE))
|
||||
{
|
||||
artwork.AddRange(await _luminaDbContext.MusicArtwork
|
||||
.AsNoTracking()
|
||||
.Where(musicArtwork => musicArtwork.OwnerType == ownerType && chunk.Contains(musicArtwork.OwnerId))
|
||||
.OrderBy(musicArtwork => musicArtwork.ArtworkType)
|
||||
.ThenBy(musicArtwork => musicArtwork.Ordinal)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
return Result.From<IReadOnlyList<MusicArtworkEntity>>(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>
|
||||
|
||||
@@ -559,6 +559,39 @@ internal sealed class TrackRepository : ITrackRepository
|
||||
return Result.From(new TrackAccessSummaryDto(trackCount, [.. libraryIds]));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tracks identified by <paramref name="trackIds"/>, optionally together with their navigation properties.
|
||||
/// </summary>
|
||||
/// <param name="trackIds">The Ids of the tracks to get.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the tracks should be loaded together with the tracks themselves.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the existing tracks, or an error.</returns>
|
||||
public async Task<Result<IReadOnlyList<TrackEntity>>> GetByIdsAsync(IReadOnlyCollection<Guid> trackIds, bool shouldIncludeNavigationProperties, CancellationToken cancellationToken)
|
||||
{
|
||||
List<TrackEntity> tracks = [];
|
||||
// The lookup is split into bounded chunks, so even an unpaginated history never exceeds the storage parameter limit.
|
||||
const int LOOKUP_CHUNK_SIZE = 500;
|
||||
foreach (Guid[] chunk in trackIds.Chunk(LOOKUP_CHUNK_SIZE))
|
||||
{
|
||||
IQueryable<TrackEntity> query = _luminaDbContext.Tracks.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(track => track.Album)
|
||||
.ThenInclude(album => album!.Artist)
|
||||
.Include(track => track.Ratings)
|
||||
.Include(track => track.Tags)
|
||||
.Include(track => track.Genres)
|
||||
.Include(track => track.Moods)
|
||||
.Include(track => track.Isrcs)
|
||||
.Include(track => track.Contributors)
|
||||
.AsSplitQuery();
|
||||
}
|
||||
tracks.AddRange(await query.Where(track => chunk.Contains(track.Id)).ToListAsync(cancellationToken).ConfigureAwait(false));
|
||||
}
|
||||
return Result.From<IReadOnlyList<TrackEntity>>(tracks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The number of requested tracks that exist in one library.
|
||||
/// </summary>
|
||||
|
||||
@@ -75,6 +75,7 @@ public class UnitOfWork : IUnitOfWork, IDisposable
|
||||
private IFavoriteRepository? _favoriteRepository;
|
||||
private IMediaConsumptionRecordRepository? _mediaConsumptionRecordRepository;
|
||||
private IMediaResumePointRepository? _mediaResumePointRepository;
|
||||
private IConsumptionStatisticsRepository? _consumptionStatisticsRepository;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the permission repository.
|
||||
@@ -445,6 +446,18 @@ public class UnitOfWork : IUnitOfWork, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the repository for the consumption records used to build consumption statistics.
|
||||
/// </summary>
|
||||
public IConsumptionStatisticsRepository ConsumptionStatisticsRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
_consumptionStatisticsRepository ??= new ConsumptionStatisticsRepository(_luminaDbContext);
|
||||
return _consumptionStatisticsRepository;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UnitOfWork"/> class.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user