mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 15:11:10 +00:00
Adds the consumptions API endpoints, their summaries and documnetation
This commit is contained in:
1 parent
bc65e35de7
commit
c27621818c
24 files changed
+2200
-4
No files matched your search
+3
-2
@@ -4,6 +4,7 @@ using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaConsumption.Commands.StartConsumption;
|
||||
@@ -28,13 +29,13 @@ public class StartConsumptionCommandValidator : AbstractValidator<StartConsumpti
|
||||
.When(command => command.LibraryId is not null && command.LibraryId.Length > 0)
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Validates the kind of media that is consumed, taken from the route. Kinds that are not supported by the current catalogues are rejected by the handler, because that is a business rule.
|
||||
// Validates the kind of media that is consumed, taken from the route. Only the member names are accepted, so a numeric route value that happens to parse as an enumeration value is rejected. Kinds that are not supported by the current catalogues are rejected by the handler, because that is a business rule.
|
||||
RuleFor(command => command.MediaKind)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
RuleFor(command => command.MediaKind)
|
||||
.Must(mediaKind => Enum.TryParse<MediaKind>(mediaKind, ignoreCase: true, out MediaKind parsedMediaKind) && Enum.IsDefined(parsedMediaKind))
|
||||
.Must(mediaKind => Enum.GetNames<MediaKind>().Contains(mediaKind, StringComparer.OrdinalIgnoreCase))
|
||||
.When(command => command.MediaKind is not null && command.MediaKind.Length > 0)
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
|
||||
+4
@@ -76,6 +76,10 @@ public class GetResumePointQueryHandler : IQueryHandler<GetResumePointQuery, Res
|
||||
return DomainErrors.MediaConsumption.UnsupportedMediaKind;
|
||||
if (!ruleResult.Value.CanResume)
|
||||
return DomainErrors.MediaConsumption.MediaKindCannotResume;
|
||||
// A planned kind cannot have a resume point, because its catalogue does not exist yet and start rejects it. Report it as unsupported instead of
|
||||
// as a missing resume point, so the response does not imply that the kind is usable.
|
||||
if (!ruleResult.Value.IsSupportedByCurrentCatalogues)
|
||||
return DomainErrors.MediaConsumption.UnsupportedMediaKind;
|
||||
|
||||
// The resume point is read without tracking and scoped to the current user, so a resume point of another user is indistinguishable from a missing one.
|
||||
Result<MediaResumePointEntity?> getResumePointResult = await _unitOfWork.MediaResumePointRepository
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@ using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaConsumption.Queries.GetResumePoint;
|
||||
@@ -18,13 +19,13 @@ public class GetResumePointQueryValidator : AbstractValidator<GetResumePointQuer
|
||||
/// </summary>
|
||||
public GetResumePointQueryValidator()
|
||||
{
|
||||
// Validates the kind of media to resume, taken from the route.
|
||||
// Validates the kind of media to resume, taken from the route. Only the member names are accepted, so a numeric route value that happens to parse as an enumeration value is rejected.
|
||||
RuleFor(query => query.MediaKind)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
RuleFor(query => query.MediaKind)
|
||||
.Must(mediaKind => Enum.TryParse(mediaKind, ignoreCase: true, out MediaKind parsedMediaKind) && Enum.IsDefined(parsedMediaKind))
|
||||
.Must(mediaKind => Enum.GetNames<MediaKind>().Contains(mediaKind, StringComparer.OrdinalIgnoreCase))
|
||||
.When(query => query.MediaKind is not null && query.MediaKind.Length > 0)
|
||||
.WithError(Errors.MediaConsumption.UnsupportedMediaKind);
|
||||
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
namespace Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class for the collection of the media consumption routes of the API.
|
||||
/// </summary>
|
||||
public static partial class ApiRoutes
|
||||
{
|
||||
/// <summary>
|
||||
/// Routes for the media consumption lifecycle, records, and resume points of the current user.
|
||||
/// </summary>
|
||||
public static class MediaConsumption
|
||||
{
|
||||
public const string START_CONSUMPTION = "/libraries/{libraryId}/media-items/{mediaKind}/{mediaItemId}/consumption-records";
|
||||
public const string GET_CONSUMPTION_RECORD = "/users/me/consumption-records/{recordId}";
|
||||
public const string REPORT_CONSUMPTION_PROGRESS = "/users/me/consumption-records/{recordId}/progress";
|
||||
public const string COMPLETE_CONSUMPTION = "/users/me/consumption-records/{recordId}/complete";
|
||||
public const string STOP_CONSUMPTION = "/users/me/consumption-records/{recordId}/stop";
|
||||
public const string GET_RESUME_POINT = "/users/me/media-resume-points/{mediaKind}/{mediaItemId}";
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
using Lumina.Application.Core.MediaConsumption.Commands.CompleteConsumption;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.CompleteConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/users/me/consumption-records/{recordId}/complete</c> route.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Uses "me" because it is always for the currently authenticated user.
|
||||
/// </remarks>
|
||||
public class CompleteConsumptionEndpoint : BaseEndpoint<CompleteConsumptionRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<CompleteConsumptionCommand, Result<ConsumptionRecordResponse>> _completeConsumptionCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompleteConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="completeConsumptionCommandHandler">Injected service for handling complete consumption commands.</param>
|
||||
public CompleteConsumptionEndpoint(ICommandHandler<CompleteConsumptionCommand, Result<ConsumptionRecordResponse>> completeConsumptionCommandHandler)
|
||||
{
|
||||
_completeConsumptionCommandHandler = completeConsumptionCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.POST);
|
||||
Routes(ApiRoutes.MediaConsumption.COMPLETE_CONSUMPTION);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports that a consumption session of the current user completed.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the final position, the progress fraction, and the consumed duration.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(CompleteConsumptionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers.Append("Vary", "Authorization");
|
||||
// Take the unique identifier from the route, so that it cannot be replaced by a value posted in the body.
|
||||
string? recordId = HttpContext.Request.RouteValues["recordId"]?.ToString();
|
||||
Result<ConsumptionRecordResponse> result = await _completeConsumptionCommandHandler.HandleAsync(request.ToCommand(recordId), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.CompleteConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="CompleteConsumptionEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CompleteConsumptionEndpointSummary : Summary<CompleteConsumptionEndpoint, CompleteConsumptionRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompleteConsumptionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public CompleteConsumptionEndpointSummary()
|
||||
{
|
||||
Summary = "Completes a consumption session of the current user.";
|
||||
Description = "Completes an in-progress consumption session of the current user, storing its final position, progress fraction, and active consumed duration. Completion is explicit, so it is recorded even when the media was not played to its final second. Completing a session deletes the resume point of the current user for the same media item. A music track reports its final position as a TimeOffset with TimeOffsetSeconds, while a book reports it as a Location with an opaque locator. A record of another user is reported with the same not found error as a missing record.";
|
||||
|
||||
ExampleRequest = new CompleteConsumptionRequest(
|
||||
Position: new MediaPositionRequest("TimeOffset", 300, null),
|
||||
ProgressFraction: 1,
|
||||
ConsumedDurationInSeconds: 300
|
||||
);
|
||||
RequestExamples.Add(new RequestExample(new CompleteConsumptionRequest(
|
||||
Position: new MediaPositionRequest("Location", null, "epubcfi(/6/8[chapter-4]!/4/2/2)"),
|
||||
ProgressFraction: 1,
|
||||
ConsumedDurationInSeconds: 900
|
||||
)));
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
RequestParam(request => request.Position, "The final position reported for the media item, if the client knows it. It carries the position Kind, and either TimeOffsetSeconds or Location. Optional.");
|
||||
RequestParam(request => request.ProgressFraction, "The final fraction of the media item that the client reached, between 0 and 1. Required.");
|
||||
RequestParam(request => request.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds. Required.");
|
||||
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Id, "The Id of the consumption record.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LibraryId, "The Id of the media library that contains the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaItemId, "The Id of the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaKind, "The kind of media that was consumed.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Status, "The state of the consumption session.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.StartedOnUtc, "The date and time when the session started.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastActivityOnUtc, "The date and time when the session last received activity.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.EndedOnUtc, "The date and time when the session completed.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastPosition, "The final known position for the media item, if any. It carries the position Kind, and either TimeOffsetSeconds or Location.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastProgressFraction, "The final reported progress fraction of the media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsecutivePagesRead, "The number of consecutive pages read in the session.");
|
||||
|
||||
Response<ConsumptionRecordResponse>(statusCode: 200, description: "The consumption session is completed, its resume point is deleted, and the updated record is returned.", contentType: "application/json",
|
||||
example: new ConsumptionRecordResponse(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.Completed,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 5, 0, DateTimeKind.Utc),
|
||||
EndedOnUtc: new DateTime(2026, 10, 24, 12, 5, 0, DateTimeKind.Utc),
|
||||
ConsumedDurationInSeconds: 300,
|
||||
LastPosition: new MediaPositionResponse(MediaPositionKind.TimeOffset, 300, null),
|
||||
LastProgressFraction: 1,
|
||||
ConsecutivePagesRead: 0
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Authentication failed",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token has expired",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated request does not carry the identity of a user, or because the reported consumed duration is lower than the duration already stored for the session.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Unauthorized",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "ConsumedDurationCannotDecrease",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the consumption record does not exist, or it does not belong to the current user.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ConsumptionRecordNotFound",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(409, "The request failed because the session is already terminal, or because another request changed it after this request read it.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConsumptionIsAlreadyTerminal",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConcurrencyConflict",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/complete",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"ConsumptionRecordIdCannotBeEmpty",
|
||||
"InvalidMediaPosition",
|
||||
"InvalidProgressFraction",
|
||||
"ConsumedDurationMustBeNonNegative",
|
||||
"ConsumedDurationIsOutOfRange"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Core.MediaConsumption.Queries.GetConsumptionRecord;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.GetConsumptionRecord;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/users/me/consumption-records/{recordId}</c> route.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Uses "me" because it is always for the currently authenticated user.
|
||||
/// </remarks>
|
||||
public class GetConsumptionRecordEndpoint : BaseEndpoint<FastEndpoints.EmptyRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetConsumptionRecordQuery, Result<ConsumptionRecordResponse>> _getConsumptionRecordQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetConsumptionRecordEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getConsumptionRecordQueryHandler">Injected service for handling get consumption record queries.</param>
|
||||
public GetConsumptionRecordEndpoint(IQueryHandler<GetConsumptionRecordQuery, Result<ConsumptionRecordResponse>> getConsumptionRecordQueryHandler)
|
||||
{
|
||||
_getConsumptionRecordQueryHandler = getConsumptionRecordQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.GET);
|
||||
Routes(ApiRoutes.MediaConsumption.GET_CONSUMPTION_RECORD);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a consumption record of the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request object.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(FastEndpoints.EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers.Append("Vary", "Authorization");
|
||||
// Take the unique identifier from the route.
|
||||
string? recordId = HttpContext.Request.RouteValues["recordId"]?.ToString();
|
||||
Result<ConsumptionRecordResponse> result = await _getConsumptionRecordQueryHandler.HandleAsync(new GetConsumptionRecordQuery(recordId), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.GetConsumptionRecord;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetConsumptionRecordEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetConsumptionRecordEndpointSummary : Summary<GetConsumptionRecordEndpoint, EmptyRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetConsumptionRecordEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetConsumptionRecordEndpointSummary()
|
||||
{
|
||||
Summary = "Retrieves a consumption record of the current user.";
|
||||
Description = "Retrieves the consumption record identified by the route, when it belongs to the current user. The record carries the kind of media, the last reported position, the progress fraction, the consumed duration, the consecutive pages read for page based media, and whether the session is in progress, completed or stopped. It never carries a queue, a current index, a repeat or shuffle state, or a playback session. A record of another user is reported with the same not found error as a missing record. The example is a book record, whose position is an opaque location; a music track record uses a time offset instead.";
|
||||
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Id, "The Id of the consumption record.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LibraryId, "The Id of the media library that contains the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaItemId, "The Id of the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaKind, "The kind of media that was consumed.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Status, "The state of the consumption session.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.StartedOnUtc, "The date and time when the session started.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastActivityOnUtc, "The date and time when the session last received activity.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.EndedOnUtc, "The date and time when the session completed or stopped, if it has ended.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastPosition, "The last known position for the media item, if any. It carries the position Kind, and either TimeOffsetSeconds or Location.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastProgressFraction, "The latest reported progress fraction of the media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsecutivePagesRead, "The number of consecutive pages read in the session.");
|
||||
|
||||
Response<ConsumptionRecordResponse>(statusCode: 200, description: "The consumption record is returned.", contentType: "application/json",
|
||||
example: new ConsumptionRecordResponse(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d"),
|
||||
MediaKind: MediaKind.Book,
|
||||
Status: ConsumptionStatus.Stopped,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 12, 0, DateTimeKind.Utc),
|
||||
EndedOnUtc: new DateTime(2026, 10, 24, 12, 12, 0, DateTimeKind.Utc),
|
||||
ConsumedDurationInSeconds: 720,
|
||||
LastPosition: new MediaPositionResponse(MediaPositionKind.Location, null, "epubcfi(/6/4[chapter-3]!/4/2/2)"),
|
||||
LastProgressFraction: 0.42,
|
||||
ConsecutivePagesRead: 6
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Authentication failed",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token has expired",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated request does not carry the identity of a user.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Unauthorized",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the consumption record does not exist, or it does not belong to the current user.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ConsumptionRecordNotFound",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"ConsumptionRecordIdCannotBeEmpty"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Core.MediaConsumption.Queries.GetResumePoint;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.GetResumePoint;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/users/me/media-resume-points/{mediaKind}/{mediaItemId}</c> route.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Uses "me" because it is always for the currently authenticated user.
|
||||
/// </remarks>
|
||||
public class GetResumePointEndpoint : BaseEndpoint<FastEndpoints.EmptyRequest, IResult>
|
||||
{
|
||||
private readonly IQueryHandler<GetResumePointQuery, Result<ResumePointResponse>> _getResumePointQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetResumePointEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="getResumePointQueryHandler">Injected service for handling get resume point queries.</param>
|
||||
public GetResumePointEndpoint(IQueryHandler<GetResumePointQuery, Result<ResumePointResponse>> getResumePointQueryHandler)
|
||||
{
|
||||
_getResumePointQueryHandler = getResumePointQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.GET);
|
||||
Routes(ApiRoutes.MediaConsumption.GET_RESUME_POINT);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resume point of the current user for a media item.
|
||||
/// </summary>
|
||||
/// <param name="request">The request object.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(FastEndpoints.EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The resume point belongs to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers.Append("Vary", "Authorization");
|
||||
// Take the unique identifiers from the route.
|
||||
string? mediaKind = HttpContext.Request.RouteValues["mediaKind"]?.ToString();
|
||||
string? mediaItemId = HttpContext.Request.RouteValues["mediaItemId"]?.ToString();
|
||||
Result<ResumePointResponse> result = await _getResumePointQueryHandler.HandleAsync(new GetResumePointQuery(mediaKind, mediaItemId), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.GetResumePoint;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="GetResumePointEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetResumePointEndpointSummary : Summary<GetResumePointEndpoint, EmptyRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetResumePointEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public GetResumePointEndpointSummary()
|
||||
{
|
||||
Summary = "Retrieves the resume point of the current user for a media item.";
|
||||
Description = "Retrieves the point at which the current user can resume the media item named by the route. A book example is /users/me/media-resume-points/Book/{bookId}, and a music track example is /users/me/media-resume-points/MusicTrack/{trackId}. A music track resume point carries a TimeOffset position, while a book resume point carries a Location with an opaque locator. Only the supported MusicTrack and Book kinds can resume. A planned kind whose catalogue does not exist yet is rejected with UnsupportedMediaKind, while a photo has no position representation and is rejected with MediaKindCannotResume. A resume point of another user is reported with the same not found error as a missing resume point. When the media item no longer exists, or no longer belongs to the recorded library, the orphan resume point is deleted and the item is reported as not found.";
|
||||
|
||||
Params["mediaKind"] = "The kind of media to resume. One of MusicTrack or Book. A Photo cannot resume. Only the member names are accepted; numeric values are rejected.";
|
||||
Params["mediaItemId"] = "The Id of the media item to resume.";
|
||||
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.Id, "The Id of the resume point.");
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.LibraryId, "The Id of the media library that contains the media item.");
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.MediaItemId, "The Id of the media item to resume.");
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.MediaKind, "The kind of media that is resumed.");
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.LastPosition, "The position at which the media item can be resumed. It carries the position Kind, and either TimeOffsetSeconds or Location.");
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.LastProgressFraction, "The progress fraction at which the media item can be resumed.");
|
||||
ResponseParam<ResumePointResponse>(resumePoint => resumePoint.LastConsumedOnUtc, "The date and time when the media item was last consumed.");
|
||||
|
||||
Response<ResumePointResponse>(statusCode: 200, description: "The resume point is returned.", contentType: "application/json",
|
||||
example: new ResumePointResponse(
|
||||
Id: Guid.Parse("4d5e6f70-8192-4a3b-9c4d-5e6f708192a3"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d"),
|
||||
MediaKind: MediaKind.Book,
|
||||
LastPosition: new MediaPositionResponse(MediaPositionKind.Location, null, "epubcfi(/6/4[chapter-3]!/4/2/2)"),
|
||||
LastProgressFraction: 0.42,
|
||||
LastConsumedOnUtc: new DateTime(2026, 10, 24, 12, 12, 0, DateTimeKind.Utc)
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Authentication failed",
|
||||
instance = "/api/v1/users/me/media-resume-points/Book/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token has expired",
|
||||
instance = "/api/v1/users/me/media-resume-points/Book/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated request does not carry the identity of a user, or because the kind of media cannot be resumed.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Unauthorized",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/users/me/media-resume-points/Photo/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "MediaKindCannotResume",
|
||||
instance = "/api/v1/users/me/media-resume-points/Photo/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the resume point does not exist, because the media item does not exist, or because the media item no longer belongs to the recorded library.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "MediaResumePointNotFound",
|
||||
instance = "/api/v1/users/me/media-resume-points/Book/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "TrackNotFound",
|
||||
instance = "/api/v1/users/me/media-resume-points/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "BookNotFound",
|
||||
instance = "/api/v1/users/me/media-resume-points/Book/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/users/me/media-resume-points/Book/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"MediaItemIdCannotBeEmpty",
|
||||
"UnsupportedMediaKind"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
using Lumina.Application.Core.MediaConsumption.Commands.ReportConsumptionProgress;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.ReportConsumptionProgress;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/users/me/consumption-records/{recordId}/progress</c> route.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Uses "me" because it is always for the currently authenticated user.
|
||||
/// </remarks>
|
||||
public class ReportConsumptionProgressEndpoint : BaseEndpoint<ReportConsumptionProgressRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<ReportConsumptionProgressCommand, Result<ConsumptionRecordResponse>> _reportConsumptionProgressCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReportConsumptionProgressEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="reportConsumptionProgressCommandHandler">Injected service for handling report consumption progress commands.</param>
|
||||
public ReportConsumptionProgressEndpoint(ICommandHandler<ReportConsumptionProgressCommand, Result<ConsumptionRecordResponse>> reportConsumptionProgressCommandHandler)
|
||||
{
|
||||
_reportConsumptionProgressCommandHandler = reportConsumptionProgressCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.PUT);
|
||||
Routes(ApiRoutes.MediaConsumption.REPORT_CONSUMPTION_PROGRESS);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports the progress of a consumption session of the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the position, the progress fraction, and the consumed duration.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(ReportConsumptionProgressRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers.Append("Vary", "Authorization");
|
||||
// Take the unique identifier from the route, so that it cannot be replaced by a value posted in the body.
|
||||
string? recordId = HttpContext.Request.RouteValues["recordId"]?.ToString();
|
||||
Result<ConsumptionRecordResponse> result = await _reportConsumptionProgressCommandHandler.HandleAsync(request.ToCommand(recordId), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.ReportConsumptionProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="ReportConsumptionProgressEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ReportConsumptionProgressEndpointSummary : Summary<ReportConsumptionProgressEndpoint, ReportConsumptionProgressRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReportConsumptionProgressEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public ReportConsumptionProgressEndpointSummary()
|
||||
{
|
||||
Summary = "Reports the progress of a consumption session of the current user.";
|
||||
Description = "Reports the last position, the progress fraction, and the active consumed duration of an in-progress consumption session of the current user. A music track reports its position as a TimeOffset with TimeOffsetSeconds, while a book reports it as a Location with an opaque locator and the number of consecutive pages read. The position and the progress fraction are the latest reported values and may move backwards. The consumed duration is the only monotonic measurement: it can never decrease within the session. A record of another user is reported with the same not found error as a missing record.";
|
||||
|
||||
ExampleRequest = new ReportConsumptionProgressRequest(
|
||||
Position: new MediaPositionRequest("TimeOffset", 92.5, null),
|
||||
ProgressFraction: 0.31,
|
||||
ConsumedDurationInSeconds: 92.5,
|
||||
ConsecutivePagesRead: 0
|
||||
);
|
||||
RequestExamples.Add(new RequestExample(new ReportConsumptionProgressRequest(
|
||||
Position: new MediaPositionRequest("Location", null, "epubcfi(/6/4[chapter-3]!/4/2/2)"),
|
||||
ProgressFraction: 0.42,
|
||||
ConsumedDurationInSeconds: 240,
|
||||
ConsecutivePagesRead: 2
|
||||
)));
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
RequestParam(request => request.Position, "The last position reported for the media item, if the client knows it. It carries the position Kind, and either TimeOffsetSeconds or Location. Optional.");
|
||||
RequestParam(request => request.ProgressFraction, "The fraction of the media item that the client reached, between 0 and 1. Required.");
|
||||
RequestParam(request => request.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds. Required.");
|
||||
RequestParam(request => request.ConsecutivePagesRead, "The number of consecutive pages read in the current session, used by page based media such as books. Optional, defaults to 0.");
|
||||
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Id, "The Id of the consumption record.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LibraryId, "The Id of the media library that contains the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaItemId, "The Id of the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaKind, "The kind of media that was consumed.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Status, "The state of the consumption session.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.StartedOnUtc, "The date and time when the session started.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastActivityOnUtc, "The date and time when the session last received activity.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.EndedOnUtc, "The date and time when the session completed or stopped, if it has ended.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastPosition, "The last known position for the media item, if any. It carries the position Kind, and either TimeOffsetSeconds or Location.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastProgressFraction, "The latest reported progress fraction of the media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsecutivePagesRead, "The number of consecutive pages read in the session.");
|
||||
|
||||
Response<ConsumptionRecordResponse>(statusCode: 200, description: "The progress of the consumption session is reported and the updated record is returned.", contentType: "application/json",
|
||||
example: new ConsumptionRecordResponse(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.InProgress,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 1, 32, 500, DateTimeKind.Utc),
|
||||
EndedOnUtc: null,
|
||||
ConsumedDurationInSeconds: 92.5,
|
||||
LastPosition: new MediaPositionResponse(MediaPositionKind.TimeOffset, 92.5, null),
|
||||
LastProgressFraction: 0.31,
|
||||
ConsecutivePagesRead: 0
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Authentication failed",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token has expired",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated request does not carry the identity of a user, because the reported consumed duration is lower than the duration already stored for the session, or because a session of a non-page-based kind reported consecutive pages.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Unauthorized",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "ConsumedDurationCannotDecrease",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "ConsecutivePagesReadIsOnlySupportedForPageBasedMedia",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the consumption record does not exist, or it does not belong to the current user.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ConsumptionRecordNotFound",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(409, "The request failed because the session is not in progress anymore, or because another report changed it after this request read it.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConsumptionIsNotInProgress",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConcurrencyConflict",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/progress",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"ConsumptionRecordIdCannotBeEmpty",
|
||||
"InvalidMediaPosition",
|
||||
"InvalidProgressFraction",
|
||||
"ConsumedDurationMustBeNonNegative",
|
||||
"ConsumedDurationIsOutOfRange",
|
||||
"ConsecutivePagesReadMustBeNonNegative"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Core.MediaConsumption.Commands.StartConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.StartConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/libraries/{libraryId}/media-items/{mediaKind}/{mediaItemId}/consumption-records</c> route.
|
||||
/// </summary>
|
||||
public class StartConsumptionEndpoint : BaseEndpoint<FastEndpoints.EmptyRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<StartConsumptionCommand, Result<ConsumptionRecordResponse>> _startConsumptionCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="startConsumptionCommandHandler">Injected service for handling start consumption commands.</param>
|
||||
public StartConsumptionEndpoint(ICommandHandler<StartConsumptionCommand, Result<ConsumptionRecordResponse>> startConsumptionCommandHandler)
|
||||
{
|
||||
_startConsumptionCommandHandler = startConsumptionCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.POST);
|
||||
Routes(ApiRoutes.MediaConsumption.START_CONSUMPTION);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a media consumption session of the current user.
|
||||
/// </summary>
|
||||
/// <param name="request">The request object.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(FastEndpoints.EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers.Append("Vary", "Authorization");
|
||||
// Take the unique identifiers from the route, so that they cannot be replaced by values posted in a body.
|
||||
string? libraryId = HttpContext.Request.RouteValues["libraryId"]?.ToString();
|
||||
string? mediaKind = HttpContext.Request.RouteValues["mediaKind"]?.ToString();
|
||||
string? mediaItemId = HttpContext.Request.RouteValues["mediaItemId"]?.ToString();
|
||||
Result<ConsumptionRecordResponse> result = await _startConsumptionCommandHandler.HandleAsync(new StartConsumptionCommand(libraryId, mediaKind, mediaItemId), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.StartConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="StartConsumptionEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StartConsumptionEndpointSummary : Summary<StartConsumptionEndpoint, EmptyRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StartConsumptionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public StartConsumptionEndpointSummary()
|
||||
{
|
||||
Summary = "Starts a media consumption session of the current user.";
|
||||
Description = "Starts a consumption record for the media item named by the route, in the library named by the route. The supported kinds are MusicTrack and Book. A music track example is /libraries/{libraryId}/media-items/MusicTrack/{trackId}/consumption-records, and a book example is /libraries/{libraryId}/media-items/Book/{bookId}/consumption-records. When the library tracks progress and the kind can resume, the new record starts from the stored resume point; the consumed duration always starts at zero. A missing media item and a media item that belongs to another library are reported with the same not found error, so the route cannot be used to probe another library. The record belongs to the current user, and it never stores a queue, a current index, a repeat or shuffle state, or a playback session.";
|
||||
|
||||
Params["libraryId"] = "The Id of the media library that contains the media item.";
|
||||
Params["mediaKind"] = "The kind of media that is consumed. One of MusicTrack or Book. Only the member names are accepted; numeric values are rejected.";
|
||||
Params["mediaItemId"] = "The Id of the consumed media item.";
|
||||
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Id, "The Id of the consumption record.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LibraryId, "The Id of the media library that contains the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaItemId, "The Id of the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaKind, "The kind of media that was consumed.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Status, "The state of the consumption session.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.StartedOnUtc, "The date and time when the session started.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastActivityOnUtc, "The date and time when the session last received activity.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.EndedOnUtc, "The date and time when the session completed or stopped, if it has ended.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastPosition, "The last known position for the media item, if any. It carries the position Kind, and either TimeOffsetSeconds or Location.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastProgressFraction, "The latest reported progress fraction of the media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsecutivePagesRead, "The number of consecutive pages read in the session.");
|
||||
|
||||
Response(statusCode: 200, description: "The consumption record is started and returned.", contentType: "application/json",
|
||||
example: new ConsumptionRecordResponse(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.InProgress,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
EndedOnUtc: null,
|
||||
ConsumedDurationInSeconds: 0,
|
||||
LastPosition: null,
|
||||
LastProgressFraction: 0,
|
||||
ConsecutivePagesRead: 0
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Authentication failed",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token has expired",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated request does not carry the identity of a user, or because the current user cannot access the library.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Unauthorized",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the library does not exist, or because the media item does not exist, or does not belong to the library named by the route.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "LibraryNotFound",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "TrackNotFound",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "BookNotFound",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/Book/7a6b5c4d-3e2f-4a1b-8c9d-0e1f2a3b4c5d/consumption-records",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(409, "The request failed because the consumption record could not be stored, because the database is busy or locked. Repeating the request is safe.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConcurrencyConflict",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/libraries/3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a/media-items/MusicTrack/9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f/consumption-records",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"LibraryIdCannotBeEmpty",
|
||||
"MediaItemIdCannotBeEmpty",
|
||||
"UnsupportedMediaKind"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.MediaConsumption;
|
||||
using Lumina.Application.Core.MediaConsumption.Commands.StopConsumption;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Presentation.Api.Common.Routes.MediaConsumption;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.StopConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// API endpoint for the <c>/users/me/consumption-records/{recordId}/stop</c> route.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Uses "me" because it is always for the currently authenticated user.
|
||||
/// </remarks>
|
||||
public class StopConsumptionEndpoint : BaseEndpoint<StopConsumptionRequest, IResult>
|
||||
{
|
||||
private readonly ICommandHandler<StopConsumptionCommand, Result<ConsumptionRecordResponse>> _stopConsumptionCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopConsumptionEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="stopConsumptionCommandHandler">Injected service for handling stop consumption commands.</param>
|
||||
public StopConsumptionEndpoint(ICommandHandler<StopConsumptionCommand, Result<ConsumptionRecordResponse>> stopConsumptionCommandHandler)
|
||||
{
|
||||
_stopConsumptionCommandHandler = stopConsumptionCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the API endpoint.
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(FastEndpoints.Http.POST);
|
||||
Routes(ApiRoutes.MediaConsumption.STOP_CONSUMPTION);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports that a consumption session of the current user stopped before it completed.
|
||||
/// </summary>
|
||||
/// <param name="request">The request containing the final position, the progress fraction, and the consumed duration.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(StopConsumptionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The consumption record belongs to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers.Append("Vary", "Authorization");
|
||||
// Take the unique identifier from the route, so that it cannot be replaced by a value posted in the body.
|
||||
string? recordId = HttpContext.Request.RouteValues["recordId"]?.ToString();
|
||||
Result<ConsumptionRecordResponse> result = await _stopConsumptionCommandHandler.HandleAsync(request.ToCommand(recordId), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Contracts.Requests.MediaConsumption;
|
||||
using Lumina.Contracts.Responses.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.Core.Endpoints.MediaConsumption.StopConsumption;
|
||||
|
||||
/// <summary>
|
||||
/// Class used for providing a textual description for the <see cref="StopConsumptionEndpoint"/> API endpoint, for OpenAPI.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StopConsumptionEndpointSummary : Summary<StopConsumptionEndpoint, StopConsumptionRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StopConsumptionEndpointSummary"/> class.
|
||||
/// </summary>
|
||||
public StopConsumptionEndpointSummary()
|
||||
{
|
||||
Summary = "Stops a consumption session of the current user.";
|
||||
Description = "Stops an in-progress consumption session of the current user, storing its final position, progress fraction, and active consumed duration. When the library tracks progress and the final progress reaches the library completion threshold, the session is recorded as completed instead of stopped and its resume point is deleted. Otherwise the session is recorded as stopped, and when the library tracks progress, the kind can resume, and a final position is known, its resume point is saved for the next time. A music track reports its final position as a TimeOffset with TimeOffsetSeconds, while a book reports it as a Location with an opaque locator. A record of another user is reported with the same not found error as a missing record.";
|
||||
|
||||
ExampleRequest = new StopConsumptionRequest(
|
||||
Position: new MediaPositionRequest("TimeOffset", 60, null),
|
||||
ProgressFraction: 0.6,
|
||||
ConsumedDurationInSeconds: 60
|
||||
);
|
||||
RequestExamples.Add(new RequestExample(new StopConsumptionRequest(
|
||||
Position: new MediaPositionRequest("Location", null, "epubcfi(/6/6[chapter-2]!/4/2/2)"),
|
||||
ProgressFraction: 0.18,
|
||||
ConsumedDurationInSeconds: 300
|
||||
)));
|
||||
Params["recordId"] = "The Id of the consumption record.";
|
||||
|
||||
RequestParam(request => request.Position, "The final position reported for the media item, if the client knows it. It carries the position Kind, and either TimeOffsetSeconds or Location. Optional.");
|
||||
RequestParam(request => request.ProgressFraction, "The final fraction of the media item that the client reached, between 0 and 1. Required.");
|
||||
RequestParam(request => request.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds. Required.");
|
||||
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Id, "The Id of the consumption record.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LibraryId, "The Id of the media library that contains the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaItemId, "The Id of the consumed media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.MediaKind, "The kind of media that was consumed.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.Status, "The state of the consumption session.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.StartedOnUtc, "The date and time when the session started.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastActivityOnUtc, "The date and time when the session last received activity.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.EndedOnUtc, "The date and time when the session completed or stopped.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsumedDurationInSeconds, "The active duration consumed by the session, in seconds.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastPosition, "The final known position for the media item, if any. It carries the position Kind, and either TimeOffsetSeconds or Location.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.LastProgressFraction, "The final reported progress fraction of the media item.");
|
||||
ResponseParam<ConsumptionRecordResponse>(record => record.ConsecutivePagesRead, "The number of consecutive pages read in the session.");
|
||||
|
||||
Response(statusCode: 200, description: "The consumption session is stopped, or completed when it reached the library threshold, and the updated record is returned.", contentType: "application/json",
|
||||
example: new ConsumptionRecordResponse(
|
||||
Id: Guid.Parse("2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f"),
|
||||
LibraryId: Guid.Parse("3b3a19f3-1f5a-4d5a-9a3a-5c5a4a3a2a1a"),
|
||||
MediaItemId: Guid.Parse("9f0e1d2c-3b4a-4c6d-8e7f-9a0b1c2d3e4f"),
|
||||
MediaKind: MediaKind.MusicTrack,
|
||||
Status: ConsumptionStatus.Stopped,
|
||||
StartedOnUtc: new DateTime(2026, 10, 24, 12, 0, 0, DateTimeKind.Utc),
|
||||
LastActivityOnUtc: new DateTime(2026, 10, 24, 12, 1, 0, DateTimeKind.Utc),
|
||||
EndedOnUtc: new DateTime(2026, 10, 24, 12, 1, 0, DateTimeKind.Utc),
|
||||
ConsumedDurationInSeconds: 60,
|
||||
LastPosition: new MediaPositionResponse(MediaPositionKind.TimeOffset, 60, null),
|
||||
LastProgressFraction: 0.6,
|
||||
ConsecutivePagesRead: 0
|
||||
)
|
||||
);
|
||||
|
||||
Response(401, "Authentication required.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "Authentication failed",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
|
||||
status = 401,
|
||||
title = "Unauthorized",
|
||||
detail = "The token has expired",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated request does not carry the identity of a user, or because the reported consumed duration is lower than the duration already stored for the session.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Unauthorized",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "ConsumedDurationCannotDecrease",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the consumption record does not exist, or it does not belong to the current user.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "ConsumptionRecordNotFound",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
);
|
||||
|
||||
Response(409, "The request failed because the session is already terminal, or because another request changed it after this request read it.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConsumptionIsAlreadyTerminal",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.10",
|
||||
title = "General.Conflict",
|
||||
status = 409,
|
||||
detail = "ConcurrencyConflict",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
title = "General.Validation",
|
||||
status = 422,
|
||||
detail = "OneOrMoreValidationErrorsOccurred",
|
||||
instance = "/api/v1/users/me/consumption-records/2c1b0a9f-8e7d-4c6b-5a4f-3e2d1c0b9a8f/stop",
|
||||
errors = new Dictionary<string, string[]>
|
||||
{
|
||||
{
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"ConsumptionRecordIdCannotBeEmpty",
|
||||
"InvalidMediaPosition",
|
||||
"InvalidProgressFraction",
|
||||
"ConsumedDurationMustBeNonNegative",
|
||||
"ConsumedDurationIsOutOfRange"
|
||||
}
|
||||
}
|
||||
},
|
||||
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "favorites", "favorites", "{
|
||||
..\docs\technical\api\favorites\Api.Favorites.md = ..\docs\technical\api\favorites\Api.Favorites.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "media-consumption", "media-consumption", "{A3E6B1C4-7D2F-4A98-8B5C-1F0D3E6A9C42}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\docs\technical\api\media-consumption\Api.MediaConsumption.md = ..\docs\technical\api\media-consumption\Api.MediaConsumption.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "file-system-management", "file-system-management", "{2FA735A6-2141-44AB-B34C-F65E4195B1EB}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\docs\technical\api\file-system-management\Api.Directories.md = ..\docs\technical\api\file-system-management\Api.Directories.md
|
||||
@@ -228,6 +233,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "favorites", "favorites", "{
|
||||
..\docs\technical\requests\favorites\RemoveFavorite.http = ..\docs\technical\requests\favorites\RemoveFavorite.http
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "media-consumption", "media-consumption", "{B4F7C2D5-8E3A-4B69-9C6D-2A1E4F7B0D53}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
..\docs\technical\requests\media-consumption\CompleteConsumption.http = ..\docs\technical\requests\media-consumption\CompleteConsumption.http
|
||||
..\docs\technical\requests\media-consumption\GetConsumptionRecord.http = ..\docs\technical\requests\media-consumption\GetConsumptionRecord.http
|
||||
..\docs\technical\requests\media-consumption\GetResumePoint.http = ..\docs\technical\requests\media-consumption\GetResumePoint.http
|
||||
..\docs\technical\requests\media-consumption\ReportConsumptionProgress.http = ..\docs\technical\requests\media-consumption\ReportConsumptionProgress.http
|
||||
..\docs\technical\requests\media-consumption\StartConsumption.http = ..\docs\technical\requests\media-consumption\StartConsumption.http
|
||||
..\docs\technical\requests\media-consumption\StopConsumption.http = ..\docs\technical\requests\media-consumption\StopConsumption.http
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "file-system-management", "file-system-management", "{6D5C6E19-67A1-4327-A958-1EC0DA6E9D7D}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "directories", "directories", "{B6A1AB2C-766F-4DC2-894A-4DF1FE102AED}"
|
||||
@@ -1404,6 +1419,7 @@ Global
|
||||
{459EF701-AB73-40ED-81E6-144D3587C667} = {D78A725D-C3AE-420B-ABA8-255A000D7324}
|
||||
{08113205-695B-4DA4-8355-6C5377BBD8C0} = {459EF701-AB73-40ED-81E6-144D3587C667}
|
||||
{A841E8D2-F3C9-403D-BAE1-A087B8040C61} = {D78A725D-C3AE-420B-ABA8-255A000D7324}
|
||||
{A3E6B1C4-7D2F-4A98-8B5C-1F0D3E6A9C42} = {D78A725D-C3AE-420B-ABA8-255A000D7324}
|
||||
{2FA735A6-2141-44AB-B34C-F65E4195B1EB} = {D78A725D-C3AE-420B-ABA8-255A000D7324}
|
||||
{6A5D3BED-AE4A-4D26-B12A-5DE996152A64} = {D78A725D-C3AE-420B-ABA8-255A000D7324}
|
||||
{252D7F8D-991F-4C62-9BB1-D13988D7B4E2} = {6A5D3BED-AE4A-4D26-B12A-5DE996152A64}
|
||||
@@ -1426,6 +1442,7 @@ Global
|
||||
{7DC07C5C-4877-45DF-86C2-D93069EA7608} = {A9BD996E-E8E8-4C34-B792-5E6A00BD5938}
|
||||
{3BCFBE37-A64D-4F3C-9D18-E37E89CB490F} = {A9BD996E-E8E8-4C34-B792-5E6A00BD5938}
|
||||
{8D16AB83-DE2B-4527-AFFC-9F3292AC4CA9} = {B45637D3-840C-4898-A047-C6FB568FFFE6}
|
||||
{B4F7C2D5-8E3A-4B69-9C6D-2A1E4F7B0D53} = {B45637D3-840C-4898-A047-C6FB568FFFE6}
|
||||
{6D5C6E19-67A1-4327-A958-1EC0DA6E9D7D} = {B45637D3-840C-4898-A047-C6FB568FFFE6}
|
||||
{B6A1AB2C-766F-4DC2-894A-4DF1FE102AED} = {6D5C6E19-67A1-4327-A958-1EC0DA6E9D7D}
|
||||
{82D80C8B-593E-4E54-BDC2-26931EA3E2D6} = {6D5C6E19-67A1-4327-A958-1EC0DA6E9D7D}
|
||||
|
||||
Reference in new issue
Block a user