mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Adds the media resume point aggregate
This commit is contained in:
1 parent
fa8513cc71
commit
9247d9fe0f
3 files changed
+288
No files matched your search
@@ -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));
|
||||
|
||||
+236
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Aggregate root for the point at which a user can resume consuming a media item.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("Id: {Id.Value}; MediaKind: {MediaKind}; MediaItemId: {MediaItemId.Value}")]
|
||||
public sealed class MediaResumePoint : AggregateRoot<MediaResumePointId>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the object representing the unique identifier of the user that owns the resume point.
|
||||
/// </summary>
|
||||
public UserId UserId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Id of the media library that contains the media item.
|
||||
/// </summary>
|
||||
public LibraryId LibraryId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Id of the media item to resume.
|
||||
/// </summary>
|
||||
public MediaItemId MediaItemId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the kind of media that is resumed.
|
||||
/// </summary>
|
||||
public MediaKind MediaKind { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position at which the media item can be resumed.
|
||||
/// </summary>
|
||||
public MediaPosition LastPosition { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the progress fraction at which the media item can be resumed.
|
||||
/// </summary>
|
||||
public double LastProgressFraction { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the date and time when the media item was last consumed.
|
||||
/// </summary>
|
||||
public DateTime LastConsumedOnUtc { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaResumePoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="id">The object representing the unique identifier of the resume point.</param>
|
||||
/// <param name="userId">The object representing the unique identifier of the user that owns the resume point.</param>
|
||||
/// <param name="libraryId">The Id of the media library that contains the media item.</param>
|
||||
/// <param name="mediaItemId">The Id of the media item to resume.</param>
|
||||
/// <param name="mediaKind">The kind of media that is resumed.</param>
|
||||
/// <param name="lastPosition">The position at which the media item can be resumed.</param>
|
||||
/// <param name="lastProgressFraction">The progress fraction at which the media item can be resumed.</param>
|
||||
/// <param name="lastConsumedOnUtc">The date and time when the media item was last consumed.</param>
|
||||
/// <param name="createdOnUtc">The date and time when the resume point was created.</param>
|
||||
/// <param name="updatedOnUtc">The date and time when the resume point was last updated, if applicable.</param>
|
||||
private MediaResumePoint(
|
||||
MediaResumePointId id,
|
||||
UserId userId,
|
||||
LibraryId libraryId,
|
||||
MediaItemId mediaItemId,
|
||||
MediaKind mediaKind,
|
||||
MediaPosition lastPosition,
|
||||
double lastProgressFraction,
|
||||
DateTime lastConsumedOnUtc,
|
||||
DateTime createdOnUtc,
|
||||
Optional<DateTime> updatedOnUtc) : base(id)
|
||||
{
|
||||
Id = id;
|
||||
UserId = userId;
|
||||
LibraryId = libraryId;
|
||||
MediaItemId = mediaItemId;
|
||||
MediaKind = mediaKind;
|
||||
LastPosition = lastPosition;
|
||||
LastProgressFraction = lastProgressFraction;
|
||||
LastConsumedOnUtc = lastConsumedOnUtc;
|
||||
CreatedOnUtc = createdOnUtc;
|
||||
UpdatedOnUtc = updatedOnUtc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new resume point for a media item.
|
||||
/// </summary>
|
||||
/// <param name="userId">The object representing the unique identifier of the user that owns the resume point.</param>
|
||||
/// <param name="libraryId">The Id of the media library that contains the media item.</param>
|
||||
/// <param name="mediaItemId">The Id of the media item to resume.</param>
|
||||
/// <param name="mediaKind">The kind of media that is resumed.</param>
|
||||
/// <param name="lastPosition">The position at which the media item can be resumed.</param>
|
||||
/// <param name="lastProgressFraction">The progress fraction at which the media item can be resumed.</param>
|
||||
/// <param name="lastConsumedOnUtc">The date and time when the media item was last consumed.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully created <see cref="MediaResumePoint"/>, or an error message.
|
||||
/// </returns>
|
||||
public static Result<MediaResumePoint> Create(
|
||||
UserId userId,
|
||||
LibraryId libraryId,
|
||||
MediaItemId mediaItemId,
|
||||
MediaKind mediaKind,
|
||||
MediaPosition lastPosition,
|
||||
double lastProgressFraction,
|
||||
DateTime lastConsumedOnUtc)
|
||||
{
|
||||
Optional<MediaConsumptionRule> 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<DateTime>.None());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="MediaResumePoint"/> class, with pre-existing data.
|
||||
/// </summary>
|
||||
/// <param name="id">The object representing the unique identifier of the resume point.</param>
|
||||
/// <param name="userId">The object representing the unique identifier of the user that owns the resume point.</param>
|
||||
/// <param name="libraryId">The Id of the media library that contains the media item.</param>
|
||||
/// <param name="mediaItemId">The Id of the media item to resume.</param>
|
||||
/// <param name="mediaKind">The kind of media that is resumed.</param>
|
||||
/// <param name="lastPosition">The position at which the media item can be resumed.</param>
|
||||
/// <param name="lastProgressFraction">The progress fraction at which the media item can be resumed.</param>
|
||||
/// <param name="lastConsumedOnUtc">The date and time when the media item was last consumed.</param>
|
||||
/// <param name="createdOnUtc">The date and time when the resume point was created.</param>
|
||||
/// <param name="updatedOnUtc">The date and time when the resume point was last updated, if applicable.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully reconstituted <see cref="MediaResumePoint"/>, or an error message.
|
||||
/// </returns>
|
||||
public static Result<MediaResumePoint> Reconstitute(
|
||||
MediaResumePointId id,
|
||||
UserId userId,
|
||||
LibraryId libraryId,
|
||||
MediaItemId mediaItemId,
|
||||
MediaKind mediaKind,
|
||||
MediaPosition lastPosition,
|
||||
double lastProgressFraction,
|
||||
DateTime lastConsumedOnUtc,
|
||||
DateTime createdOnUtc,
|
||||
Optional<DateTime> updatedOnUtc)
|
||||
{
|
||||
Optional<MediaConsumptionRule> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the resume point to a new position and progress fraction.
|
||||
/// </summary>
|
||||
/// <param name="position">The position at which the media item can be resumed.</param>
|
||||
/// <param name="progressFraction">The progress fraction at which the media item can be resumed.</param>
|
||||
/// <param name="lastConsumedOnUtc">The date and time when the media item was last consumed.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful update, or an error.</returns>
|
||||
public Result<Updated> Advance(MediaPosition position, double progressFraction, DateTime lastConsumedOnUtc)
|
||||
{
|
||||
Optional<MediaConsumptionRule> 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<DateTime>.Some(lastConsumedOnUtc);
|
||||
return Result.Updated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the progress fraction is a finite value between zero and one.
|
||||
/// </summary>
|
||||
/// <param name="progressFraction">The progress fraction to check.</param>
|
||||
/// <returns><see langword="true"/> if the progress fraction is valid, <see langword="false"/> otherwise.</returns>
|
||||
private static bool IsProgressFractionValid(double progressFraction)
|
||||
{
|
||||
return double.IsFinite(progressFraction) && progressFraction >= 0 && progressFraction <= 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the position uses the position representation of the consumption rule of the media kind.
|
||||
/// </summary>
|
||||
/// <param name="rule">The consumption rule of the media kind.</param>
|
||||
/// <param name="position">The position to check.</param>
|
||||
/// <returns><see langword="true"/> if the position is valid for the media kind, <see langword="false"/> otherwise.</returns>
|
||||
private static bool DoesPositionMatchRule(MediaConsumptionRule rule, MediaPosition position)
|
||||
{
|
||||
return rule.PositionKind.HasValue && position.Kind == rule.PositionKind.Value;
|
||||
}
|
||||
}
|
||||
+51
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Value Object for the Id of a media resume point.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{Value}")]
|
||||
public sealed class MediaResumePointId : EntityId<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaResumePointId"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The value representing this object.</param>
|
||||
private MediaResumePointId(Guid value) : base(value)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="MediaResumePointId"/> class.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="MediaResumePointId"/> instance.</returns>
|
||||
public static MediaResumePointId CreateUnique()
|
||||
{
|
||||
return new MediaResumePointId(Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="MediaResumePointId"/> class, from a pre-existing <paramref name="value"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value used to create the <see cref="MediaResumePointId"/> instance.</param>
|
||||
/// <returns>The created <see cref="MediaResumePointId"/> instance.</returns>
|
||||
public static MediaResumePointId Create(Guid value)
|
||||
{
|
||||
return new MediaResumePointId(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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user