freetubesync/FreeTubeSync/EndPoints/SearchHistoryEndpoint.cs
Mario Steele 4985dc4179 Started the Split
Split Json data models coming from REST Api, from the Database models
storing them in a SQLite database.
Work to re-engineer endpoints to use Database objects, and copy/update
data from the json objects.
More work is needed.
2025-07-22 17:03:33 -05:00

48 lines
No EOL
1.6 KiB
C#

using FreeTubeSync.Database;
using FreeTubeSync.Model.Database;
using FreeTubeSync.Model.Json;
namespace FreeTubeSync.EndPoints;
public static class SearchHistoryEndpoint
{
public static void MapSearchHistoryEndpoints(this WebApplication app)
{
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.MapPost("/", async (IRepository<SearchHistory> repository, CancellationToken ct, SearchHistoryJson historyJson) =>
{
var result = await repository.GetByIdAsync(historyJson._id, ct);
if (result == null)
{
result = new SearchHistory();
result.MapFrom(historyJson);
await repository.AddAsync(result, ct);
}
else
{
result.MapFrom(historyJson);
await repository.UpdateAsync(result, ct);
}
return Results.Ok();
});
group.MapDelete("/{id}", async (IRepository<SearchHistory> 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();
});
}
}