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:
Mario Steele 2025-08-08 16:59:29 -05:00
parent 2e4f644c17
commit 73955353cb
23 changed files with 314 additions and 352 deletions

View file

@ -1,6 +1,4 @@
using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
using FreeTubeSync.Model.Json;
using FreeTubeSync.Model;
namespace FreeTubeSync.EndPoints;
@ -10,37 +8,58 @@ public static class HistoryEndpoint
{
var group = app.MapGroup("history");
group.MapGet("/", async (IRepository<History> repository, CancellationToken ct) =>
{
var results = await repository.GetAllAsync(ct);
var jsonResults = new List<HistoryJson>();
results.MapTo(jsonResults);
return Results.Ok(jsonResults);
});
group.MapGet("/", GetAllHistory);
group.MapGet("/{id}", GetHistory);
group.MapPost("/", UpdateHistory);
group.MapDelete("/{id}", DeleteHistory);
}
private static async Task<IResult> GetAllHistory(IRepository<History> repository, CancellationToken ct)
{
var results = await repository.GetAllAsync(ct);
return Results.Ok(results);
}
group.MapPost("/", async (IRepository<History> repository, CancellationToken ct, HistoryJson historyJson) =>
private static async Task<IResult> GetHistory(IRepository<History> 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> UpdateHistory(IRepository<History> repository, History history, CancellationToken ct, ILogger<Program> logger)
{
var results = await repository.GetByIdAsync(history._id, ct);
if (results == null)
{
var results = await repository.GetByIdAsync(historyJson._id, ct);
if (results == null)
try
{
results = new History();
results.MapFrom(historyJson);
await repository.AddAsync(results, ct);
await repository.AddAsync(history, ct);
}
else
catch (Exception ex)
{
results.MapFrom(historyJson);
await repository.UpdateAsync(results, ct);
logger.LogError(ex, "Failed to create history {json}", history._id);
return Results.StatusCode(500);
}
return Results.Ok();
});
}
else
{
try
{
await repository.UpdateAsync(history, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to update history {json}", history._id);
return Results.StatusCode(500);
}
}
return Results.Ok();
}
group.MapDelete("/{id}", async (IRepository<History> 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();
});
private static async Task<IResult> DeleteHistory(IRepository<History> 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();
}
}

View file

@ -1,5 +1,4 @@
using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
namespace FreeTubeSync.EndPoints;

View file

@ -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();
}
}

View file

@ -1,6 +1,4 @@
using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
using FreeTubeSync.Model.Json;
using FreeTubeSync.Model;
namespace FreeTubeSync.EndPoints;
@ -10,78 +8,73 @@ public static class ProfileEndpoint
{
var group = app.MapGroup("profile");
group.MapGet("/", async (IRepository<Profile> repository, CancellationToken ct) =>
{
var results = (await repository.GetAllAsync(ct)).ToList();
var jsonResults = new List<ProfileJson>();
results.MapTo(jsonResults);
for (var i = 0; i < jsonResults.Count; i++)
results[i].subscriptions.MapTo(jsonResults[i].subscriptions);
return Results.Ok(results);
});
group.MapGet("/", GetAllProfiles);
group.MapGet("/{id}", GetProfile);
group.MapPost("/", UpdateProfile);
group.MapDelete("/{id}", DeleteProfile);
}
group.MapPost("/", async (IRepository<Profile> repository, IRepository<Subscription> subRepo, CancellationToken ct, ProfileJson profileJson) =>
private static async Task<IResult> GetAllProfiles(IRepository<Profile> repository, CancellationToken ct)
{
var results = await repository.GetAllAsync(ct);
return Results.Ok(results);
}
private static async Task<IResult> GetProfile(IRepository<Profile> 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> UpdateProfile(IRepository<Profile> repository, Profile profile, CancellationToken ct, ILogger<Program> logger)
{
var res = await repository.GetByIdAsync(profile._id, ct);
if (res == null)
{
var res = await repository.GetByIdAsync(profileJson._id, ct);
if (res == null)
try
{
res = new Profile();
res.MapFrom(profileJson);
await repository.AddAsync(res, ct);
foreach (var subscription in profileJson.subscriptions)
{
var sub = new Subscription();
sub.MapFrom(subscription);
await repository.AddAsync(profile, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to create profile {json}", profile._id);
return Results.StatusCode(500);
}
}
else
{
var toRemove = res.subscriptions.Where(sub => !profile.subscriptions.Any(x => x.id == sub.id)).ToList();
res.name = profile.name;
res.textColor = profile.textColor;
res.bgColor = profile.bgColor;
foreach (var sub in toRemove)
res.subscriptions.Remove(sub);
foreach (var sub in profile.subscriptions)
{
var osub = res.subscriptions.FirstOrDefault(x => x.id == sub.id);
if (osub == null)
res.subscriptions.Add(sub);
}
await repository.AddAsync(res, ct);
}
else
{
res.MapFrom(profileJson);
var notFound = new List<Subscription>();
foreach (var subscription in res.subscriptions)
else
{
var f = profileJson.subscriptions.FirstOrDefault(s => s.id == subscription.id);
if (f == null)
notFound.Add(subscription);
else
{
subscription.MapFrom(f);
}
osub.name = sub.name;
osub.thumbnail = sub.thumbnail;
}
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 = new Subscription();
sres.MapFrom(newSub);
res.subscriptions.Add(sres);
}
foreach (var nfSub in notFound)
{
await subRepo.DeleteAsync(nfSub, ct, false);
res.subscriptions.Remove(nfSub);
}
await repository.UpdateAsync(res, ct);
}
return Results.Ok();
});
await repository.UpdateAsync(res, ct);
}
group.MapDelete("/{id}", async (IRepository<Profile> 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> DeleteProfile(IRepository<Profile> 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();
}
}

View file

@ -1,6 +1,4 @@
using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
using FreeTubeSync.Model.Json;
using FreeTubeSync.Model;
namespace FreeTubeSync.EndPoints;
@ -10,39 +8,61 @@ public static class SearchHistoryEndpoint
{
var group = app.MapGroup("searchHistory");
group.MapGet("/", async (IRepository<SearchHistory> repository, CancellationToken ct) =>
{
var result = await repository.GetAllAsync(ct);
var jsonResults = new List<SearchHistoryJson>();
result.MapTo(jsonResults);
return Results.Ok(jsonResults);
});
group.MapGet("/", GetAllSearchHistory);
group.MapGet("/{id}", GetSearchHistory);
group.MapPost("/", UpdateSearchHistory);
group.MapDelete("/{id}", DeleteSearchHistory);
}
group.MapPost("/", async (IRepository<SearchHistory> repository, CancellationToken ct, SearchHistoryJson historyJson) =>
private static async Task<IResult> GetAllSearchHistory(IRepository<SearchHistory> repository, CancellationToken ct)
{
var results = await repository.GetAllAsync(ct);
return Results.Ok(results);
}
private static async Task<IResult> GetSearchHistory(IRepository<SearchHistory> 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> UpdateSearchHistory(IRepository<SearchHistory> repository, SearchHistory history, CancellationToken ct,
ILogger<Program> logger)
{
var result = await repository.GetByIdAsync(history._id, ct);
if (result == null)
{
var result = await repository.GetByIdAsync(historyJson._id, ct);
if (result == null)
try
{
result = new SearchHistory();
result.MapFrom(historyJson);
await repository.AddAsync(result, ct);
await repository.AddAsync(history, ct);
}
else
catch (Exception e)
{
result.MapFrom(historyJson);
await repository.UpdateAsync(result, ct);
logger.LogError(e, "Failed to update history {json}", history._id);
return Results.StatusCode(500);
}
return Results.Ok();
});
group.MapDelete("/{id}", async (IRepository<SearchHistory> repository, CancellationToken ct, string id) =>
}
else
{
var result = await repository.GetByIdAsync(id, ct);
if (result == null) return Results.NotFound();
await repository.DeleteAsync(result, ct);
return Results.Ok();
try
{
await repository.UpdateAsync(history, ct);
}
catch (Exception e)
{
logger.LogError(e, "Failed to update history {json}", history._id);
return Results.StatusCode(500);
}
}
});
return Results.Ok();
}
private static async Task<IResult> DeleteSearchHistory(IRepository<SearchHistory> 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();
}
}

View file

@ -1,6 +1,4 @@
using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
using FreeTubeSync.Model.Json;
using FreeTubeSync.Model;
namespace FreeTubeSync.EndPoints;
@ -10,37 +8,59 @@ public static class SettingEndpoint
{
var group = app.MapGroup("settings");
group.MapGet("/", async (IRepository<Setting> repository, CancellationToken ct) =>
{
var settings = await repository.GetAllAsync(ct);
var jsonSettings = new List<SettingJson>();
settings.MapTo(jsonSettings);
return Results.Ok(jsonSettings);
});
group.MapGet("/", GetAllSettings);
group.MapGet("/{id}", GetSetting);
group.MapPost("/", UpdateSetting);
group.MapDelete("/{id}", DeleteSetting);
}
group.MapPost("/", async (IRepository<Setting> repository, CancellationToken ct, SettingJson settingJson) =>
{
var res = await repository.GetByIdAsync(settingJson._id, ct);
if (res == null)
{
res = new Setting();
res.MapFrom(settingJson);
await repository.AddAsync(res, ct);
}
else
{
res.MapFrom(settingJson);
await repository.UpdateAsync(res, ct);
}
return Results.Ok();
});
private static async Task<IResult> GetAllSettings(IRepository<Setting> repository, CancellationToken ct)
{
var settings = await repository.GetAllAsync(ct);
return Results.Ok(settings);
}
group.MapDelete("/{id}", async (IRepository<Setting> repository, CancellationToken ct, string id) =>
private static async Task<IResult> GetSetting(IRepository<Setting> 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> UpdateSetting(IRepository<Setting> repository, Setting setting, CancellationToken ct, ILogger<Program> logger)
{
var res = await repository.GetByIdAsync(setting._id, ct);
if (res == null)
{
var result = await repository.GetByIdAsync(id, ct);
if (result == null) return Results.NotFound();
await repository.DeleteAsync(result, ct);
return Results.Ok();
});
try
{
await repository.AddAsync(setting, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to add setting {setting}", setting._id);
return Results.StatusCode(500);
}
}
else
{
try
{
await repository.UpdateAsync(setting, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to update setting {setting}", setting._id);
return Results.StatusCode(500);
}
}
return Results.Ok();
}
private static async Task<IResult> DeleteSetting(IRepository<Setting> 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();
}
}