Updated all endpoints to use Update() method of the model, instead of trying to use the data from the REST object directly to update the database. WHen doing so, tracking gets busted in EFCore, so instead, will go through and update all properties of a model from the database object, with the data from the REST object.
38 lines
No EOL
1.2 KiB
C#
38 lines
No EOL
1.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("/", async (IRepository<History> repository, CancellationToken ct) =>
|
|
{
|
|
var results = await repository.GetAllAsync(ct);
|
|
return Results.Ok(results);
|
|
});
|
|
|
|
group.MapPost("/", async (IRepository<History> repository, CancellationToken ct, History history) =>
|
|
{
|
|
var results = await repository.GetByIdAsync(history._id, ct);
|
|
if (results == null)
|
|
await repository.AddAsync(history, ct);
|
|
else
|
|
{
|
|
results.Update(history);
|
|
await repository.UpdateAsync(results, ct);
|
|
}
|
|
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();
|
|
});
|
|
}
|
|
} |