Split Json data models coming from REST Api, from the Database models storing them in a SQLite database. Work to re-engineer endpoints to use Database objects, and copy/update data from the json objects. More work is needed.
76 lines
No EOL
2.8 KiB
C#
76 lines
No EOL
2.8 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);
|
|
}
|
|
await repository.AddAsync(results, ct);
|
|
}
|
|
else
|
|
{
|
|
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)
|
|
{
|
|
await vidRepo.DeleteAsync(video, ct, false);
|
|
}
|
|
|
|
foreach (var video in add)
|
|
{
|
|
var vid = new Video();
|
|
vid.MapFrom(video);
|
|
await vidRepo.AddAsync(vid, ct, false);
|
|
vids.Add(vid);
|
|
}
|
|
|
|
results.videos.RemoveAll(x => remove.Contains(x));
|
|
results.videos.AddRange(vids);
|
|
|
|
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();
|
|
});
|
|
}
|
|
} |