diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/Playback/PlaybackRecordStatus.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/Playback/PlaybackRecordStatus.cs new file mode 100644 index 00000000..4eb75fa6 --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/Playback/PlaybackRecordStatus.cs @@ -0,0 +1,22 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.Playback; + +/// +/// Enumeration for the states of a recorded playback. +/// +public enum PlaybackRecordStatus +{ + /// + /// The playback was recorded as started and has not ended yet. + /// + InProgress, + + /// + /// The playback reached the end of the media item. + /// + Completed, + + /// + /// The playback ended before the media item completed. + /// + Stopped, +} diff --git a/src/Lumina.Domain/Common/Errors/Errors.MusicPlayHistory.cs b/src/Lumina.Domain/Common/Errors/Errors.MusicPlayHistory.cs new file mode 100644 index 00000000..4253d687 --- /dev/null +++ b/src/Lumina.Domain/Common/Errors/Errors.MusicPlayHistory.cs @@ -0,0 +1,19 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +#endregion + +namespace Lumina.Domain.Common.Errors; + +/// +/// Music play history error types. +/// +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)); + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/AlbumId.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/AlbumId.cs new file mode 100644 index 00000000..e1955782 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/AlbumId.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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate; + +/// +/// Value Object for the Id of an album, duplicated from the AudioLibrary bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class AlbumId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private AlbumId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static AlbumId CreateUnique() + { + return new AlbumId(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 AlbumId Create(Guid value) + { + return new AlbumId(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; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/ArtistId.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/ArtistId.cs new file mode 100644 index 00000000..3b00f250 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/ArtistId.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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate; + +/// +/// Value Object for the Id of an artist, duplicated from the AudioLibrary bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class ArtistId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private ArtistId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static ArtistId CreateUnique() + { + return new ArtistId(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 ArtistId Create(Guid value) + { + return new ArtistId(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; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/TrackId.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/TrackId.cs new file mode 100644 index 00000000..ee9a4f1e --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/AudioLibraryBoundedContext/MusicLibraryAggregate/TrackId.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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.AudioLibraryBoundedContext.MusicLibraryAggregate; + +/// +/// Value Object for the Id of a track, duplicated from the AudioLibrary bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class TrackId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private TrackId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static TrackId CreateUnique() + { + return new TrackId(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 TrackId Create(Guid value) + { + return new TrackId(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; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.cs new file mode 100644 index 00000000..435a3fc4 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate; + +/// +/// Value Object for the Id of a media library, duplicated from the LibraryManagement bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class LibraryId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private LibraryId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static LibraryId CreateUnique() + { + return new LibraryId(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 LibraryId Create(Guid value) + { + return new LibraryId(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; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs new file mode 100644 index 00000000..4339b0f4 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.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.MusicPlayHistoryBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate; + +/// +/// Value Object for the Id of a user, duplicated from the UserManagement bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class UserId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private UserId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static UserId CreateUnique() + { + return new UserId(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 UserId Create(Guid value) + { + return new UserId(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; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayCompletedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayCompletedDomainEvent.cs new file mode 100644 index 00000000..8fa32ceb --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayCompletedDomainEvent.cs @@ -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; + +/// +/// Domain event raised when a music play completes. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the play record. +/// The unique identifier of the user that owns the play record. +/// The unique identifier of the track that completed. +/// The last position reported by the player. +/// The duration that the user listened to. +/// The date and time when the domain event occurred. +[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; diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayProgressedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayProgressedDomainEvent.cs new file mode 100644 index 00000000..070ffb2c --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayProgressedDomainEvent.cs @@ -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; + +/// +/// Domain event raised when a player reports the progress of a music play. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the play record. +/// The unique identifier of the user that owns the play record. +/// The unique identifier of the track that is being played. +/// The last position reported by the player. +/// The duration that the user has listened to so far. +/// The date and time when the domain event occurred. +[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; diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayStartedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayStartedDomainEvent.cs new file mode 100644 index 00000000..a7768e58 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayStartedDomainEvent.cs @@ -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; + +/// +/// Domain event raised when a music play starts. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the play record. +/// The unique identifier of the user that started the play. +/// The unique identifier of the track that was played. +/// The unique identifier of the media library that contains the track. +/// The unique identifier of the album that contains the track. +/// The unique identifier of the artist that produced the track. +/// The date and time when the domain event occurred. +[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; diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayStoppedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayStoppedDomainEvent.cs new file mode 100644 index 00000000..0cb26b6f --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/Events/MusicPlayStoppedDomainEvent.cs @@ -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; + +/// +/// Domain event raised when a music play stops before the track completes. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the play record. +/// The unique identifier of the user that owns the play record. +/// The unique identifier of the track that was stopped. +/// The last position reported by the player. +/// The duration that the user listened to. +/// The date and time when the domain event occurred. +[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; diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/MusicPlayRecord.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/MusicPlayRecord.cs new file mode 100644 index 00000000..e7c57da8 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/MusicPlayRecord.cs @@ -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; + +/// +/// Aggregate root for a recorded music play. +/// +/// +/// 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 is the start time. +/// +[DebuggerDisplay("Id: {Id.Value}; TrackId: {TrackId.Value}; Status: {Status}")] +public sealed class MusicPlayRecord : AggregateRoot +{ + /// + /// Gets the object representing the unique identifier of the user that started the play. + /// + public UserId UserId { get; private set; } + + /// + /// Gets the Id of the track that was played. + /// + public TrackId TrackId { get; private set; } + + /// + /// Gets the Id of the media library that contains the track. + /// + public LibraryId LibraryId { get; private set; } + + /// + /// Gets the Id of the album that contains the track. + /// + public AlbumId AlbumId { get; private set; } + + /// + /// Gets the Id of the artist that produced the track. + /// + public ArtistId ArtistId { get; private set; } + + /// + /// Gets the state of the recorded play. + /// + public PlaybackRecordStatus Status { get; private set; } + + /// + /// Gets the last position reported by the player. + /// + public MusicPlayPosition LastPosition { get; private set; } + + /// + /// Gets the duration that the user has listened to. + /// + public TimeSpan ListenedDuration { get; private set; } + + /// + /// Gets the date and time when the play completed or stopped, if it has ended. + /// + public Optional EndedOnUtc { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// The object representing the unique identifier of the play record. + /// The object representing the unique identifier of the user that started the play. + /// The Id of the track that was played. + /// The Id of the media library that contains the track. + /// The Id of the album that contains the track. + /// The Id of the artist that produced the track. + /// The state of the recorded play. + /// The last position reported by the player. + /// The duration that the user has listened to. + /// The date and time when the play completed or stopped, if it has ended. + /// The date and time when the play started. + /// The date and time when the play record was last updated. + private MusicPlayRecord( + MusicPlayRecordId id, + UserId userId, + TrackId trackId, + LibraryId libraryId, + AlbumId albumId, + ArtistId artistId, + PlaybackRecordStatus status, + MusicPlayPosition lastPosition, + TimeSpan listenedDuration, + Optional endedOnUtc, + DateTime createdOnUtc, + Optional 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; + } + + /// + /// Creates a new play record for a track that started playing. + /// + /// The object representing the unique identifier of the user that started the play. + /// The Id of the track that was played. + /// The Id of the media library that contains the track. + /// The Id of the album that contains the track. + /// The Id of the artist that produced the track. + /// The created instance. + 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.None(), + occurredOnUtc, + Optional.None()); + + playRecord._domainEvents.Add(new MusicPlayStartedDomainEvent( + Guid.NewGuid(), + playRecord.Id, + userId, + trackId, + libraryId, + albumId, + artistId, + occurredOnUtc)); + return playRecord; + } + + /// + /// Creates a new instance of the class, with pre-existing data. + /// + /// The object representing the unique identifier of the play record. + /// The object representing the unique identifier of the user that started the play. + /// The Id of the track that was played. + /// The Id of the media library that contains the track. + /// The Id of the album that contains the track. + /// The Id of the artist that produced the track. + /// The state of the recorded play. + /// The last position reported by the player. + /// The duration that the user has listened to. + /// The date and time when the play completed or stopped, if it has ended. + /// The date and time when the play started. + /// The date and time when the play record was last updated. + /// + /// An containing either a successfully created , or an error message. + /// + public static Result Create( + MusicPlayRecordId id, + UserId userId, + TrackId trackId, + LibraryId libraryId, + AlbumId albumId, + ArtistId artistId, + PlaybackRecordStatus status, + MusicPlayPosition lastPosition, + TimeSpan listenedDuration, + Optional endedOnUtc, + DateTime createdOnUtc, + Optional 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); + } + + /// + /// Updates the last reported position and the listened duration of an in-progress play. + /// + /// The last position reported by the player. + /// The duration that the user has listened to. + /// An representing either a successful update, or an error. + public Result 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.Some(occurredOnUtc); + _domainEvents.Add(new MusicPlayProgressedDomainEvent( + Guid.NewGuid(), + Id, + UserId, + TrackId, + position, + listenedDuration, + occurredOnUtc)); + return Result.Updated; + } + + /// + /// Marks an in-progress play as completed. + /// + /// The last position reported by the player. + /// The duration that the user listened to. + /// An representing either a successful update, or an error. + public Result 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.Some(occurredOnUtc); + UpdatedOnUtc = Optional.Some(occurredOnUtc); + _domainEvents.Add(new MusicPlayCompletedDomainEvent( + Guid.NewGuid(), + Id, + UserId, + TrackId, + position, + listenedDuration, + occurredOnUtc)); + return Result.Updated; + } + + /// + /// Marks an in-progress play as stopped before the track completed. + /// + /// The last position reported by the player. + /// The duration that the user listened to. + /// An representing either a successful update, or an error. + public Result 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.Some(occurredOnUtc); + UpdatedOnUtc = Optional.Some(occurredOnUtc); + _domainEvents.Add(new MusicPlayStoppedDomainEvent( + Guid.NewGuid(), + Id, + UserId, + TrackId, + position, + listenedDuration, + occurredOnUtc)); + return Result.Updated; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/ValueObjects/MusicPlayPosition.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/ValueObjects/MusicPlayPosition.cs new file mode 100644 index 00000000..abaa70ab --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/ValueObjects/MusicPlayPosition.cs @@ -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; + +/// +/// Value Object for the last position reported by a player for a music play record. +/// +public sealed class MusicPlayPosition : ValueObject +{ + /// + /// Gets the position reported by the player. + /// + public TimeSpan Offset { get; } + + /// + /// Gets the position at the very start of a track. + /// + public static MusicPlayPosition Zero { get; } = new(TimeSpan.Zero); + + /// + /// Initializes a new instance of the class. + /// + /// The position reported by the player. + private MusicPlayPosition(TimeSpan offset) + { + Offset = offset; + } + + /// + /// Creates a new instance of the class. + /// + /// The position reported by the player. + /// The created instance. + public static MusicPlayPosition Create(TimeSpan offset) + { + return new MusicPlayPosition(offset); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Offset; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/ValueObjects/MusicPlayRecordId.cs b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/ValueObjects/MusicPlayRecordId.cs new file mode 100644 index 00000000..a8679528 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MusicPlayHistoryBoundedContext/MusicPlayRecordAggregate/ValueObjects/MusicPlayRecordId.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.MusicPlayHistoryBoundedContext.MusicPlayRecordAggregate.ValueObjects; + +/// +/// Value Object for the Id of a music play record. +/// +[DebuggerDisplay("{Value}")] +public sealed class MusicPlayRecordId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private MusicPlayRecordId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static MusicPlayRecordId CreateUnique() + { + return new MusicPlayRecordId(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 MusicPlayRecordId Create(Guid value) + { + return new MusicPlayRecordId(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; + } +}