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,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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue