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:
@@ -10,17 +10,13 @@ namespace Musify.Application.Tracks.Responses
|
||||
DateTime CreatedAt,
|
||||
DateTime UpdatedAt)
|
||||
{
|
||||
public static TrackApplicationResponse FromEntity(Track track)
|
||||
{
|
||||
var count = track.ListeningHistories.Count(x => x.TrackId == track.Id);
|
||||
|
||||
return new TrackApplicationResponse(
|
||||
public static TrackApplicationResponse FromEntity(Track track, int listensCount) =>
|
||||
new(
|
||||
track.Id,
|
||||
track.Title,
|
||||
track.Duration,
|
||||
count,
|
||||
listensCount,
|
||||
track.CreatedAt,
|
||||
track.UpdatedAt);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user