From 9247d9fe0f03d82278a3282c8764de49e425be54 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sat, 24 Oct 2026 13:00:49 +0300 Subject: [PATCH] Adds the media resume point aggregate --- .../Common/Errors/Errors.MediaConsumption.cs | 1 + .../MediaResumePoint.cs | 236 ++++++++++++++++++ .../ValueObjects/MediaResumePointId.cs | 51 ++++ 3 files changed, 288 insertions(+) create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/MediaResumePoint.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/ValueObjects/MediaResumePointId.cs diff --git a/src/Lumina.Domain/Common/Errors/Errors.MediaConsumption.cs b/src/Lumina.Domain/Common/Errors/Errors.MediaConsumption.cs index e3545816..ed452bce 100644 --- a/src/Lumina.Domain/Common/Errors/Errors.MediaConsumption.cs +++ b/src/Lumina.Domain/Common/Errors/Errors.MediaConsumption.cs @@ -13,6 +13,7 @@ public static partial class Errors { public static Error UnsupportedMediaKind => Error.Validation(description: nameof(UnsupportedMediaKind)); public static Error MediaKindCannotStartCompleted => Error.Forbidden(description: nameof(MediaKindCannotStartCompleted)); + public static Error MediaKindCannotResume => Error.Forbidden(description: nameof(MediaKindCannotResume)); public static Error InvalidMediaPosition => Error.Validation(description: nameof(InvalidMediaPosition)); public static Error InvalidProgressFraction => Error.Validation(description: nameof(InvalidProgressFraction)); public static Error ConsumedDurationMustBeNonNegative => Error.Validation(description: nameof(ConsumedDurationMustBeNonNegative)); diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/MediaResumePoint.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/MediaResumePoint.cs new file mode 100644 index 00000000..e20e564c --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/MediaResumePoint.cs @@ -0,0 +1,236 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Errors; +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaResumePointAggregate.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaResumePointAggregate; + +/// +/// Aggregate root for the point at which a user can resume consuming a media item. +/// +[DebuggerDisplay("Id: {Id.Value}; MediaKind: {MediaKind}; MediaItemId: {MediaItemId.Value}")] +public sealed class MediaResumePoint : AggregateRoot +{ + /// + /// Gets the object representing the unique identifier of the user that owns the resume point. + /// + public UserId UserId { get; private set; } + + /// + /// Gets the Id of the media library that contains the media item. + /// + public LibraryId LibraryId { get; private set; } + + /// + /// Gets the Id of the media item to resume. + /// + public MediaItemId MediaItemId { get; private set; } + + /// + /// Gets the kind of media that is resumed. + /// + public MediaKind MediaKind { get; private set; } + + /// + /// Gets the position at which the media item can be resumed. + /// + public MediaPosition LastPosition { get; private set; } + + /// + /// Gets the progress fraction at which the media item can be resumed. + /// + public double LastProgressFraction { get; private set; } + + /// + /// Gets the date and time when the media item was last consumed. + /// + public DateTime LastConsumedOnUtc { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// The object representing the unique identifier of the resume point. + /// The object representing the unique identifier of the user that owns the resume point. + /// The Id of the media library that contains the media item. + /// The Id of the media item to resume. + /// The kind of media that is resumed. + /// The position at which the media item can be resumed. + /// The progress fraction at which the media item can be resumed. + /// The date and time when the media item was last consumed. + /// The date and time when the resume point was created. + /// The date and time when the resume point was last updated, if applicable. + private MediaResumePoint( + MediaResumePointId id, + UserId userId, + LibraryId libraryId, + MediaItemId mediaItemId, + MediaKind mediaKind, + MediaPosition lastPosition, + double lastProgressFraction, + DateTime lastConsumedOnUtc, + DateTime createdOnUtc, + Optional updatedOnUtc) : base(id) + { + Id = id; + UserId = userId; + LibraryId = libraryId; + MediaItemId = mediaItemId; + MediaKind = mediaKind; + LastPosition = lastPosition; + LastProgressFraction = lastProgressFraction; + LastConsumedOnUtc = lastConsumedOnUtc; + CreatedOnUtc = createdOnUtc; + UpdatedOnUtc = updatedOnUtc; + } + + /// + /// Creates a new resume point for a media item. + /// + /// The object representing the unique identifier of the user that owns the resume point. + /// The Id of the media library that contains the media item. + /// The Id of the media item to resume. + /// The kind of media that is resumed. + /// The position at which the media item can be resumed. + /// The progress fraction at which the media item can be resumed. + /// The date and time when the media item was last consumed. + /// + /// An containing either a successfully created , or an error message. + /// + public static Result Create( + UserId userId, + LibraryId libraryId, + MediaItemId mediaItemId, + MediaKind mediaKind, + MediaPosition lastPosition, + double lastProgressFraction, + DateTime lastConsumedOnUtc) + { + Optional ruleResult = MediaConsumptionRule.For(mediaKind); + if (!ruleResult.HasValue) + return Errors.MediaConsumption.UnsupportedMediaKind; + if (!ruleResult.Value.CanResume) + return Errors.MediaConsumption.MediaKindCannotResume; + if (!DoesPositionMatchRule(ruleResult.Value, lastPosition)) + return Errors.MediaConsumption.InvalidMediaPosition; + if (!IsProgressFractionValid(lastProgressFraction)) + return Errors.MediaConsumption.InvalidProgressFraction; + + // A resume point is created when the media item is first consumed, so the first consumption time is also its creation time. + return new MediaResumePoint( + MediaResumePointId.CreateUnique(), + userId, + libraryId, + mediaItemId, + mediaKind, + lastPosition, + lastProgressFraction, + lastConsumedOnUtc, + lastConsumedOnUtc, + Optional.None()); + } + + /// + /// Creates a new instance of the class, with pre-existing data. + /// + /// The object representing the unique identifier of the resume point. + /// The object representing the unique identifier of the user that owns the resume point. + /// The Id of the media library that contains the media item. + /// The Id of the media item to resume. + /// The kind of media that is resumed. + /// The position at which the media item can be resumed. + /// The progress fraction at which the media item can be resumed. + /// The date and time when the media item was last consumed. + /// The date and time when the resume point was created. + /// The date and time when the resume point was last updated, if applicable. + /// + /// An containing either a successfully reconstituted , or an error message. + /// + public static Result Reconstitute( + MediaResumePointId id, + UserId userId, + LibraryId libraryId, + MediaItemId mediaItemId, + MediaKind mediaKind, + MediaPosition lastPosition, + double lastProgressFraction, + DateTime lastConsumedOnUtc, + DateTime createdOnUtc, + Optional updatedOnUtc) + { + Optional ruleResult = MediaConsumptionRule.For(mediaKind); + if (!ruleResult.HasValue) + return Errors.MediaConsumption.UnsupportedMediaKind; + if (!ruleResult.Value.CanResume) + return Errors.MediaConsumption.MediaKindCannotResume; + if (!DoesPositionMatchRule(ruleResult.Value, lastPosition)) + return Errors.MediaConsumption.InvalidMediaPosition; + if (!IsProgressFractionValid(lastProgressFraction)) + return Errors.MediaConsumption.InvalidProgressFraction; + + return new MediaResumePoint( + id, + userId, + libraryId, + mediaItemId, + mediaKind, + lastPosition, + lastProgressFraction, + lastConsumedOnUtc, + createdOnUtc, + updatedOnUtc); + } + + /// + /// Moves the resume point to a new position and progress fraction. + /// + /// The position at which the media item can be resumed. + /// The progress fraction at which the media item can be resumed. + /// The date and time when the media item was last consumed. + /// An representing either a successful update, or an error. + public Result Advance(MediaPosition position, double progressFraction, DateTime lastConsumedOnUtc) + { + Optional ruleResult = MediaConsumptionRule.For(MediaKind); + if (!ruleResult.HasValue) + return Errors.MediaConsumption.UnsupportedMediaKind; + if (!DoesPositionMatchRule(ruleResult.Value, position)) + return Errors.MediaConsumption.InvalidMediaPosition; + if (!IsProgressFractionValid(progressFraction)) + return Errors.MediaConsumption.InvalidProgressFraction; + + LastPosition = position; + LastProgressFraction = progressFraction; + LastConsumedOnUtc = lastConsumedOnUtc; + UpdatedOnUtc = Optional.Some(lastConsumedOnUtc); + return Result.Updated; + } + + /// + /// Determines whether the progress fraction is a finite value between zero and one. + /// + /// The progress fraction to check. + /// if the progress fraction is valid, otherwise. + private static bool IsProgressFractionValid(double progressFraction) + { + return double.IsFinite(progressFraction) && progressFraction >= 0 && progressFraction <= 1; + } + + /// + /// Determines whether the position uses the position representation of the consumption rule of the media kind. + /// + /// The consumption rule of the media kind. + /// The position to check. + /// if the position is valid for the media kind, otherwise. + private static bool DoesPositionMatchRule(MediaConsumptionRule rule, MediaPosition position) + { + return rule.PositionKind.HasValue && position.Kind == rule.PositionKind.Value; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/ValueObjects/MediaResumePointId.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/ValueObjects/MediaResumePointId.cs new file mode 100644 index 00000000..4e24a555 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaResumePointAggregate/ValueObjects/MediaResumePointId.cs @@ -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.MediaConsumptionBoundedContext.MediaResumePointAggregate.ValueObjects; + +/// +/// Value Object for the Id of a media resume point. +/// +[DebuggerDisplay("{Value}")] +public sealed class MediaResumePointId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private MediaResumePointId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static MediaResumePointId CreateUnique() + { + return new MediaResumePointId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static MediaResumePointId Create(Guid value) + { + return new MediaResumePointId(value); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +}