mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Adds the media consumption history, statistics, resume, and deletion queries
This commit is contained in:
1 parent
bde7ee4b1f
commit
f777af07cf
61 files changed
+2833
-35
No files matched your search
+17
@@ -0,0 +1,17 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DTO.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Data transfer object for the consumption statistics of every user, together with their combined totals.
|
||||
/// </summary>
|
||||
/// <param name="Total">The combined consumption statistics of every user.</param>
|
||||
/// <param name="Users">The consumption statistics of each user.</param>
|
||||
[DebuggerDisplay("UserCount: {Users.Count}; CountedSessionCount: {Total.CountedSessionCount}")]
|
||||
public sealed record AllUsersConsumptionStatisticsDto(
|
||||
ConsumptionTotalsDto Total,
|
||||
IReadOnlyList<ConsumptionStatisticsDto> Users
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DTO.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Data transfer object for the combined consumption statistics of every user.
|
||||
/// </summary>
|
||||
/// <param name="TotalConsumedDurationMilliseconds">The total duration consumed over every recorded session of every user, in milliseconds.</param>
|
||||
/// <param name="CountedSessionCount">The number of recorded sessions of every user that count towards statistics.</param>
|
||||
/// <param name="CompletedSessionCount">The number of recorded sessions of every user that completed.</param>
|
||||
/// <param name="StoppedSessionCount">The number of recorded sessions of every user that stopped before they completed and count towards statistics.</param>
|
||||
/// <param name="CompletionRate">The fraction of the counted sessions of every user that completed, between 0 and 1, or 0 when there is no counted session.</param>
|
||||
/// <param name="MediaKindSummaries">The combined consumption statistics broken down by media kind.</param>
|
||||
[DebuggerDisplay("CountedSessionCount: {CountedSessionCount}; CompletedSessionCount: {CompletedSessionCount}")]
|
||||
public sealed record ConsumptionTotalsDto(
|
||||
long TotalConsumedDurationMilliseconds,
|
||||
int CountedSessionCount,
|
||||
int CompletedSessionCount,
|
||||
int StoppedSessionCount,
|
||||
double CompletionRate,
|
||||
IReadOnlyList<MediaKindConsumptionStatisticsDto> MediaKindSummaries
|
||||
);
|
||||
@@ -124,6 +124,15 @@ public interface IBookRepository : IRepository<BookEntity>,
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the display names of the authors of the books, keyed by book Id, or an error.</returns>
|
||||
Task<Result<IReadOnlyDictionary<Guid, string?>>> GetAuthorsDisplayNamesByBookIdsAsync(IReadOnlyCollection<Guid> bookIds, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the books identified by <paramref name="bookIds"/>, optionally together with their navigation properties.
|
||||
/// </summary>
|
||||
/// <param name="bookIds">The Ids of the books to get.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the books should be loaded together with the books themselves.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the existing books, or an error.</returns>
|
||||
Task<Result<IReadOnlyList<BookEntity>>> GetByIdsAsync(IReadOnlyCollection<Guid> bookIds, bool shouldIncludeNavigationProperties, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the books of the media library identified by <paramref name="libraryId"/>, together with their artwork and their related data.
|
||||
/// </summary>
|
||||
|
||||
+9
@@ -27,6 +27,15 @@ public interface IMusicArtworkRepository : IRepository<MusicArtworkEntity>,
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="MusicArtworkEntity"/>, or an error.</returns>
|
||||
Task<Result<IReadOnlyList<MusicArtworkEntity>>> GetByOwnerAsync(MusicArtworkOwnerType ownerType, Guid ownerId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pieces of artwork owned by the music library items of the provided <paramref name="ownerType"/> identified by any of <paramref name="ownerIds"/>.
|
||||
/// </summary>
|
||||
/// <param name="ownerType">The type of the music library items that own the artwork.</param>
|
||||
/// <param name="ownerIds">The Ids of the music library items that own the artwork.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the artwork of the existing owners, or an error.</returns>
|
||||
Task<Result<IReadOnlyList<MusicArtworkEntity>>> GetByOwnersAsync(MusicArtworkOwnerType ownerType, IReadOnlyCollection<Guid> ownerIds, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the pieces of artwork owned by the music library item of the provided <paramref name="ownerType"/> identified by <paramref name="ownerId"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -137,4 +137,13 @@ public interface ITrackRepository : IRepository<TrackEntity>,
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the access summary of the existing tracks, or an error.</returns>
|
||||
Task<Result<TrackAccessSummaryDto>> GetAccessInfoByIdsAsync(IReadOnlyCollection<Guid> trackIds, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tracks identified by <paramref name="trackIds"/>, optionally together with their navigation properties.
|
||||
/// </summary>
|
||||
/// <param name="trackIds">The Ids of the tracks to get.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the tracks should be loaded together with the tracks themselves.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the existing tracks, or an error.</returns>
|
||||
Task<Result<IReadOnlyList<TrackEntity>>> GetByIdsAsync(IReadOnlyCollection<Guid> trackIds, bool shouldIncludeNavigationProperties, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -178,6 +178,11 @@ public interface IUnitOfWork
|
||||
/// </summary>
|
||||
IMediaResumePointRepository MediaResumePointRepository { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the repository for the consumption records used to build consumption statistics.
|
||||
/// </summary>
|
||||
IConsumptionStatisticsRepository ConsumptionStatisticsRepository { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Saves the changes made to the database.
|
||||
/// </summary>
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="AllUsersConsumptionStatisticsDto"/>.
|
||||
/// </summary>
|
||||
public static class AllUsersConsumptionStatisticsDtoMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="data"/> to <see cref="AllUsersConsumptionStatisticsResponse"/>.
|
||||
/// </summary>
|
||||
/// <param name="data">The data transfer object to be converted.</param>
|
||||
/// <returns>The converted response.</returns>
|
||||
public static AllUsersConsumptionStatisticsResponse ToResponse(this AllUsersConsumptionStatisticsDto data)
|
||||
{
|
||||
return new AllUsersConsumptionStatisticsResponse(
|
||||
data.Total.ToResponse(),
|
||||
[.. data.Users.Select(user => user.ToResponse())]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.Common;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System;
|
||||
@@ -33,7 +34,7 @@ public static class ConsumedItemDtoMapping
|
||||
DateTime.SpecifyKind(data.LastConsumedOnUtc, DateTimeKind.Utc),
|
||||
data.CountedSessionCount,
|
||||
data.CompletedSessionCount,
|
||||
TimeSpan.FromMilliseconds(data.ConsumedDurationMilliseconds).TotalSeconds
|
||||
data.ConsumedDurationMilliseconds.ToClampedTimeSpan().TotalSeconds
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System;
|
||||
#endregion
|
||||
@@ -28,7 +29,7 @@ public static class ConsumptionRecordDtoMapping
|
||||
DateTime.SpecifyKind(data.StartedOnUtc, DateTimeKind.Utc),
|
||||
DateTime.SpecifyKind(data.LastActivityOnUtc, DateTimeKind.Utc),
|
||||
data.EndedOnUtc.HasValue ? DateTime.SpecifyKind(data.EndedOnUtc.Value, DateTimeKind.Utc) : null,
|
||||
TimeSpan.FromMilliseconds(data.ConsumedDurationMilliseconds).TotalSeconds,
|
||||
data.ConsumedDurationMilliseconds.ToClampedTimeSpan().TotalSeconds,
|
||||
data.LastPosition?.ToResponse(),
|
||||
data.LastProgressFraction,
|
||||
data.ConsecutivePagesRead
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
@@ -21,7 +21,7 @@ public static class ConsumptionStatisticsDtoMapping
|
||||
{
|
||||
return new ConsumptionSummaryResponse(
|
||||
data.UserId,
|
||||
TimeSpan.FromMilliseconds(data.TotalConsumedDurationMilliseconds).TotalSeconds,
|
||||
data.TotalConsumedDurationMilliseconds.ToClampedTimeSpan().TotalSeconds,
|
||||
data.CountedSessionCount,
|
||||
data.CompletedSessionCount,
|
||||
data.StoppedSessionCount,
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting <see cref="ConsumptionTotalsDto"/>.
|
||||
/// </summary>
|
||||
public static class ConsumptionTotalsDtoMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="data"/> to <see cref="ConsumptionTotalsResponse"/>.
|
||||
/// </summary>
|
||||
/// <param name="data">The data transfer object to be converted.</param>
|
||||
/// <returns>The converted response.</returns>
|
||||
public static ConsumptionTotalsResponse ToResponse(this ConsumptionTotalsDto data)
|
||||
{
|
||||
return new ConsumptionTotalsResponse(
|
||||
data.TotalConsumedDurationMilliseconds.ToClampedTimeSpan().TotalSeconds,
|
||||
data.CountedSessionCount,
|
||||
data.CompletedSessionCount,
|
||||
data.StoppedSessionCount,
|
||||
data.CompletionRate,
|
||||
[.. data.MediaKindSummaries.Select(mediaKindSummary => mediaKindSummary.ToResponse())]
|
||||
);
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
@@ -39,8 +40,11 @@ public static class MediaConsumptionRecordEntityMapping
|
||||
MediaPositionKind positionKind = repositoryEntity.LastPositionKind.Value;
|
||||
if (positionKind == MediaPositionKind.TimeOffset)
|
||||
{
|
||||
// A stored position outside the representable range is rejected like a negative one, instead of being silently clamped to a position that no client ever reported.
|
||||
long maximumTimeOffsetMilliseconds = TimeSpan.MaxValue.Ticks / TimeSpan.TicksPerMillisecond;
|
||||
if (!repositoryEntity.LastPositionTimeOffsetMilliseconds.HasValue ||
|
||||
repositoryEntity.LastPositionTimeOffsetMilliseconds.Value < 0 ||
|
||||
repositoryEntity.LastPositionTimeOffsetMilliseconds.Value > maximumTimeOffsetMilliseconds ||
|
||||
repositoryEntity.LastPositionLocation is not null)
|
||||
return DomainErrors.MediaConsumption.InvalidMediaPosition;
|
||||
lastPosition = Optional<MediaPosition>.Some(
|
||||
@@ -67,7 +71,7 @@ public static class MediaConsumptionRecordEntityMapping
|
||||
DateTime.SpecifyKind(repositoryEntity.StartedOnUtc, DateTimeKind.Utc),
|
||||
DateTime.SpecifyKind(repositoryEntity.LastActivityOnUtc, DateTimeKind.Utc),
|
||||
endedOnUtc,
|
||||
TimeSpan.FromMilliseconds(repositoryEntity.ConsumedDurationMilliseconds),
|
||||
repositoryEntity.ConsumedDurationMilliseconds.ToClampedTimeSpan(),
|
||||
lastPosition,
|
||||
repositoryEntity.LastProgressFraction,
|
||||
repositoryEntity.ConsecutivePagesRead
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
@@ -20,7 +20,7 @@ public static class MediaKindConsumptionStatisticsDtoMapping
|
||||
{
|
||||
return new MediaKindConsumptionSummaryResponse(
|
||||
data.MediaKind,
|
||||
TimeSpan.FromMilliseconds(data.TotalConsumedDurationMilliseconds).TotalSeconds,
|
||||
data.TotalConsumedDurationMilliseconds.ToClampedTimeSpan().TotalSeconds,
|
||||
data.CountedSessionCount,
|
||||
data.CompletedSessionCount,
|
||||
data.StoppedSessionCount,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
@@ -20,7 +20,7 @@ public static class MediaPositionDtoMapping
|
||||
{
|
||||
return new MediaPositionResponse(
|
||||
data.Kind,
|
||||
data.TimeOffsetMilliseconds.HasValue ? TimeSpan.FromMilliseconds(data.TimeOffsetMilliseconds.Value).TotalSeconds : null,
|
||||
data.TimeOffsetMilliseconds.HasValue ? data.TimeOffsetMilliseconds.Value.ToClampedTimeSpan().TotalSeconds : null,
|
||||
data.Location
|
||||
);
|
||||
}
|
||||
|
||||
+3
@@ -32,8 +32,11 @@ public static class MediaResumePointEntityMapping
|
||||
MediaPosition lastPosition;
|
||||
if (repositoryEntity.LastPositionKind == MediaPositionKind.TimeOffset)
|
||||
{
|
||||
// A stored position outside the representable range is rejected like a negative one, instead of being silently clamped to a position that no client ever reported.
|
||||
long maximumTimeOffsetMilliseconds = TimeSpan.MaxValue.Ticks / TimeSpan.TicksPerMillisecond;
|
||||
if (!repositoryEntity.LastPositionTimeOffsetMilliseconds.HasValue ||
|
||||
repositoryEntity.LastPositionTimeOffsetMilliseconds.Value < 0 ||
|
||||
repositoryEntity.LastPositionTimeOffsetMilliseconds.Value > maximumTimeOffsetMilliseconds ||
|
||||
repositoryEntity.LastPositionLocation is not null)
|
||||
return DomainErrors.MediaConsumption.InvalidMediaPosition;
|
||||
lastPosition = MediaPosition.CreateTimeOffset(TimeSpan.FromMilliseconds(repositoryEntity.LastPositionTimeOffsetMilliseconds.Value));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
@@ -24,7 +24,7 @@ public static class TagConsumptionDtoMapping
|
||||
data.Name,
|
||||
data.TagKind,
|
||||
data.ConsumedItemCount,
|
||||
TimeSpan.FromMilliseconds(data.ConsumedDurationMilliseconds).TotalSeconds
|
||||
data.ConsumedDurationMilliseconds.ToClampedTimeSpan().TotalSeconds
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+29
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.Mapping.Common.Metadata;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
|
||||
using Lumina.Contracts.DTO.Common;
|
||||
@@ -13,6 +14,7 @@ using Lumina.Domain.Core.BoundedContexts.AudioLibraryBoundedContext.MusicLibrary
|
||||
using Lumina.Domain.Core.BoundedContexts.AudioLibraryBoundedContext.MusicLibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -27,6 +29,33 @@ namespace Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibra
|
||||
/// </summary>
|
||||
public static class TrackEntityMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to the consumption history row of a music track.
|
||||
/// </summary>
|
||||
/// <param name="repositoryEntity">The repository entity to be converted.</param>
|
||||
/// <param name="lastConsumedOnUtc">The date and time when the track was last consumed.</param>
|
||||
/// <param name="countedSessionCount">The number of sessions of the track that count towards statistics.</param>
|
||||
/// <param name="completedSessionCount">The number of sessions of the track that completed.</param>
|
||||
/// <param name="consumedDurationMilliseconds">The active duration consumed in the track, over all of its sessions, in milliseconds.</param>
|
||||
/// <param name="coverPath">The path of the cover art of the album that contains the track, if available.</param>
|
||||
/// <returns>The converted consumption history row.</returns>
|
||||
public static ConsumedItemDto ToDto(this TrackEntity repositoryEntity, DateTime lastConsumedOnUtc, int countedSessionCount, int completedSessionCount, long consumedDurationMilliseconds, string? coverPath)
|
||||
{
|
||||
return new ConsumedItemDto(
|
||||
MediaKind.MusicTrack,
|
||||
repositoryEntity.Id,
|
||||
repositoryEntity.LibraryId,
|
||||
true,
|
||||
repositoryEntity.Title,
|
||||
repositoryEntity.Album?.Title ?? string.Empty,
|
||||
repositoryEntity.Album?.Artist?.Name ?? string.Empty,
|
||||
coverPath,
|
||||
lastConsumedOnUtc,
|
||||
countedSessionCount,
|
||||
completedSessionCount,
|
||||
consumedDurationMilliseconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to <see cref="Track"/>.
|
||||
/// </summary>
|
||||
|
||||
+28
@@ -1,5 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DTO.MediaConsumption;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Common.Mapping.Common.Metadata;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
@@ -31,6 +32,33 @@ namespace Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.B
|
||||
/// </summary>
|
||||
public static class BookEntityMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to the consumption history row of a book.
|
||||
/// </summary>
|
||||
/// <param name="repositoryEntity">The repository entity to be converted.</param>
|
||||
/// <param name="lastConsumedOnUtc">The date and time when the book was last consumed.</param>
|
||||
/// <param name="countedSessionCount">The number of sessions of the book that count towards statistics.</param>
|
||||
/// <param name="completedSessionCount">The number of sessions of the book that completed.</param>
|
||||
/// <param name="consumedDurationMilliseconds">The active duration consumed in the book, over all of its sessions, in milliseconds.</param>
|
||||
/// <param name="creatorName">The name of the author of the book, or an empty string when there is none.</param>
|
||||
/// <returns>The converted consumption history row.</returns>
|
||||
public static ConsumedItemDto ToDto(this BookEntity repositoryEntity, DateTime lastConsumedOnUtc, int countedSessionCount, int completedSessionCount, long consumedDurationMilliseconds, string creatorName)
|
||||
{
|
||||
return new ConsumedItemDto(
|
||||
MediaKind.Book,
|
||||
repositoryEntity.Id,
|
||||
repositoryEntity.LibraryId,
|
||||
true,
|
||||
repositoryEntity.Title,
|
||||
string.Empty,
|
||||
creatorName,
|
||||
repositoryEntity.Artwork.FirstOrDefault(artwork => artwork.ArtworkType == ArtworkType.Cover)?.FileName,
|
||||
lastConsumedOnUtc,
|
||||
countedSessionCount,
|
||||
completedSessionCount,
|
||||
consumedDurationMilliseconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts <paramref name="repositoryEntity"/> to <see cref="Book"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for converting stored duration values to <see cref="TimeSpan"/>.
|
||||
/// </summary>
|
||||
public static class DurationUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a duration stored as whole milliseconds to a <see cref="TimeSpan"/>, clamped to the largest value a <see cref="TimeSpan"/> can
|
||||
/// represent so that an accumulated total above that limit does not throw.
|
||||
/// </summary>
|
||||
/// <param name="milliseconds">The duration, in milliseconds.</param>
|
||||
/// <returns>The duration, clamped to <see cref="TimeSpan.MaxValue"/> when it exceeds the representable range.</returns>
|
||||
public static TimeSpan ToClampedTimeSpan(this long milliseconds)
|
||||
{
|
||||
long maximumMilliseconds = TimeSpan.MaxValue.Ticks / TimeSpan.TicksPerMillisecond;
|
||||
return TimeSpan.FromMilliseconds(Math.Min(milliseconds, maximumMilliseconds));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user