2025-08-08 16:59:29 -05:00
|
|
|
using FreeTubeSync.Model;
|
2025-07-19 04:02:09 -05:00
|
|
|
|
|
|
|
|
namespace FreeTubeSync.EndPoints;
|
|
|
|
|
|
|
|
|
|
public static class HistoryEndpoint
|
|
|
|
|
{
|
|
|
|
|
public static void MapHistoryEndpoints(this WebApplication app)
|
|
|
|
|
{
|
|
|
|
|
var group = app.MapGroup("history");
|
|
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
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)
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
try
|
2025-07-22 17:03:33 -05:00
|
|
|
{
|
2025-08-09 04:09:17 -05:00
|
|
|
logger.LogInformation("History {id} does not exist, adding it to the database Thread: {thread}",
|
|
|
|
|
history._id, Thread.CurrentThread.ManagedThreadId);
|
2025-08-08 16:59:29 -05:00
|
|
|
await repository.AddAsync(history, ct);
|
2025-07-22 17:03:33 -05:00
|
|
|
}
|
2025-08-08 16:59:29 -05:00
|
|
|
catch (Exception ex)
|
2025-07-21 17:10:42 -05:00
|
|
|
{
|
2025-08-09 04:09:17 -05:00
|
|
|
logger.LogError("Failed to create history {json}", history._id);
|
2025-08-08 16:59:29 -05:00
|
|
|
return Results.StatusCode(500);
|
2025-07-21 17:10:42 -05:00
|
|
|
}
|
2025-08-08 16:59:29 -05:00
|
|
|
}
|
|
|
|
|
else
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
2025-08-09 04:09:17 -05:00
|
|
|
logger.LogInformation("History {id} exists, updating the database. Thread: {thread}", history._id,
|
|
|
|
|
Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
results.UpdateFrom(history);
|
|
|
|
|
await repository.UpdateAsync(results, ct);
|
2025-08-08 16:59:29 -05:00
|
|
|
}
|
|
|
|
|
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();
|
2025-07-19 04:02:09 -05:00
|
|
|
}
|
|
|
|
|
}
|