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.
91 lines
No EOL
3.2 KiB
C#
91 lines
No EOL
3.2 KiB
C#
using FreeTubeSync.Database;
|
|
using FreeTubeSync.Model.Database;
|
|
using FreeTubeSync.Model.Json;
|
|
|
|
namespace FreeTubeSync.EndPoints;
|
|
|
|
public static class PlaylistEndpoint
|
|
{
|
|
public static void MapPlaylistEndpoints(this WebApplication app)
|
|
{
|
|
var group = app.MapGroup("playlists");
|
|
|
|
group.MapGet("/", async (IRepository<Playlist> repository, CancellationToken ct) =>
|
|
{
|
|
var results = (await repository.GetAllAsync(ct)).ToList();
|
|
var jsonResults = new List<PlaylistJson>();
|
|
results.MapTo(jsonResults);
|
|
for (var i = 0; i < jsonResults.Count; i++)
|
|
results[i].videos.MapTo(jsonResults[i].videos);
|
|
|
|
return Results.Ok(jsonResults);
|
|
});
|
|
|
|
group.MapPost("/", async (IRepository<Playlist> repository, IRepository<Video> vidRepo, CancellationToken ct, PlaylistJson playlistJson) =>
|
|
{
|
|
var results = await repository.GetByIdAsync(playlistJson._id, ct);
|
|
if (results == null)
|
|
{
|
|
results = new Playlist();
|
|
results.MapFrom(playlistJson);
|
|
foreach (var video in playlistJson.videos)
|
|
{
|
|
var vid = new Video();
|
|
vid.MapFrom(video);
|
|
await vidRepo.AddAsync(vid, ct, false);
|
|
|
|
results.videos.Add(vid);
|
|
}
|
|
await repository.AddAsync(results, ct);
|
|
}
|
|
else
|
|
{
|
|
results.MapFrom(playlistJson);
|
|
|
|
var notFound = new List<Video>();
|
|
foreach (var video in results.videos)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
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 vres = new Video();
|
|
vres.MapFrom(newVid);
|
|
await vidRepo.AddAsync(vres, ct, false);
|
|
results.videos.Add(vres);
|
|
}
|
|
|
|
foreach (var nfVid in notFound)
|
|
{
|
|
await vidRepo.DeleteAsync(nfVid, ct, false);
|
|
results.videos.Remove(nfVid);
|
|
}
|
|
|
|
await repository.UpdateAsync(results, ct);
|
|
}
|
|
|
|
return Results.Ok();
|
|
});
|
|
|
|
group.MapDelete("/{id}", async (IRepository<Playlist> repository, CancellationToken ct, string id) =>
|
|
{
|
|
var result = await repository.GetByIdAsync(id, ct);
|
|
if (result == null) return Results.NotFound();
|
|
await repository.DeleteAsync(result, ct);
|
|
return Results.Ok();
|
|
});
|
|
}
|
|
} |