Files
Musify/backend/Core/Musify.Application/Albums/Responses/AlbumApplicationResponse.cs
T
Admin 7ae6c0a7a2 Split AlbumApplicationResponse into a Local/External hierarchy
AlbumApplicationResponse used to carry YouTubeAlbumId/ThumbnailUrl as
two always-present-but-usually-null fields for the sake of a single
flat shape. Since one of these fields IS genuinely exclusive to
external albums, an inheritance split reads better than the flattened
shape: AlbumApplicationResponse now holds only the fields shared by
every album, and ExternalAlbumApplicationResponse adds ExternalId
(renamed from YouTubeAlbumId, matching the source-generalized naming
used everywhere else) and ThumbnailUrl.

[JsonPolymorphic]/[JsonDerivedType] are required here, not optional:
without them System.Text.Json serializes strictly by the declared CLR
type at each call site (both the bare return type and nested inside
AlbumSearchItemResponse.Album), so an ExternalAlbumApplicationResponse
instance would silently lose its extra fields on the wire. This also
gives every response a "source" discriminator field for free,
consistent with AlbumSearchItemResponse.Source, and the generated
OpenAPI schema now models it as a proper oneOf/discriminator instead
of nullable fields.
2026-09-10 19:19:31 +02:00

65 lines
2.5 KiB
C#

using System.Text.Json.Serialization;
using Musify.Domain.Entities;
namespace Musify.Application.Albums.Responses
{
// [JsonPolymorphic]/[JsonDerivedType] are required here, not optional: without them
// System.Text.Json serializes strictly by the declared CLR type at each call site
// (AlbumApplicationResponse, both as a bare return type and nested inside
// AlbumSearchItemResponse.Album), so an ExternalAlbumApplicationResponse instance would
// silently lose ExternalId/ThumbnailUrl on the wire. This also gives every response a
// "source" discriminator field for free, matching AlbumSearchItemResponse.Source.
[JsonPolymorphic(TypeDiscriminatorPropertyName = "source")]
[JsonDerivedType(typeof(AlbumApplicationResponse), typeDiscriminator: "Local")]
[JsonDerivedType(typeof(ExternalAlbumApplicationResponse), typeDiscriminator: "YouTube")]
public record AlbumApplicationResponse(
Guid Id,
string Title,
string? Description,
int? ReleaseYear,
long OwnerUserId,
int TrackCount,
string? SmallImageKeyName,
string? MediumImageKeyName,
string? LargeImageKeyName,
DateTime CreatedAt,
DateTime UpdatedAt,
IReadOnlyList<Guid> CoverTrackIds)
{
public const int CoverTrackCount = 4;
public static AlbumApplicationResponse FromEntity(
UserAlbum album,
int trackCount,
IReadOnlyList<Guid>? coverTrackIds = null)
{
return new AlbumApplicationResponse(
album.Id,
album.Title,
album.Description,
album.ReleaseYear,
album.OwnerUserId,
trackCount,
album.Pictures?.SmallName,
album.Pictures?.MediumName,
album.Pictures?.LargeName,
album.CreatedAt,
album.UpdatedAt,
coverTrackIds ?? []);
}
public static AlbumApplicationResponse FromEntity(
Album album,
int trackCount,
IReadOnlyList<Guid>? coverTrackIds = null)
{
return album switch
{
ExternalAlbum externalAlbum => ExternalAlbumApplicationResponse.FromEntity(externalAlbum, trackCount, coverTrackIds),
UserAlbum userAlbum => FromEntity(userAlbum, trackCount, coverTrackIds),
_ => throw new NotSupportedException($"Unsupported album entity type '{album.GetType().Name}'.")
};
}
}
}