feat(tracks): add user listening history
Record a listening-history entry when a stream ticket is issued and expose GET /users/{id}/listening-history to read it back.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using Mediator;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Tracks.Responses;
|
||||
|
||||
namespace Musify.Application.Tracks
|
||||
{
|
||||
public record GetListeningHistoryQuery(long UserId) : IQuery<IEnumerable<TrackApplicationResponse>>;
|
||||
|
||||
public class GetListeningHistoryQueryHandler(
|
||||
IDatabase database)
|
||||
: IQueryHandler<GetListeningHistoryQuery, IEnumerable<TrackApplicationResponse>>
|
||||
{
|
||||
public ValueTask<IEnumerable<TrackApplicationResponse>> Handle(GetListeningHistoryQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
var listeningHistory = database.ListeningHistories
|
||||
.Where(l => l.UserId == query.UserId)
|
||||
.Select(l => l.TrackId)
|
||||
.ToList();
|
||||
|
||||
var tracks = database.Tracks
|
||||
.Where(t => listeningHistory.Contains(t.Id))
|
||||
.Select(TrackApplicationResponse.FromEntity)
|
||||
.AsEnumerable();
|
||||
|
||||
return ValueTask.FromResult(tracks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Tracks.Responses;
|
||||
using Musify.Domain.Entities;
|
||||
using Musify.Domain.ValueObjects;
|
||||
|
||||
namespace Musify.Application.Tracks
|
||||
@@ -27,7 +28,7 @@ namespace Musify.Application.Tracks
|
||||
.Select(t => new { t.Id, t.AudioTranscodeProcessingStatus, t.AudioFolderName } )
|
||||
.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (track is null)
|
||||
if (track == null)
|
||||
return Error.NotFound();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(track.AudioFolderName)
|
||||
@@ -50,6 +51,16 @@ namespace Musify.Application.Tracks
|
||||
|
||||
logger.LogInformation("Issued stream ticket for track {TrackId} to user {UserId}", request.TrackId, request.UserId);
|
||||
|
||||
var newListeningHistory = new ListeningHistory
|
||||
{
|
||||
UserId = request.UserId,
|
||||
TrackId = request.TrackId,
|
||||
};
|
||||
await database.ListeningHistories.AddAsync(newListeningHistory, cancellationToken);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogDebug("Listening history added for track {TrackId} by user {UserId}", request.TrackId, request.UserId);
|
||||
|
||||
return new TrackStreamResponse(manifestUrl, ticket.Token, ticket.ExpiresInSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user