freetubesync/FreeTubeSync/EndPoints/SettingEndpoint.cs
Mario Steele 004c490dd8 Looks like we finally have a solution.
Change logging to just log a message, instead of the exception.
Moved logic to Syncer involvement.  When posting the data, if a 500 is
returned, then it is up to the Syncer to re-submit it.
2025-08-09 04:09:17 -05:00

61 lines
No EOL
2.1 KiB
C#

using FreeTubeSync.Model;
namespace FreeTubeSync.EndPoints;
public static class SettingEndpoint
{
public static void MapSettingEndpoints(this WebApplication app)
{
var group = app.MapGroup("settings");
group.MapGet("/", GetAllSettings);
group.MapGet("/{id}", GetSetting);
group.MapPost("/", UpdateSetting);
group.MapDelete("/{id}", DeleteSetting);
}
private static async Task<IResult> GetAllSettings(IRepository<Setting> repository, CancellationToken ct)
{
var settings = await repository.GetAllAsync(ct);
return Results.Ok(settings);
}
private static async Task<IResult> GetSetting(IRepository<Setting> repository, string id, CancellationToken ct)
{
var result = await repository.GetByIdAsync(id, ct);
return result == null ? Results.NotFound() : Results.Ok(result);
}
private static async Task<IResult> UpdateSetting(IRepository<Setting> repository, Setting setting, CancellationToken ct, ILogger<Program> logger)
{
var res = await repository.GetByIdAsync(setting._id, ct);
if (res == null)
{
try
{
logger.LogInformation("Setting {id} does not exist, adding it to the database", setting._id);
await repository.AddAsync(setting, ct);
}
catch (Exception)
{
logger.LogError("Failed to add setting {setting}", setting._id);
return Results.StatusCode(500);
}
}
else
{
logger.LogInformation("Setting {id} exists, updating the database.", setting._id);
res.UpdateFrom(setting);
await repository.UpdateAsync(res, ct);
}
return Results.Ok();
}
private static async Task<IResult> DeleteSetting(IRepository<Setting> repository, string id, CancellationToken ct)
{
var result = await repository.GetByIdAsync(id, ct);
if (result == null) return Results.NotFound();
await repository.DeleteAsync(result, ct);
return Results.Ok();
}
}