2025-07-22 17:03:33 -05:00
|
|
|
using FreeTubeSync.Database;
|
|
|
|
|
using FreeTubeSync.Model.Database;
|
|
|
|
|
using FreeTubeSync.Model.Json;
|
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");
|
|
|
|
|
|
|
|
|
|
group.MapGet("/", async (IRepository<History> repository, CancellationToken ct) =>
|
|
|
|
|
{
|
|
|
|
|
var results = await repository.GetAllAsync(ct);
|
2025-07-22 17:03:33 -05:00
|
|
|
var jsonResults = new List<HistoryJson>();
|
|
|
|
|
results.MapTo(jsonResults);
|
|
|
|
|
return Results.Ok(jsonResults);
|
2025-07-19 04:02:09 -05:00
|
|
|
});
|
|
|
|
|
|
2025-07-22 17:03:33 -05:00
|
|
|
group.MapPost("/", async (IRepository<History> repository, CancellationToken ct, HistoryJson historyJson) =>
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
2025-07-22 17:03:33 -05:00
|
|
|
var results = await repository.GetByIdAsync(historyJson._id, ct);
|
2025-07-19 04:02:09 -05:00
|
|
|
if (results == null)
|
2025-07-22 17:03:33 -05:00
|
|
|
{
|
|
|
|
|
results = new History();
|
|
|
|
|
results.MapFrom(historyJson);
|
|
|
|
|
await repository.AddAsync(results, ct);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
else
|
2025-07-21 17:10:42 -05:00
|
|
|
{
|
2025-07-22 17:03:33 -05:00
|
|
|
results.MapFrom(historyJson);
|
2025-07-21 17:10:42 -05:00
|
|
|
await repository.UpdateAsync(results, ct);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|