Possibly fix issues
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.
This commit is contained in:
parent
2e4f644c17
commit
73955353cb
23 changed files with 314 additions and 352 deletions
|
|
@ -1,6 +1,4 @@
|
|||
using FreeTubeSync.Database;
|
||||
using FreeTubeSync.Model.Database;
|
||||
using FreeTubeSync.Model.Json;
|
||||
using FreeTubeSync.Model;
|
||||
|
||||
namespace FreeTubeSync.EndPoints;
|
||||
|
||||
|
|
@ -10,37 +8,59 @@ public static class SettingEndpoint
|
|||
{
|
||||
var group = app.MapGroup("settings");
|
||||
|
||||
group.MapGet("/", async (IRepository<Setting> repository, CancellationToken ct) =>
|
||||
{
|
||||
var settings = await repository.GetAllAsync(ct);
|
||||
var jsonSettings = new List<SettingJson>();
|
||||
settings.MapTo(jsonSettings);
|
||||
return Results.Ok(jsonSettings);
|
||||
});
|
||||
group.MapGet("/", GetAllSettings);
|
||||
group.MapGet("/{id}", GetSetting);
|
||||
group.MapPost("/", UpdateSetting);
|
||||
group.MapDelete("/{id}", DeleteSetting);
|
||||
}
|
||||
|
||||
group.MapPost("/", async (IRepository<Setting> repository, CancellationToken ct, SettingJson settingJson) =>
|
||||
{
|
||||
var res = await repository.GetByIdAsync(settingJson._id, ct);
|
||||
if (res == null)
|
||||
{
|
||||
res = new Setting();
|
||||
res.MapFrom(settingJson);
|
||||
await repository.AddAsync(res, ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
res.MapFrom(settingJson);
|
||||
await repository.UpdateAsync(res, ct);
|
||||
}
|
||||
return Results.Ok();
|
||||
});
|
||||
private static async Task<IResult> GetAllSettings(IRepository<Setting> repository, CancellationToken ct)
|
||||
{
|
||||
var settings = await repository.GetAllAsync(ct);
|
||||
return Results.Ok(settings);
|
||||
}
|
||||
|
||||
group.MapDelete("/{id}", async (IRepository<Setting> repository, CancellationToken ct, string id) =>
|
||||
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)
|
||||
{
|
||||
var result = await repository.GetByIdAsync(id, ct);
|
||||
if (result == null) return Results.NotFound();
|
||||
await repository.DeleteAsync(result, ct);
|
||||
return Results.Ok();
|
||||
});
|
||||
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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue