refactor(backend): unify YouTube track creation via a provisioner

Track get-or-create for YouTube videos was duplicated between the
old GetYouTubeTrackStream query handler (which also wrote to the
database) and AddYouTubeTrackToPlayList, with frontend-supplied
metadata (title/artist/duration/thumbnail) trusted at add-to-playlist
time instead of resolving it server-side.

Extract YouTubeTrackProvisioner as the single get-or-create path,
resolving structured artist refs (id + name) from YouTube Music so
multi-artist songs map to distinct Artist rows. Rename
GetYouTubeTrackStream to ResolveYouTubeTrackStream (it writes, so it
is a command, not a query) and delegate to the provisioner.
AddYouTubeTrackToPlayList slims down to (UserId, PlayListId, VideoId)
since all metadata now comes from the provisioner/YouTube API.
This commit is contained in:
2026-07-17 17:56:37 +02:00
parent cdf89dc632
commit ca275fe7a8
11 changed files with 271 additions and 224 deletions
@@ -1,8 +1,3 @@
namespace Musify.Api.DataTransferObjects.PlayLists;
public record AddYouTubeTrackRequest(
string VideoId,
string Title,
string Artist,
int DurationSeconds,
string ThumbnailUrl);
public record AddYouTubeTrackRequest(string VideoId);
@@ -254,11 +254,7 @@ public static class PlayListEndpoints
new AddYouTubeTrackToPlayListCommand(
currentUser.RequiredId,
playlistId,
request.VideoId,
request.Title,
request.Artist,
request.DurationSeconds,
request.ThumbnailUrl),
request.VideoId),
cancellationToken);
return result.ToCreatedResult(track => $"/tracks/{track.Id}");
@@ -40,7 +40,7 @@ public static class YouTubeEndpoints
CancellationToken cancellationToken)
{
var result = await mediator.Send(
new GetYouTubeTrackStreamQuery(videoId, currentUser.RequiredId),
new ResolveYouTubeTrackStreamCommand(videoId, currentUser.RequiredId),
cancellationToken);
return result.ToHttpResult();
@@ -10,20 +10,5 @@ public sealed class AddYouTubeTrackRequestValidator : AbstractValidator<AddYouTu
RuleFor(x => x.VideoId)
.NotEmpty()
.Matches("^[A-Za-z0-9_-]{11}$");
RuleFor(x => x.Title)
.NotEmpty()
.MaximumLength(200);
RuleFor(x => x.Artist)
.MaximumLength(400);
RuleFor(x => x.DurationSeconds)
.GreaterThan(0);
RuleFor(x => x.ThumbnailUrl)
.NotEmpty()
.Must(url => Uri.TryCreate(url, UriKind.Absolute, out var uri) && uri.Scheme == Uri.UriSchemeHttps)
.WithMessage("ThumbnailUrl must be an absolute https URL.");
}
}