Some updates to Endpoint Logic

Ensure that we are adding videos to the playlist database object when we insert them.
Updated logic to follow profiles for playlist.
Do not search for existing subs when inserting a new sub, the sub's belongs to id will be different.
Formatted linq statement to be multi-line, instead of single line, to make it easier to read.
Removed check for subscription if we are adding a new one, the profile id is going to be different.
Ensure we are deleting subs from the database when they are no longer associated with a profile.
This commit is contained in:
Mario Steele 2025-08-04 19:13:17 -05:00
parent f34ad94ea6
commit 8187a03419
2 changed files with 39 additions and 27 deletions

View file

@ -33,6 +33,8 @@ public static class PlaylistEndpoint
var vid = new Video();
vid.MapFrom(video);
await vidRepo.AddAsync(vid, ct, false);
results.videos.Add(vid);
}
await repository.AddAsync(results, ct);
}
@ -40,24 +42,37 @@ public static class PlaylistEndpoint
{
results.MapFrom(playlistJson);
var remove = results.videos.Where(video => playlistJson.videos.All(x => x.playlistItemId != video.playlistItemId)).ToList();
var add = playlistJson.videos.Where(video => results.videos.All(x => x.playlistItemId != video.playlistItemId)).ToList();
var vids = new List<Video>();
foreach (var video in remove)
var notFound = new List<Video>();
foreach (var video in results.videos)
{
await vidRepo.DeleteAsync(video, ct, false);
var f = playlistJson.videos.FirstOrDefault(v => video.playlistItemId == v.playlistItemId);
if (f == null)
notFound.Add(video);
else
{
video.MapFrom(f);
await vidRepo.UpdateAsync(video, ct, false);
}
}
foreach (var video in add)
var newVids = (from video in playlistJson.videos
let f = results.videos.FirstOrDefault(v => v.playlistItemId == video.playlistItemId)
where f == null
select video).ToList();
foreach (var newVid in newVids)
{
var vid = new Video();
vid.MapFrom(video);
await vidRepo.AddAsync(vid, ct, false);
vids.Add(vid);
var vres = new Video();
vres.MapFrom(newVid);
await vidRepo.AddAsync(vres, ct, false);
results.videos.Add(vres);
}
results.videos.RemoveAll(x => remove.Contains(x));
results.videos.AddRange(vids);
foreach (var nfVid in notFound)
{
await vidRepo.DeleteAsync(nfVid, ct, false);
results.videos.Remove(nfVid);
}
await repository.UpdateAsync(results, ct);
}

View file

@ -29,13 +29,9 @@ public static class ProfileEndpoint
res.MapFrom(profileJson);
foreach (var subscription in profileJson.subscriptions)
{
var sub = await subRepo.GetByIdAsync(subscription.id, ct);
if (sub == null)
{
sub = new Subscription();
sub.MapFrom(subscription);
await subRepo.AddAsync(sub, ct, false);
}
var sub = new Subscription();
sub.MapFrom(subscription);
await subRepo.AddAsync(sub, ct, false);
res.subscriptions.Add(sub);
}
@ -56,23 +52,24 @@ public static class ProfileEndpoint
await subRepo.UpdateAsync(subscription, ct, false);
}
}
var newSubs = (from subscription in profileJson.subscriptions let f = res.subscriptions.FirstOrDefault(s => s.id == subscription.id) where f == null select subscription).ToList();
var newSubs = (from subscription in profileJson.subscriptions
let f = res.subscriptions.FirstOrDefault(s => s.id == subscription.id)
where f == null
select subscription).ToList();
foreach (var newSub in newSubs)
{
var sres = await subRepo.GetByIdAsync(newSub.id, ct);
if (sres == null)
{
sres = new Subscription();
sres.MapFrom(newSub);
await subRepo.AddAsync(sres, ct, false);
}
var sres = new Subscription();
sres.MapFrom(newSub);
await subRepo.AddAsync(sres, ct, false);
res.subscriptions.Add(sres);
}
foreach (var nfSub in notFound)
{
await subRepo.DeleteAsync(nfSub, ct, false);
res.subscriptions.Remove(nfSub);
}