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.
62 lines
No EOL
2.2 KiB
C#
62 lines
No EOL
2.2 KiB
C#
using FreeTubeSync.Model;
|
|
|
|
namespace FreeTubeSync.EndPoints;
|
|
|
|
public static class HistoryEndpoint
|
|
{
|
|
public static void MapHistoryEndpoints(this WebApplication app)
|
|
{
|
|
var group = app.MapGroup("history");
|
|
|
|
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);
|
|
}
|
|
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation("History {id} does not exist, adding it to the database Thread: {thread}",
|
|
history._id, Thread.CurrentThread.ManagedThreadId);
|
|
await repository.AddAsync(history, ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError("Failed to create history {json}", history._id);
|
|
return Results.StatusCode(500);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
logger.LogInformation("History {id} exists, updating the database. Thread: {thread}", history._id,
|
|
Thread.CurrentThread.ManagedThreadId);
|
|
results.UpdateFrom(history);
|
|
await repository.UpdateAsync(results, 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();
|
|
}
|
|
} |