Adds music play history domain aggregate

This commit is contained in:
BinaryAlley committed 2026-10-19 19:33:57 +03:00
1 parent 067e9a21d0
commit 9919965a73
14 files changed
+828

No files matched your search

@@ -0,0 +1,22 @@
namespace Lumina.Domain.SharedKernel.Common.Enums.Playback;
/// <summary>
/// Enumeration for the states of a recorded playback.
/// </summary>
public enum PlaybackRecordStatus
{
/// <summary>
/// The playback was recorded as started and has not ended yet.
/// </summary>
InProgress,
/// <summary>
/// The playback reached the end of the media item.
/// </summary>
Completed,
/// <summary>
/// The playback ended before the media item completed.
/// </summary>
Stopped,
}
@@ -0,0 +1,19 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
#endregion
namespace Lumina.Domain.Common.Errors;
/// <summary>
/// Music play history error types.
/// </summary>
public static partial class Errors
{
public static class MusicPlayHistory
{
public static Error PlayIsNotInProgress => Error.Conflict(description: nameof(PlayIsNotInProgress));
public static Error ListenedDurationCannotDecrease => Error.Validation(description: nameof(ListenedDurationCannotDecrease));
public static Error InProgressPlayCannotHaveEndTime => Error.Validation(description: nameof(InProgressPlayCannotHaveEndTime));
public static Error EndedPlayMustHaveEndTime => Error.Validation(description: nameof(EndedPlayMustHaveEndTime));
}
}
@@ -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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
/// <summary>
/// Value Object for the Id of an album, duplicated from the AudioLibrary bounded context to keep the cross bounded context references decoupled.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class AlbumId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="AlbumId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private AlbumId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="AlbumId"/> class.
/// </summary>
/// <returns>The created <see cref="AlbumId"/> instance.</returns>
public static AlbumId CreateUnique()
{
return new AlbumId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="AlbumId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="AlbumId"/> instance.</param>
/// <returns>The created <see cref="AlbumId"/> instance.</returns>
public static AlbumId Create(Guid value)
{
return new AlbumId(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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
/// <summary>
/// Value Object for the Id of an artist, duplicated from the AudioLibrary bounded context to keep the cross bounded context references decoupled.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class ArtistId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="ArtistId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private ArtistId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="ArtistId"/> class.
/// </summary>
/// <returns>The created <see cref="ArtistId"/> instance.</returns>
public static ArtistId CreateUnique()
{
return new ArtistId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="ArtistId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="ArtistId"/> instance.</param>
/// <returns>The created <see cref="ArtistId"/> instance.</returns>
public static ArtistId Create(Guid value)
{
return new ArtistId(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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
/// <summary>
/// Value Object for the Id of a track, duplicated from the AudioLibrary bounded context to keep the cross bounded context references decoupled.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class TrackId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="TrackId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private TrackId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="TrackId"/> class.
/// </summary>
/// <returns>The created <see cref="TrackId"/> instance.</returns>
public static TrackId CreateUnique()
{
return new TrackId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="TrackId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="TrackId"/> instance.</param>
/// <returns>The created <see cref="TrackId"/> instance.</returns>
public static TrackId Create(Guid value)
{
return new TrackId(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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
/// <summary>
/// Value Object for the Id of a media library, duplicated from the LibraryManagement bounded context to keep the cross bounded context references decoupled.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class LibraryId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="LibraryId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private LibraryId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="LibraryId"/> class.
/// </summary>
/// <returns>The created <see cref="LibraryId"/> instance.</returns>
public static LibraryId CreateUnique()
{
return new LibraryId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="LibraryId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="LibraryId"/> instance.</param>
/// <returns>The created <see cref="LibraryId"/> instance.</returns>
public static LibraryId Create(Guid value)
{
return new LibraryId(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.MusicPlayHistoryBoundedContext.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,31 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.Events;
/// <summary>
/// Domain event raised when a music play completes.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="MusicPlayRecordId">The unique identifier of the play record.</param>
/// <param name="UserId">The unique identifier of the user that owns the play record.</param>
/// <param name="TrackId">The unique identifier of the track that completed.</param>
/// <param name="Position">The last position reported by the player.</param>
/// <param name="ListenedDuration">The duration that the user listened to.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("MusicPlayRecordId: {MusicPlayRecordId.Value}; TrackId: {TrackId.Value}")]
public record MusicPlayCompletedDomainEvent(
Guid Id,
MusicPlayRecordId MusicPlayRecordId,
UserId UserId,
TrackId TrackId,
MusicPlayPosition Position,
TimeSpan ListenedDuration,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -0,0 +1,31 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.Events;
/// <summary>
/// Domain event raised when a player reports the progress of a music play.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="MusicPlayRecordId">The unique identifier of the play record.</param>
/// <param name="UserId">The unique identifier of the user that owns the play record.</param>
/// <param name="TrackId">The unique identifier of the track that is being played.</param>
/// <param name="Position">The last position reported by the player.</param>
/// <param name="ListenedDuration">The duration that the user has listened to so far.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("MusicPlayRecordId: {MusicPlayRecordId.Value}; Position: {Position.Offset}")]
public record MusicPlayProgressedDomainEvent(
Guid Id,
MusicPlayRecordId MusicPlayRecordId,
UserId UserId,
TrackId TrackId,
MusicPlayPosition Position,
TimeSpan ListenedDuration,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.Events;
/// <summary>
/// Domain event raised when a music play starts.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="MusicPlayRecordId">The unique identifier of the play record.</param>
/// <param name="UserId">The unique identifier of the user that started the play.</param>
/// <param name="TrackId">The unique identifier of the track that was played.</param>
/// <param name="LibraryId">The unique identifier of the media library that contains the track.</param>
/// <param name="AlbumId">The unique identifier of the album that contains the track.</param>
/// <param name="ArtistId">The unique identifier of the artist that produced the track.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("MusicPlayRecordId: {MusicPlayRecordId.Value}; TrackId: {TrackId.Value}")]
public record MusicPlayStartedDomainEvent(
Guid Id,
MusicPlayRecordId MusicPlayRecordId,
UserId UserId,
TrackId TrackId,
LibraryId LibraryId,
AlbumId AlbumId,
ArtistId ArtistId,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -0,0 +1,31 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Events;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.Events;
/// <summary>
/// Domain event raised when a music play stops before the track completes.
/// </summary>
/// <param name="Id">The unique identifier of the domain event.</param>
/// <param name="MusicPlayRecordId">The unique identifier of the play record.</param>
/// <param name="UserId">The unique identifier of the user that owns the play record.</param>
/// <param name="TrackId">The unique identifier of the track that was stopped.</param>
/// <param name="Position">The last position reported by the player.</param>
/// <param name="ListenedDuration">The duration that the user listened to.</param>
/// <param name="OccurredOnUtc">The date and time when the domain event occurred.</param>
[DebuggerDisplay("MusicPlayRecordId: {MusicPlayRecordId.Value}; TrackId: {TrackId.Value}")]
public record MusicPlayStoppedDomainEvent(
Guid Id,
MusicPlayRecordId MusicPlayRecordId,
UserId UserId,
TrackId TrackId,
MusicPlayPosition Position,
TimeSpan ListenedDuration,
DateTime OccurredOnUtc
) : IDomainEvent;
@@ -0,0 +1,303 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Models.Core;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.Events;
using Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.Playback;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate;
/// <summary>
/// Aggregate root for a recorded music play.
/// </summary>
/// <remarks>
/// A record is created when a player starts a track and is updated by the progress, completion and stop reports of that player. It logs what was
/// played, how far it reached and how long the user listened, so statistics and recommendations can be built from it. The record is created when the
/// play starts, so <see cref="CreatedOnUtc"/> is the start time.
/// </remarks>
[DebuggerDisplay("Id: {Id.Value}; TrackId: {TrackId.Value}; Status: {Status}")]
public sealed class MusicPlayRecord : AggregateRoot<MusicPlayRecordId>
{
/// <summary>
/// Gets the object representing the unique identifier of the user that started the play.
/// </summary>
public UserId UserId { get; private set; }
/// <summary>
/// Gets the Id of the track that was played.
/// </summary>
public TrackId TrackId { get; private set; }
/// <summary>
/// Gets the Id of the media library that contains the track.
/// </summary>
public LibraryId LibraryId { get; private set; }
/// <summary>
/// Gets the Id of the album that contains the track.
/// </summary>
public AlbumId AlbumId { get; private set; }
/// <summary>
/// Gets the Id of the artist that produced the track.
/// </summary>
public ArtistId ArtistId { get; private set; }
/// <summary>
/// Gets the state of the recorded play.
/// </summary>
public PlaybackRecordStatus Status { get; private set; }
/// <summary>
/// Gets the last position reported by the player.
/// </summary>
public MusicPlayPosition LastPosition { get; private set; }
/// <summary>
/// Gets the duration that the user has listened to.
/// </summary>
public TimeSpan ListenedDuration { get; private set; }
/// <summary>
/// Gets the date and time when the play completed or stopped, if it has ended.
/// </summary>
public Optional<DateTime> EndedOnUtc { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="MusicPlayRecord"/> class.
/// </summary>
/// <param name="id">The object representing the unique identifier of the play record.</param>
/// <param name="userId">The object representing the unique identifier of the user that started the play.</param>
/// <param name="trackId">The Id of the track that was played.</param>
/// <param name="libraryId">The Id of the media library that contains the track.</param>
/// <param name="albumId">The Id of the album that contains the track.</param>
/// <param name="artistId">The Id of the artist that produced the track.</param>
/// <param name="status">The state of the recorded play.</param>
/// <param name="lastPosition">The last position reported by the player.</param>
/// <param name="listenedDuration">The duration that the user has listened to.</param>
/// <param name="endedOnUtc">The date and time when the play completed or stopped, if it has ended.</param>
/// <param name="createdOnUtc">The date and time when the play started.</param>
/// <param name="updatedOnUtc">The date and time when the play record was last updated.</param>
private MusicPlayRecord(
MusicPlayRecordId id,
UserId userId,
TrackId trackId,
LibraryId libraryId,
AlbumId albumId,
ArtistId artistId,
PlaybackRecordStatus status,
MusicPlayPosition lastPosition,
TimeSpan listenedDuration,
Optional<DateTime> endedOnUtc,
DateTime createdOnUtc,
Optional<DateTime> updatedOnUtc) : base(id)
{
Id = id;
UserId = userId;
TrackId = trackId;
LibraryId = libraryId;
AlbumId = albumId;
ArtistId = artistId;
Status = status;
LastPosition = lastPosition;
ListenedDuration = listenedDuration;
EndedOnUtc = endedOnUtc;
CreatedOnUtc = createdOnUtc;
UpdatedOnUtc = updatedOnUtc;
}
/// <summary>
/// Creates a new play record for a track that started playing.
/// </summary>
/// <param name="userId">The object representing the unique identifier of the user that started the play.</param>
/// <param name="trackId">The Id of the track that was played.</param>
/// <param name="libraryId">The Id of the media library that contains the track.</param>
/// <param name="albumId">The Id of the album that contains the track.</param>
/// <param name="artistId">The Id of the artist that produced the track.</param>
/// <returns>The created <see cref="MusicPlayRecord"/> instance.</returns>
public static MusicPlayRecord Create(
UserId userId,
TrackId trackId,
LibraryId libraryId,
AlbumId albumId,
ArtistId artistId)
{
DateTime occurredOnUtc = DateTime.UtcNow;
MusicPlayRecord playRecord = new(
MusicPlayRecordId.CreateUnique(),
userId,
trackId,
libraryId,
albumId,
artistId,
PlaybackRecordStatus.InProgress,
MusicPlayPosition.Zero,
TimeSpan.Zero,
Optional<DateTime>.None(),
occurredOnUtc,
Optional<DateTime>.None());
playRecord._domainEvents.Add(new MusicPlayStartedDomainEvent(
Guid.NewGuid(),
playRecord.Id,
userId,
trackId,
libraryId,
albumId,
artistId,
occurredOnUtc));
return playRecord;
}
/// <summary>
/// Creates a new instance of the <see cref="MusicPlayRecord"/> class, with pre-existing data.
/// </summary>
/// <param name="id">The object representing the unique identifier of the play record.</param>
/// <param name="userId">The object representing the unique identifier of the user that started the play.</param>
/// <param name="trackId">The Id of the track that was played.</param>
/// <param name="libraryId">The Id of the media library that contains the track.</param>
/// <param name="albumId">The Id of the album that contains the track.</param>
/// <param name="artistId">The Id of the artist that produced the track.</param>
/// <param name="status">The state of the recorded play.</param>
/// <param name="lastPosition">The last position reported by the player.</param>
/// <param name="listenedDuration">The duration that the user has listened to.</param>
/// <param name="endedOnUtc">The date and time when the play completed or stopped, if it has ended.</param>
/// <param name="createdOnUtc">The date and time when the play started.</param>
/// <param name="updatedOnUtc">The date and time when the play record was last updated.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either a successfully created <see cref="MusicPlayRecord"/>, or an error message.
/// </returns>
public static Result<MusicPlayRecord> Create(
MusicPlayRecordId id,
UserId userId,
TrackId trackId,
LibraryId libraryId,
AlbumId albumId,
ArtistId artistId,
PlaybackRecordStatus status,
MusicPlayPosition lastPosition,
TimeSpan listenedDuration,
Optional<DateTime> endedOnUtc,
DateTime createdOnUtc,
Optional<DateTime> updatedOnUtc)
{
if (status == PlaybackRecordStatus.InProgress && endedOnUtc.HasValue)
return Errors.MusicPlayHistory.InProgressPlayCannotHaveEndTime;
if (status != PlaybackRecordStatus.InProgress && !endedOnUtc.HasValue)
return Errors.MusicPlayHistory.EndedPlayMustHaveEndTime;
return new MusicPlayRecord(
id,
userId,
trackId,
libraryId,
albumId,
artistId,
status,
lastPosition,
listenedDuration,
endedOnUtc,
createdOnUtc,
updatedOnUtc);
}
/// <summary>
/// Updates the last reported position and the listened duration of an in-progress play.
/// </summary>
/// <param name="position">The last position reported by the player.</param>
/// <param name="listenedDuration">The duration that the user has listened to.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful update, or an error.</returns>
public Result<Updated> ReportProgress(MusicPlayPosition position, TimeSpan listenedDuration)
{
if (Status != PlaybackRecordStatus.InProgress)
return Errors.MusicPlayHistory.PlayIsNotInProgress;
if (listenedDuration < ListenedDuration)
return Errors.MusicPlayHistory.ListenedDurationCannotDecrease;
if (LastPosition.Offset == position.Offset && ListenedDuration == listenedDuration)
return Result.Updated;
LastPosition = position;
ListenedDuration = listenedDuration;
DateTime occurredOnUtc = DateTime.UtcNow;
UpdatedOnUtc = Optional<DateTime>.Some(occurredOnUtc);
_domainEvents.Add(new MusicPlayProgressedDomainEvent(
Guid.NewGuid(),
Id,
UserId,
TrackId,
position,
listenedDuration,
occurredOnUtc));
return Result.Updated;
}
/// <summary>
/// Marks an in-progress play as completed.
/// </summary>
/// <param name="position">The last position reported by the player.</param>
/// <param name="listenedDuration">The duration that the user listened to.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful update, or an error.</returns>
public Result<Updated> Complete(MusicPlayPosition position, TimeSpan listenedDuration)
{
if (Status != PlaybackRecordStatus.InProgress)
return Errors.MusicPlayHistory.PlayIsNotInProgress;
if (listenedDuration < ListenedDuration)
return Errors.MusicPlayHistory.ListenedDurationCannotDecrease;
LastPosition = position;
ListenedDuration = listenedDuration;
Status = PlaybackRecordStatus.Completed;
DateTime occurredOnUtc = DateTime.UtcNow;
EndedOnUtc = Optional<DateTime>.Some(occurredOnUtc);
UpdatedOnUtc = Optional<DateTime>.Some(occurredOnUtc);
_domainEvents.Add(new MusicPlayCompletedDomainEvent(
Guid.NewGuid(),
Id,
UserId,
TrackId,
position,
listenedDuration,
occurredOnUtc));
return Result.Updated;
}
/// <summary>
/// Marks an in-progress play as stopped before the track completed.
/// </summary>
/// <param name="position">The last position reported by the player.</param>
/// <param name="listenedDuration">The duration that the user listened to.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful update, or an error.</returns>
public Result<Updated> Stop(MusicPlayPosition position, TimeSpan listenedDuration)
{
if (Status != PlaybackRecordStatus.InProgress)
return Errors.MusicPlayHistory.PlayIsNotInProgress;
if (listenedDuration < ListenedDuration)
return Errors.MusicPlayHistory.ListenedDurationCannotDecrease;
LastPosition = position;
ListenedDuration = listenedDuration;
Status = PlaybackRecordStatus.Stopped;
DateTime occurredOnUtc = DateTime.UtcNow;
EndedOnUtc = Optional<DateTime>.Some(occurredOnUtc);
UpdatedOnUtc = Optional<DateTime>.Some(occurredOnUtc);
_domainEvents.Add(new MusicPlayStoppedDomainEvent(
Guid.NewGuid(),
Id,
UserId,
TrackId,
position,
listenedDuration,
occurredOnUtc));
return Result.Updated;
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Models.Core;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Domain.Core.BoundedContexts.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
/// <summary>
/// Value Object for the last position reported by a player for a music play record.
/// </summary>
public sealed class MusicPlayPosition : ValueObject
{
/// <summary>
/// Gets the position reported by the player.
/// </summary>
public TimeSpan Offset { get; }
/// <summary>
/// Gets the position at the very start of a track.
/// </summary>
public static MusicPlayPosition Zero { get; } = new(TimeSpan.Zero);
/// <summary>
/// Initializes a new instance of the <see cref="MusicPlayPosition"/> class.
/// </summary>
/// <param name="offset">The position reported by the player.</param>
private MusicPlayPosition(TimeSpan offset)
{
Offset = offset;
}
/// <summary>
/// Creates a new instance of the <see cref="MusicPlayPosition"/> class.
/// </summary>
/// <param name="offset">The position reported by the player.</param>
/// <returns>The created <see cref="MusicPlayPosition"/> instance.</returns>
public static MusicPlayPosition Create(TimeSpan offset)
{
return new MusicPlayPosition(offset);
}
/// <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 Offset;
}
}
@@ -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.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects;
/// <summary>
/// Value Object for the Id of a music play record.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class MusicPlayRecordId : EntityId<Guid>
{
/// <summary>
/// Initializes a new instance of the <see cref="MusicPlayRecordId"/> class.
/// </summary>
/// <param name="value">The value representing this object.</param>
private MusicPlayRecordId(Guid value) : base(value)
{
}
/// <summary>
/// Creates a new instance of the <see cref="MusicPlayRecordId"/> class.
/// </summary>
/// <returns>The created <see cref="MusicPlayRecordId"/> instance.</returns>
public static MusicPlayRecordId CreateUnique()
{
return new MusicPlayRecordId(Guid.NewGuid());
}
/// <summary>
/// Creates a new instance of the <see cref="MusicPlayRecordId"/> class, from a pre-existing <paramref name="value"/>.
/// </summary>
/// <param name="value">The value used to create the <see cref="MusicPlayRecordId"/> instance.</param>
/// <returns>The created <see cref="MusicPlayRecordId"/> instance.</returns>
public static MusicPlayRecordId Create(Guid value)
{
return new MusicPlayRecordId(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;
}
}