Possibly fix issues
After doing testing with a test project, re-organized code to new method of storing, as well as ensure that all Endpoint lambda's are separated out into functions. Large commit, will be testing.
This commit is contained in:
parent
2e4f644c17
commit
73955353cb
23 changed files with 314 additions and 352 deletions
|
|
@ -1,6 +1,4 @@
|
|||
using FreeTubeSync.Database;
|
||||
using FreeTubeSync.Model.Database;
|
||||
using FreeTubeSync.Model.Json;
|
||||
using FreeTubeSync.Model;
|
||||
|
||||
namespace FreeTubeSync.EndPoints;
|
||||
|
||||
|
|
@ -10,78 +8,82 @@ public static class PlaylistEndpoint
|
|||
{
|
||||
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.MapGet("/", GetAllPlaylists);
|
||||
group.MapGet("/{id}", GetPlaylist);
|
||||
group.MapPost("/", UpdatePlaylist);
|
||||
group.MapDelete("/{id}", RemovePlaylist);
|
||||
}
|
||||
|
||||
group.MapPost("/", async (IRepository<Playlist> repository, IRepository<Video> vidRepo, CancellationToken ct, PlaylistJson playlistJson) =>
|
||||
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)
|
||||
{
|
||||
var results = await repository.GetByIdAsync(playlistJson._id, ct);
|
||||
if (results == null)
|
||||
try
|
||||
{
|
||||
results = new Playlist();
|
||||
results.MapFrom(playlistJson);
|
||||
foreach (var video in playlistJson.videos)
|
||||
{
|
||||
var vid = new Video();
|
||||
vid.MapFrom(video);
|
||||
await repository.AddAsync(playlist, ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
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();
|
||||
|
||||
results.lastUpdatedAt = playlist.lastUpdatedAt;
|
||||
results.@protected = playlist.@protected;
|
||||
results.playlistName = playlist.playlistName;
|
||||
results.createdAt = playlist.createdAt;
|
||||
|
||||
foreach (var vid in toRemove)
|
||||
results.videos.Remove(vid);
|
||||
|
||||
foreach (var vid in playlist.videos)
|
||||
{
|
||||
var ovid = results.videos.FirstOrDefault(x => x.playlistItemId == vid.playlistItemId);
|
||||
if (ovid == null)
|
||||
results.videos.Add(vid);
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
await repository.AddAsync(results, ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
results.MapFrom(playlistJson);
|
||||
await repository.UpdateAsync(results, ct);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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();
|
||||
});
|
||||
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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue