freetubesync/FreeTubeSync/EndPoints/SettingEndpoint.cs
Mario Steele 157d46ee2e Updated Endpoints
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.
2025-07-21 17:10:42 -05:00

40 lines
No EOL
1.3 KiB
C#

using FreeTubeSync.Model;
using FreeTubeSync.SpecialResponses;
namespace FreeTubeSync.EndPoints;
public static class SettingEndpoint
{
public static void MapSettingEndpoints(this WebApplication app)
{
var group = app.MapGroup("settings");
group.MapGet("/", async (IRepository<Setting> repository, CancellationToken ct) =>
{
var settings = await repository.GetAllAsync(ct);
var response = settings.MapToResponse();
return Results.Ok(response);
});
group.MapPost("/", async (IRepository<Setting> repository, CancellationToken ct, Setting setting) =>
{
var res = await repository.GetByIdAsync(setting._id, ct);
if (res == null)
await repository.AddAsync(setting, ct);
else
{
res.Update(setting);
await repository.UpdateAsync(res, ct);
}
return Results.Ok();
});
group.MapDelete("/{id}", async (IRepository<Setting> 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();
});
}
}