fix(backend): compute track listens count via SQL instead of an unloaded navigation
TrackApplicationResponse.FromEntity counted track.ListeningHistories, but none of the queries that built a response ever Included it, so the collection was always empty and listensCount was always 0. Each query now projects the count as a correlated subquery before mapping to the response.
This commit is contained in:
@@ -14,15 +14,15 @@ namespace Musify.Application.Tracks
|
||||
{
|
||||
public async ValueTask<ErrorOr<TrackApplicationResponse>> Handle(GetTrackByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var track = await database.Tracks.AsNoTracking()
|
||||
var entity = await database.Tracks.AsNoTracking()
|
||||
.Where(t => t.Id == request.TrackId)
|
||||
.Select(t => TrackApplicationResponse.FromEntity(t))
|
||||
.Select(t => new { Track = t, ListensCount = t.ListeningHistories.Count })
|
||||
.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (track is null)
|
||||
if (entity is null)
|
||||
return Error.NotFound();
|
||||
|
||||
return track;
|
||||
return TrackApplicationResponse.FromEntity(entity.Track, entity.ListensCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user