Compare commits

...

6 commits

Author SHA1 Message Date
157d46ee2e Updated Endpoints
Updated all endpoints to use Update() method of the model, instead of
trying to use the data from the REST object directly to update the
database.  WHen doing so, tracking gets busted in EFCore, so instead,
will go through and update all properties of a model from the database
object, with the data from the REST object.
2025-07-21 17:10:42 -05:00
21af6f4300 Update Video
Added Update() method to model.
2025-07-21 17:09:14 -05:00
77c95a9525 Update Subscription
Added Update() Method to model.
2025-07-21 17:08:58 -05:00
e1ad30da0c Updated Setting
Added Update() method to model.
2025-07-21 17:08:43 -05:00
b08c22e77b Updated Profile
Updated code to filter out subscriptions that have been removed from the
profile, and insert new ones into the list in the Update() fucntion.
2025-07-21 17:08:17 -05:00
561ba4f34a Updated Playlist
Updated code to filter out videos that have been removed from the
playlist, and insert new ones into the list in the Update() function.
2025-07-21 17:05:27 -05:00
10 changed files with 77 additions and 9 deletions

View file

@ -20,7 +20,10 @@ public static class HistoryEndpoint
if (results == null) if (results == null)
await repository.AddAsync(history, ct); await repository.AddAsync(history, ct);
else else
await repository.UpdateAsync(history, ct); {
results.Update(history);
await repository.UpdateAsync(results, ct);
}
return Results.Ok(); return Results.Ok();
}); });

View file

@ -20,7 +20,11 @@ public static class PlaylistEndpoint
if (results == null) if (results == null)
await repository.AddAsync(playlist, ct); await repository.AddAsync(playlist, ct);
else else
await repository.UpdateAsync(playlist, ct); {
results.Update(playlist);
await repository.UpdateAsync(results, ct);
}
return Results.Ok(); return Results.Ok();
}); });

View file

@ -20,7 +20,11 @@ public static class ProfileEndpoint
if (res == null) if (res == null)
await repository.AddAsync(profile, ct); await repository.AddAsync(profile, ct);
else else
await repository.UpdateAsync(profile, ct); {
res.Update(profile);
await repository.UpdateAsync(res, ct);
}
return Results.Ok(); return Results.Ok();
}); });

View file

@ -17,10 +17,14 @@ public static class SearchHistoryEndpoint
group.MapPost("/", async (IRepository<SearchHistory> repository, CancellationToken ct, SearchHistory history) => group.MapPost("/", async (IRepository<SearchHistory> repository, CancellationToken ct, SearchHistory history) =>
{ {
var result = await repository.GetByIdAsync(history._id, ct); var result = await repository.GetByIdAsync(history._id, ct);
if (result != null) if (result == null)
await repository.UpdateAsync(history, ct);
else
await repository.AddAsync(history, ct); await repository.AddAsync(history, ct);
else
{
result.Update(history);
await repository.UpdateAsync(result, ct);
}
return Results.Ok(); return Results.Ok();
}); });

View file

@ -22,7 +22,10 @@ public static class SettingEndpoint
if (res == null) if (res == null)
await repository.AddAsync(setting, ct); await repository.AddAsync(setting, ct);
else else
await repository.UpdateAsync(setting, ct); {
res.Update(setting);
await repository.UpdateAsync(res, ct);
}
return Results.Ok(); return Results.Ok();
}); });

View file

@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore.Query.Internal;
namespace FreeTubeSync.Model; namespace FreeTubeSync.Model;
@ -18,7 +19,20 @@ public class Playlist
{ {
if (other.playlistName != playlistName) playlistName = other.playlistName; if (other.playlistName != playlistName) playlistName = other.playlistName;
if (other.@protected != @protected) @protected = other.@protected; if (other.@protected != @protected) @protected = other.@protected;
if (other.videos.Count != videos.Count) videos = other.videos; var remove = new List<Video>();
foreach (var vid in videos)
{
if (other.videos.Any(x => x.playlistItemId == vid.playlistItemId)) continue;
remove.Add(vid);
}
videos.RemoveAll(x => remove.Contains(x));
remove.Clear();
foreach (var vid in other.videos)
{
if (videos.Any(x => x.playlistItemId == vid.playlistItemId)) continue;
remove.Add(vid);
}
videos.AddRange(remove);
if (other.createdAt != createdAt) createdAt = other.createdAt; if (other.createdAt != createdAt) createdAt = other.createdAt;
if (other.lastUpdatedAt != lastUpdatedAt) lastUpdatedAt = other.lastUpdatedAt; if (other.lastUpdatedAt != lastUpdatedAt) lastUpdatedAt = other.lastUpdatedAt;
} }

View file

@ -18,6 +18,19 @@ public class Profile
if (other.name != name) name = other.name; if (other.name != name) name = other.name;
if (other.bgColor != bgColor) bgColor = other.bgColor; if (other.bgColor != bgColor) bgColor = other.bgColor;
if (other.textColor != textColor) textColor = other.textColor; if (other.textColor != textColor) textColor = other.textColor;
if (other.subscriptions.Count != subscriptions.Count) subscriptions = other.subscriptions; var remove = new List<Subscription>();
foreach (var sub in subscriptions)
{
if (other.subscriptions.Any(x => x.id == sub.id)) continue;
remove.Add(sub);
}
subscriptions.RemoveAll(x => remove.Contains(x));
remove.Clear();
foreach (var sub in other.subscriptions)
{
if (subscriptions.Any(x => x.id == sub.id)) continue;
remove.Add(sub);
}
subscriptions.AddRange(remove);
} }
} }

View file

@ -22,4 +22,9 @@ public class Setting
#pragma warning restore CS8603 #pragma warning restore CS8603
set => ValueJson = JsonSerializer.Serialize(value); set => ValueJson = JsonSerializer.Serialize(value);
} }
public void Update(Setting other)
{
Value = other.Value;
}
} }

View file

@ -10,4 +10,10 @@ public class Subscription
public required string id { get; set; } public required string id { get; set; }
public required string name { get; set; } public required string name { get; set; }
public string? thumbnail { get; set; } public string? thumbnail { get; set; }
public void Update(Subscription other)
{
name = other.name;
thumbnail = other.thumbnail;
}
} }

View file

@ -16,4 +16,16 @@ public class Video
public long timeAdded { get; set; } public long timeAdded { get; set; }
[Key] public string playlistItemId { get; set; } = string.Empty; [Key] public string playlistItemId { get; set; } = string.Empty;
public string type { get; set; } = string.Empty; public string type { get; set; } = string.Empty;
public void Update(Video other)
{
videoId = other.videoId;
title = other.title;
author = other.author;
authorId = other.authorId;
lengthSeconds = other.lengthSeconds;
pubished = other.pubished;
timeAdded = other.timeAdded;
type = other.type;
}
} }