After doing testing with a test project, re-organized code to new method of storing, as well as ensure that all Endpoint lambda's are separated out into functions. Large commit, will be testing.
66 lines
No EOL
2.1 KiB
C#
66 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
|
|
{
|
|
await repository.AddAsync(setting, ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Failed to add setting {setting}", setting._id);
|
|
return Results.StatusCode(500);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
await repository.UpdateAsync(setting, ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Failed to update setting {setting}", setting._id);
|
|
return Results.StatusCode(500);
|
|
}
|
|
}
|
|
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();
|
|
}
|
|
} |