From 5887bc0961eb345b1df713d95ccf358a8a630f09 Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Wed, 30 Jul 2025 12:19:53 -0500 Subject: [PATCH] Updated Syncer Added interface for Syncer class. Changed IsDirty from Property to Function Moved order for reading in initial database. First will fetch from the REST Api, then it will read in the database stored locally. Changed logic behind reading and fetching database. Fetching the database from the REST Api will mark internally that it is dirty, and force syncing. If an entry exists in our cache, and is equal, then we continue through, otherwise we remove the old entry, and use the new entry. --- FreeTubeSyncer/REST/Syncer.cs | 47 ++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/FreeTubeSyncer/REST/Syncer.cs b/FreeTubeSyncer/REST/Syncer.cs index 13758f9..f61be58 100644 --- a/FreeTubeSyncer/REST/Syncer.cs +++ b/FreeTubeSyncer/REST/Syncer.cs @@ -11,7 +11,16 @@ using RestSharp; namespace FreeTubeSyncer.REST; -public class Syncer where T : class, IDataModel, new() +public interface ISyncer +{ + Task ReadDatabase(); + Task FetchDatabase(); + void HandleDatabaseChange(string dbName, string entryObject); + void Sync(); + bool IsDirty(); +} + +public class Syncer : ISyncer where T : class, IDataModel, new() { private List _entries = new List(); private RestClient _client; @@ -19,21 +28,23 @@ public class Syncer where T : class, IDataModel, new() private string _dbName; private string _restEndpoint; - public bool IsDirty = false; + private bool _isDirty = false; public Syncer(DBSyncWatcher watcher, string dbPath, string dbName, string restEndpoint) { watcher.WatchFiles[dbName] = typeof(T); watcher.OnDatabaseChange += HandleDatabaseChange; - _client = new RestClient(new RestClientOptions("http://localhost:5183")); + _client = new RestClient(new RestClientOptions("http://192.168.1.30:5050")); _dbPath = dbPath; _dbName = dbName; _restEndpoint = restEndpoint; - ReadDatabase().Wait(); FetchDatabase().Wait(); + ReadDatabase().Wait(); } - private async Task ReadDatabase() + public bool IsDirty() => _isDirty; + + public async Task ReadDatabase() { var lines = File.ReadAllLines(_dbPath); foreach (var entry in lines) @@ -60,20 +71,26 @@ public class Syncer where T : class, IDataModel, new() } } - private async Task FetchDatabase() + public async Task FetchDatabase() { - var entries = await _client.GetAsync>(_restEndpoint); + var entries = await _client.GetAsync>(_restEndpoint); if (entries == null) return; foreach (var entry in entries) { if (_entries.Any(x => x.EqualId(entry.Id()))) + { + var data = _entries.First(x => x.EqualId(entry.Id())); + + if (data.Equals(entry)) continue; + _entries.RemoveAll(x => x.EqualId(entry.Id())); - + } _entries.Add(entry); + _isDirty = true; } } - private async void HandleDatabaseChange(string dbName, string entryObject) + public async void HandleDatabaseChange(string dbName, string entryObject) { if (dbName != _dbName) return; @@ -92,16 +109,22 @@ public class Syncer where T : class, IDataModel, new() if (entry == null) return; entry.MarshalData(entry.Id(), entryObject); + if (_entries.Any(x => x.EqualId(entry.Id()))) + { + var data = _entries.First(x => x.EqualId(entry.Id())); + if (data.Equals(entry)) return; + _entries.RemoveAll(x => x.EqualId(entry.Id())); + } + _entries.Add(entry); await _client.PostJsonAsync(_restEndpoint, entry); - IsDirty = true; } public void Sync() { - if (!IsDirty) + if (!_isDirty) return; Console.WriteLine($"Syncing {_dbPath}..."); var json = new List(); @@ -109,6 +132,6 @@ public class Syncer where T : class, IDataModel, new() json.Add(entry.JsonData()); File.WriteAllLines(_dbPath, json); Console.WriteLine($"Updated {_dbPath}."); - IsDirty = false; + _isDirty = false; } } \ No newline at end of file