feat(backend): expose the first cover track ids of playlists and albums

This commit is contained in:
2026-07-21 18:13:18 +02:00
parent 496a5f4a0b
commit 78cc2f7248
11 changed files with 125 additions and 29 deletions
@@ -14,16 +14,24 @@ namespace Musify.Application.PlayLists
{
public async ValueTask<ErrorOr<PlayListApplicationResponse>> Handle(GetPlayListByIdQuery request, CancellationToken cancellationToken)
{
var playList = await database.PlayLists
var entry = await database.PlayLists
.AsNoTracking()
.Where(p => p.Id == request.Id)
.Select(p => PlayListApplicationResponse.FromEntity(p))
.Select(p => new
{
PlayList = p,
CoverTrackIds = p.PlayListTracks
.OrderBy(plt => plt.Position)
.Take(PlayListApplicationResponse.CoverTrackCount)
.Select(plt => plt.TrackId)
.ToList()
})
.SingleOrDefaultAsync(cancellationToken);
if (playList is null)
if (entry is null)
return Error.NotFound();
return playList;
return PlayListApplicationResponse.FromEntity(entry.PlayList, entry.CoverTrackIds);
}
}
}
@@ -2,6 +2,7 @@ using ErrorOr;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Shared;
using Musify.Application.PlayLists.Responses;
using X.PagedList;
using X.PagedList.EF;
using Mediator;
using Musify.Application.Contracts;
@@ -18,12 +19,24 @@ namespace Musify.Application.PlayLists
{
var totalCount = await database.PlayLists.CountAsync(cancellationToken);
var pagedPlayLists = await database.PlayLists
var pagedEntities = await database.PlayLists
.AsNoTracking()
.OrderBy(p => p.Id)
.Select(p => PlayListApplicationResponse.FromEntity(p))
.Select(p => new
{
PlayList = p,
CoverTrackIds = p.PlayListTracks
.OrderBy(plt => plt.Position)
.Take(PlayListApplicationResponse.CoverTrackCount)
.Select(plt => plt.TrackId)
.ToList()
})
.ToPagedListAsync(request.PageNumber, request.PageSize, totalCount, cancellationToken);
var pagedPlayLists = new StaticPagedList<PlayListApplicationResponse>(
pagedEntities.Select(entry => PlayListApplicationResponse.FromEntity(entry.PlayList, entry.CoverTrackIds)),
pagedEntities.PageNumber, pagedEntities.PageSize, pagedEntities.TotalItemCount);
return PaginatedResponse<PlayListApplicationResponse>.FromPagedList(pagedPlayLists);
}
}
@@ -2,6 +2,7 @@ using ErrorOr;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Shared;
using Musify.Application.PlayLists.Responses;
using X.PagedList;
using X.PagedList.EF;
using Mediator;
using Musify.Application.Contracts;
@@ -32,11 +33,23 @@ namespace Musify.Application.PlayLists
var totalCount = await playListsQuery.CountAsync(cancellationToken);
var pagedPlayLists = await playListsQuery
var pagedEntities = await playListsQuery
.OrderBy(p => p.CreatedAt)
.Select(p => PlayListApplicationResponse.FromEntity(p))
.Select(p => new
{
PlayList = p,
CoverTrackIds = p.PlayListTracks
.OrderBy(plt => plt.Position)
.Take(PlayListApplicationResponse.CoverTrackCount)
.Select(plt => plt.TrackId)
.ToList()
})
.ToPagedListAsync(request.PageNumber, request.PageSize, totalCount, cancellationToken);
var pagedPlayLists = new StaticPagedList<PlayListApplicationResponse>(
pagedEntities.Select(entry => PlayListApplicationResponse.FromEntity(entry.PlayList, entry.CoverTrackIds)),
pagedEntities.PageNumber, pagedEntities.PageSize, pagedEntities.TotalItemCount);
return PaginatedResponse<PlayListApplicationResponse>.FromPagedList(pagedPlayLists);
}
}
@@ -1,4 +1,4 @@
using Musify.Domain.Entities;
using Musify.Domain.Entities;
namespace Musify.Application.PlayLists.Responses
{
@@ -10,9 +10,14 @@ namespace Musify.Application.PlayLists.Responses
string? MediumImageKeyName,
string? LargeImageKeyName,
DateTime CreatedAt,
DateTime UpdatedAt)
DateTime UpdatedAt,
IReadOnlyList<Guid> CoverTrackIds)
{
public static PlayListApplicationResponse FromEntity(PlayList playList)
public const int CoverTrackCount = 4;
public static PlayListApplicationResponse FromEntity(
PlayList playList,
IReadOnlyList<Guid>? coverTrackIds = null)
{
return new PlayListApplicationResponse(
playList.Id,
@@ -22,7 +27,8 @@ namespace Musify.Application.PlayLists.Responses
playList.Pictures?.MediumName,
playList.Pictures?.LargeName,
playList.CreatedAt,
playList.UpdatedAt);
playList.UpdatedAt,
coverTrackIds ?? []);
}
}
}
}
@@ -93,8 +93,16 @@ namespace Musify.Application.PlayLists
return Error.Failure(description: $"Failed to update playlist {playListEntity.Id}");
}
var coverTrackIds = await database.PlayListHasTracks
.AsNoTracking()
.Where(plt => plt.PlayListId == playListEntity.Id)
.OrderBy(plt => plt.Position)
.Take(PlayListApplicationResponse.CoverTrackCount)
.Select(plt => plt.TrackId)
.ToListAsync(cancellationToken);
logger.LogInformation("Updated playlist {PlayListId}", playListEntity.Id);
return PlayListApplicationResponse.FromEntity(playListEntity);
return PlayListApplicationResponse.FromEntity(playListEntity, coverTrackIds);
}
async Task<ErrorOr<Success>> PublishUpdatePlayListPictureSourceEventAsync(