Started the Split

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.
This commit is contained in:
Mario Steele 2025-07-22 17:03:33 -05:00
parent 157d46ee2e
commit 4985dc4179
36 changed files with 684 additions and 323 deletions

View file

@ -1,4 +1,6 @@
using FreeTubeSync.Model;
using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
using FreeTubeSync.Model.Json;
namespace FreeTubeSync.EndPoints;
@ -10,18 +12,67 @@ public static class ProfileEndpoint
group.MapGet("/", async (IRepository<Profile> repository, CancellationToken ct) =>
{
var results = await repository.GetAllAsync(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.MapPost("/", async (IRepository<Profile> repository, CancellationToken ct, Profile profile) =>
group.MapPost("/", async (IRepository<Profile> repository, IRepository<Subscription> subRepo, CancellationToken ct, ProfileJson profileJson) =>
{
var res = await repository.GetByIdAsync(profile._id, ct);
var res = await repository.GetByIdAsync(profileJson._id, ct);
if (res == null)
await repository.AddAsync(profile, ct);
{
res = new Profile();
res.MapFrom(profileJson);
foreach (var subscription in profileJson.subscriptions)
{
var sub = await subRepo.GetByIdAsync(subscription.id, ct);
if (sub == null)
{
sub = new Subscription();
sub.MapFrom(subscription);
await subRepo.AddAsync(sub, ct, false);
}
res.subscriptions.Add(sub);
}
await repository.AddAsync(res, ct);
}
else
{
res.Update(profile);
res.MapFrom(profileJson);
var notFound = new List<Subscription>();
foreach (var subscription in res.subscriptions)
{
var f = profileJson.subscriptions.FirstOrDefault(s => s.id == subscription.id);
if (f == null)
notFound.Add(subscription);
else
subscription.MapFrom(f);
}
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 = await subRepo.GetByIdAsync(newSub.id, ct);
if (sres == null)
{
sres = new Subscription();
sres.MapFrom(newSub);
await subRepo.AddAsync(sres, ct, false);
}
res.subscriptions.Add(sres);
}
foreach (var nfSub in notFound)
{
res.subscriptions.Remove(nfSub);
}
await repository.UpdateAsync(res, ct);
}