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 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);
|
2025-07-22 17:03:33 -05:00
|
|
|
var jsonSettings = new List<SettingJson>();
|
|
|
|
|
settings.MapTo(jsonSettings);
|
|
|
|
|
return Results.Ok(jsonSettings);
|
2025-07-19 04:02:09 -05:00
|
|
|
});
|
|
|
|
|
|
2025-07-22 17:03:33 -05:00
|
|
|
group.MapPost("/", async (IRepository<Setting> repository, CancellationToken ct, SettingJson settingJson) =>
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
2025-07-22 17:03:33 -05:00
|
|
|
var res = await repository.GetByIdAsync(settingJson._id, ct);
|
2025-07-19 04:02:09 -05:00
|
|
|
if (res == null)
|
2025-07-22 17:03:33 -05:00
|
|
|
{
|
|
|
|
|
res = new Setting();
|
|
|
|
|
res.MapFrom(settingJson);
|
|
|
|
|
await repository.AddAsync(res, 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
|
|
|
res.MapFrom(settingJson);
|
2025-07-21 17:10:42 -05:00
|
|
|
await repository.UpdateAsync(res, ct);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|