Change logging to just log a message, instead of the exception. Moved logic to Syncer involvement. When posting the data, if a 500 is returned, then it is up to the Syncer to re-submit it.
63 lines
No EOL
2.2 KiB
C#
63 lines
No EOL
2.2 KiB
C#
using FreeTubeSync.Model;
|
|
|
|
namespace FreeTubeSync.EndPoints;
|
|
|
|
public static class SearchHistoryEndpoint
|
|
{
|
|
public static void MapSearchHistoryEndpoints(this WebApplication app)
|
|
{
|
|
var group = app.MapGroup("searchHistory");
|
|
|
|
group.MapGet("/", GetAllSearchHistory);
|
|
group.MapGet("/{id}", GetSearchHistory);
|
|
group.MapPost("/", UpdateSearchHistory);
|
|
group.MapDelete("/{id}", DeleteSearchHistory);
|
|
}
|
|
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation("SearchHistory {id} does not exist, adding it to the database", history._id);
|
|
await repository.AddAsync(history, ct);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
logger.LogError("Failed to update history {json}", history._id);
|
|
return Results.StatusCode(500);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
logger.LogInformation("SearchHistory {id} exists, updating the database.", history._id);
|
|
result.UpdateFrom(history);
|
|
await repository.UpdateAsync(result, ct);
|
|
}
|
|
|
|
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();
|
|
}
|
|
} |