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