Adds favorites and aligns playlist and favorite pagination

This commit is contained in:
BinaryAlley committed 2026-10-22 20:27:07 +03:00
1 parent 2e9f7d8496
commit 525ac1348d
193 files changed
+10944 -643

No files matched your search

@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
#endregion
namespace Lumina.Domain.Common.Errors;
/// <summary>
/// Favorite error types.
/// </summary>
public static partial class Errors
{
public static class Favorites
{
public static Error FavoriteNotFound => Error.NotFound(description: nameof(FavoriteNotFound));
public static Error FavoriteAlreadyExists => Error.Conflict(description: nameof(FavoriteAlreadyExists));
public static Error FavoriteMediaKindIsInvalid => Error.Validation(description: nameof(FavoriteMediaKindIsInvalid));
public static Error FavoriteMediaKindIsNotSupported => Error.Validation(description: nameof(FavoriteMediaKindIsNotSupported));
public static Error FavoriteMediaItemIdCannotBeEmpty => Error.Validation(description: nameof(FavoriteMediaItemIdCannotBeEmpty));
public static Error FavoriteMediaItemIdsCannotBeEmpty => Error.Validation(description: nameof(FavoriteMediaItemIdsCannotBeEmpty));
public static Error FavoriteMediaItemIdsExceedMaximumCount => Error.Validation(description: nameof(FavoriteMediaItemIdsExceedMaximumCount));
}
}
@@ -23,7 +23,5 @@ public static partial class Errors
public static Error PlaylistEntryNotFound => Error.NotFound(description: nameof(PlaylistEntryNotFound));
public static Error PlaylistEntryPositionsAreInvalid => Error.Validation(description: nameof(PlaylistEntryPositionsAreInvalid));
public static Error PlaylistMoveTargetPositionIsInvalid => Error.Validation(description: nameof(PlaylistMoveTargetPositionIsInvalid));
public static Error PlaylistEntryWindowOffsetIsInvalid => Error.Validation(description: nameof(PlaylistEntryWindowOffsetIsInvalid));
public static Error PlaylistEntryWindowLimitIsInvalid => Error.Validation(description: nameof(PlaylistEntryWindowLimitIsInvalid));
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Models.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
/// <summary>
/// Value Object for the Id of a media item, declared by the favorite bounded context to keep the reference to the catalogue decoupled.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class MediaItemId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="MediaItemId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private MediaItemId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="MediaItemId"/> class.
/// </summary>
/// <returns>The created <see cref="MediaItemId"/> instance.</returns>
public static MediaItemId CreateUnique()
{
return new MediaItemId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="MediaItemId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="MediaItemId"/> instance.</param>
/// <returns>The created <see cref="MediaItemId"/> instance.</returns>
public static MediaItemId Create(Guid value)
{
return new MediaItemId(value);
}
/// <summary>
/// Gets the list of items that define equality of the object.
/// </summary>
/// <returns>A list of items defining the equality.</returns>
public override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Models.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
/// <summary>
/// Value Object for the Id of a user, duplicated from the UserManagement bounded context to keep the cross bounded context references decoupled.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class UserId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="UserId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private UserId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="UserId"/> class.
/// </summary>
/// <returns>The created <see cref="UserId"/> instance.</returns>
public static UserId CreateUnique()
{
return new UserId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="UserId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="UserId"/> instance.</param>
/// <returns>The created <see cref="UserId"/> instance.</returns>
public static UserId Create(Guid value)
{
return new UserId(value);
}
/// <summary>
/// Gets the list of items that define equality of the object.
/// </summary>
/// <returns>A list of items defining the equality.</returns>
public override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
}
@@ -0,0 +1,30 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.Events;
/// <summary>
/// Domain event raised when a media item is marked as a favorite.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="FavoriteId">The unique identifier of the favorite.</param>
/// <param name="UserId">The unique identifier of the user that favorited the media item.</param>
/// <param name="MediaItemId">The unique identifier of the favorited media item.</param>
/// <param name="MediaKind">The kind of the favorited media item.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("FavoriteId: {FavoriteId.Value}; UserId: {UserId.Value}; MediaKind: {MediaKind}; MediaItemId: {MediaItemId.Value}")]
public record FavoriteAddedDomainEvent(
Guid Id,
FavoriteId FavoriteId,
UserId UserId,
MediaItemId MediaItemId,
FavoriteMediaKind MediaKind,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -0,0 +1,30 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.Events;
/// <summary>
/// Domain event raised when a media item is removed from the favorites.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="FavoriteId">The unique identifier of the favorite.</param>
/// <param name="UserId">The unique identifier of the user that owned the favorite.</param>
/// <param name="MediaItemId">The unique identifier of the media item that was unfavorited.</param>
/// <param name="MediaKind">The kind of the media item that was unfavorited.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("FavoriteId: {FavoriteId.Value}; UserId: {UserId.Value}; MediaKind: {MediaKind}; MediaItemId: {MediaItemId.Value}")]
public record FavoriteRemovedDomainEvent(
Guid Id,
FavoriteId FavoriteId,
UserId UserId,
MediaItemId MediaItemId,
FavoriteMediaKind MediaKind,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -0,0 +1,163 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Models.Core;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.Events;
using Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.Favorites;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate;
/// <summary>
/// Aggregate root for a favorite, the marker a user places on a media item.
/// </summary>
[DebuggerDisplay("Id: {Id.Value}; MediaKind: {MediaKind}; MediaItemId: {MediaItemId.Value}")]
public sealed class Favorite : AggregateRoot<FavoriteId>
{
/// <summary>
/// Gets the object representing the unique identifier of the user that owns the favorite.
/// </summary>
public UserId UserId { get; private set; }
/// <summary>
/// Gets the Id of the favorited media item.
/// </summary>
public MediaItemId MediaItemId { get; private set; }
/// <summary>
/// Gets the kind of the favorited media item.
/// </summary>
public FavoriteMediaKind MediaKind { get; private set; }
/// <summary>
/// Gets a value indicating whether the favorite was marked for removal in this unit of work.
/// </summary>
public bool IsRemoved { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="Favorite"/> class.
/// </summary>
/// <param name="id">The object representing the unique identifier of the favorite.</param>
/// <param name="userId">The object representing the unique identifier of the user that owns the favorite.</param>
/// <param name="mediaItemId">The Id of the favorited media item.</param>
/// <param name="mediaKind">The kind of the favorited media item.</param>
/// <param name="createdOnUtc">The date and time when the favorite was created.</param>
/// <param name="updatedOnUtc">The date and time when the favorite was last updated.</param>
private Favorite(
FavoriteId id,
UserId userId,
MediaItemId mediaItemId,
FavoriteMediaKind mediaKind,
DateTime createdOnUtc,
Optional<DateTime> updatedOnUtc) : base(id)
{
Id = id;
UserId = userId;
MediaItemId = mediaItemId;
MediaKind = mediaKind;
CreatedOnUtc = createdOnUtc;
UpdatedOnUtc = updatedOnUtc;
}
/// <summary>
/// Creates a new favorite of the media item identified by <paramref name="mediaItemId"/> for the user identified by <paramref name="userId"/>.
/// </summary>
/// <param name="userId">The object representing the unique identifier of the user that owns the favorite.</param>
/// <param name="mediaItemId">The Id of the media item to favorite.</param>
/// <param name="mediaKind">The kind of the media item to favorite.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a successfully created <see cref="Favorite"/>, or an error message.
/// </returns>
public static Result<Favorite> Create(UserId userId, MediaItemId mediaItemId, FavoriteMediaKind mediaKind)
{
// A planned media kind is part of the model so the storage and the contracts are ready for it, but its items cannot be favorited until its library exists.
if (!IsSupported(mediaKind))
return Errors.Favorites.FavoriteMediaKindIsNotSupported;
DateTime occurredOnUtc = DateTime.UtcNow;
Favorite favorite = new(
FavoriteId.CreateUnique(),
userId,
mediaItemId,
mediaKind,
occurredOnUtc,
Optional<DateTime>.None());
favorite._domainEvents.Add(new FavoriteAddedDomainEvent(
Guid.NewGuid(),
favorite.Id,
userId,
mediaItemId,
mediaKind,
occurredOnUtc));
return favorite;
}
/// <summary>
/// Creates a new instance of the <see cref="Favorite"/> class, with pre-existing data.
/// </summary>
/// <param name="id">The object representing the unique identifier of the favorite.</param>
/// <param name="userId">The object representing the unique identifier of the user that owns the favorite.</param>
/// <param name="mediaItemId">The Id of the favorited media item.</param>
/// <param name="mediaKind">The kind of the favorited media item.</param>
/// <param name="createdOnUtc">The date and time when the favorite was created.</param>
/// <param name="updatedOnUtc">The date and time when the favorite was last updated.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a successfully created <see cref="Favorite"/>, or an error message.
/// </returns>
public static Result<Favorite> Create(
FavoriteId id,
UserId userId,
MediaItemId mediaItemId,
FavoriteMediaKind mediaKind,
DateTime createdOnUtc,
Optional<DateTime> updatedOnUtc)
{
// A stored favorite is rehydrated as it was stored, whether or not its media kind is supported today.
return new Favorite(id, userId, mediaItemId, mediaKind, createdOnUtc, updatedOnUtc);
}
/// <summary>
/// Marks the favorite as removed, raising the domain event that announces its removal.
/// </summary>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public Result<Deleted> Remove()
{
if (IsRemoved)
return Result.Deleted;
// Earlier events become irrelevant once the favorite is removed.
_domainEvents.Clear();
IsRemoved = true;
_domainEvents.Add(new FavoriteRemovedDomainEvent(
Guid.NewGuid(),
Id,
UserId,
MediaItemId,
MediaKind,
DateTime.UtcNow));
return Result.Deleted;
}
/// <summary>
/// Determines whether media items of <paramref name="mediaKind"/> can be favorited today.
/// </summary>
/// <param name="mediaKind">The kind of media item.</param>
/// <returns><see langword="true"/> if items of the media kind can be favorited, <see langword="false"/> otherwise.</returns>
public static bool IsSupported(FavoriteMediaKind mediaKind)
{
return mediaKind switch
{
FavoriteMediaKind.MusicArtist => true,
FavoriteMediaKind.MusicAlbum => true,
FavoriteMediaKind.MusicTrack => true,
FavoriteMediaKind.Book => true,
_ => false
};
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Models.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.FavoriteBoundedContext.FavoriteAggregate.ValueObjects;
/// <summary>
/// Value Object for the Id of a favorite.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class FavoriteId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="FavoriteId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private FavoriteId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FavoriteId"/> class.
/// </summary>
/// <returns>The created <see cref="FavoriteId"/> instance.</returns>
public static FavoriteId CreateUnique()
{
return new FavoriteId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="FavoriteId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="FavoriteId"/> instance.</param>
/// <returns>The created <see cref="FavoriteId"/> instance.</returns>
public static FavoriteId Create(Guid value)
{
return new FavoriteId(value);
}
/// <summary>
/// Gets the list of items that define equality of the object.
/// </summary>
/// <returns>A list of items defining the equality.</returns>
public override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
}