2025-08-08 16:59:29 -05:00
|
|
|
using FreeTubeSync.Model;
|
2025-07-19 04:02:09 -05:00
|
|
|
|
|
|
|
|
namespace FreeTubeSync.EndPoints;
|
|
|
|
|
|
|
|
|
|
public static class ProfileEndpoint
|
|
|
|
|
{
|
|
|
|
|
public static void MapProfileEndpoints(this WebApplication app)
|
|
|
|
|
{
|
|
|
|
|
var group = app.MapGroup("profile");
|
|
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
group.MapGet("/", GetAllProfiles);
|
|
|
|
|
group.MapGet("/{id}", GetProfile);
|
|
|
|
|
group.MapPost("/", UpdateProfile);
|
|
|
|
|
group.MapDelete("/{id}", DeleteProfile);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
private static async Task<IResult> GetAllProfiles(IRepository<Profile> repository, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
var results = await repository.GetAllAsync(ct);
|
|
|
|
|
return Results.Ok(results);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<IResult> GetProfile(IRepository<Profile> 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> UpdateProfile(IRepository<Profile> repository, Profile profile, CancellationToken ct, ILogger<Program> logger)
|
|
|
|
|
{
|
|
|
|
|
var res = await repository.GetByIdAsync(profile._id, ct);
|
|
|
|
|
if (res == null)
|
2025-07-19 04:02:09 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
try
|
2025-07-22 17:03:33 -05:00
|
|
|
{
|
2025-08-09 04:09:17 -05:00
|
|
|
logger.LogInformation("Profile {id} does not exist, adding it to the database", profile._id);
|
2025-08-08 16:59:29 -05:00
|
|
|
await repository.AddAsync(profile, ct);
|
2025-07-22 17:03:33 -05:00
|
|
|
}
|
2025-08-09 04:09:17 -05:00
|
|
|
catch (Exception)
|
2025-07-21 17:10:42 -05:00
|
|
|
{
|
2025-08-09 04:09:17 -05:00
|
|
|
logger.LogError("Failed to create profile {json}", profile._id);
|
2025-08-08 16:59:29 -05:00
|
|
|
return Results.StatusCode(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-08-09 04:09:17 -05:00
|
|
|
logger.LogInformation("Profile {id} exists, updating the database.", profile._id);
|
|
|
|
|
res.UpdateFrom(profile);
|
2025-08-08 16:59:29 -05:00
|
|
|
var toRemove = res.subscriptions.Where(sub => !profile.subscriptions.Any(x => x.id == sub.id)).ToList();
|
2025-08-04 19:13:17 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
foreach (var sub in toRemove)
|
|
|
|
|
res.subscriptions.Remove(sub);
|
2025-07-22 17:03:33 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
foreach (var sub in profile.subscriptions)
|
|
|
|
|
{
|
|
|
|
|
var osub = res.subscriptions.FirstOrDefault(x => x.id == sub.id);
|
|
|
|
|
if (osub == null)
|
|
|
|
|
res.subscriptions.Add(sub);
|
|
|
|
|
else
|
2025-07-22 17:03:33 -05:00
|
|
|
{
|
2025-08-08 16:59:29 -05:00
|
|
|
osub.name = sub.name;
|
|
|
|
|
osub.thumbnail = sub.thumbnail;
|
2025-07-22 17:03:33 -05:00
|
|
|
}
|
2025-07-21 17:10:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
await repository.UpdateAsync(res, ct);
|
|
|
|
|
}
|
2025-07-19 04:02:09 -05:00
|
|
|
|
2025-08-08 16:59:29 -05:00
|
|
|
return Results.Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<IResult> DeleteProfile(IRepository<Profile> 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();
|
2025-07-19 04:02:09 -05:00
|
|
|
}
|
|
|
|
|
}
|