2025-08-08 16:59:29 -05:00
|
|
|
using FreeTubeSync.Model;
|
2025-07-19 04:02:09 -05:00
|
|
|
|
|
|
|
|
namespace FreeTubeSync.EndPoints;
|
|
|
|
|
|
|
|
|
|
public static class PlaylistEndpoint
|
|
|
|
|
{
|
|
|
|
|
public static void MapPlaylistEndpoints(this WebApplication app)
|
|
|
|
|
{
|
|
|
|
|
var group = app.MapGroup("playlists");
|
|
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
group.MapGet("/", GetAllPlaylists);
|
|
|
|
|
group.MapGet("/{id}", GetPlaylist);
|
|
|
|
|
group.MapPost("/", UpdatePlaylist);
|
|
|
|
|
group.MapDelete("/{id}", RemovePlaylist);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
private static async Task<IResult> GetAllPlaylists(IRepository<Playlist> repository, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var results = await repository.GetAllAsync(ct);
|
|
|
|
|
return Results.Ok(results);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<IResult> GetPlaylist(IRepository<Playlist> repository, string id, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var result = await repository.GetByIdAsync(id, ct);
|
|
|
|
|
return result == null ? Results.NotFound() : Results.Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<IResult> UpdatePlaylist(IRepository<Playlist> repository, Playlist playlist, CancellationToken ct,
|
|
|
|
|
ILogger<Program> logger)
|
|
|
|
|
{
|
|
|
|
|
var results = await repository.GetByIdAsync(playlist._id, ct);
|
|
|
|
|
if (results == null)
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
try
|
2025-07-22 17:03:33 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
await repository.AddAsync(playlist, ct);
|
2025-07-22 17:03:33 -05:00
|
|
|
}
|
2025-08-08 16:59:29 -05:00
|
|
|
catch (Exception ex)
|
2025-07-21 17:10:42 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
logger.LogError(ex, "Failed to create Playlist {playlist}", playlist._id);
|
|
|
|
|
return Results.StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Add Update Code here
|
|
|
|
|
var toRemove = results.videos.Where(vid => !playlist.videos.Any(x => x.playlistItemId == vid.playlistItemId)).ToList();
|
2025-07-22 17:03:33 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
results.lastUpdatedAt = playlist.lastUpdatedAt;
|
|
|
|
|
results.@protected = playlist.@protected;
|
|
|
|
|
results.playlistName = playlist.playlistName;
|
|
|
|
|
results.createdAt = playlist.createdAt;
|
2025-08-04 19:13:17 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
foreach (var vid in toRemove)
|
|
|
|
|
results.videos.Remove(vid);
|
2025-07-22 17:03:33 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
foreach (var vid in playlist.videos)
|
|
|
|
|
{
|
|
|
|
|
var ovid = results.videos.FirstOrDefault(x => x.playlistItemId == vid.playlistItemId);
|
|
|
|
|
if (ovid == null)
|
|
|
|
|
results.videos.Add(vid);
|
|
|
|
|
else
|
2025-08-04 19:13:17 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
ovid.videoId = vid.videoId;
|
|
|
|
|
ovid.title = vid.title;
|
|
|
|
|
ovid.author = vid.author;
|
|
|
|
|
ovid.authorId = vid.authorId;
|
|
|
|
|
ovid.lengthSeconds = vid.lengthSeconds;
|
|
|
|
|
ovid.published = vid.published;
|
|
|
|
|
ovid.timeAdded = vid.timeAdded;
|
|
|
|
|
ovid.playlistItemId = vid.playlistItemId;
|
|
|
|
|
ovid.type = vid.type;
|
2025-08-04 19:13:17 -05:00
|
|
|
}
|
2025-07-21 17:10:42 -05:00
|
|
|
}
|
2025-08-08 16:59:29 -05:00
|
|
|
await repository.UpdateAsync(results, ct);
|
|
|
|
|
}
|
2025-07-21 17:10:42 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
return Results.Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<IResult> RemovePlaylist(IRepository<Playlist> repository, string id, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var result = await repository.GetByIdAsync(id, ct);
|
|
|
|
|
if (result == null) return Results.NotFound();
|
|
|
|
|
await repository.DeleteAsync(result, ct);
|
|
|
|
|
return Results.Ok();
|
2025-07-19 04:02:09 -05:00
|
|
|
}
|
|
|
|
|
}
|