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