Updated Syncer
Updated Syncer class to properly sync all classes, even with special requierments needed for Settings. Now properly handles all classes, and ensures that the data is properly stored in the database, and can be written out correctly to the FreeTube database files.
This commit is contained in:
parent
8e65cc3a4b
commit
fb8284b5e8
1 changed files with 39 additions and 13 deletions
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Threading.Tasks;
|
||||
using FreeTubeSyncer.Library;
|
||||
using FreeTubeSyncer.Models.DatabaseModels;
|
||||
|
|
@ -10,11 +11,12 @@ using RestSharp;
|
|||
|
||||
namespace FreeTubeSyncer.REST;
|
||||
|
||||
public class Syncer<T> where T : class, IDataModel
|
||||
public class Syncer<T> where T : class, IDataModel, new()
|
||||
{
|
||||
private List<T> _entries = new List<T>();
|
||||
private RestClient _client;
|
||||
private string _dbPath;
|
||||
private string _dbName;
|
||||
private string _restEndpoint;
|
||||
|
||||
public bool IsDirty = false;
|
||||
|
|
@ -25,6 +27,7 @@ public class Syncer<T> where T : class, IDataModel
|
|||
watcher.OnDatabaseChange += HandleDatabaseChange;
|
||||
_client = new RestClient(new RestClientOptions("http://localhost:5183"));
|
||||
_dbPath = dbPath;
|
||||
_dbName = dbName;
|
||||
_restEndpoint = restEndpoint;
|
||||
ReadDatabase().Wait();
|
||||
FetchDatabase().Wait();
|
||||
|
|
@ -36,20 +39,24 @@ public class Syncer<T> where T : class, IDataModel
|
|||
foreach (var entry in lines)
|
||||
{
|
||||
if (entry == "") continue;
|
||||
T? item;
|
||||
try
|
||||
{
|
||||
var item = JsonSerializer.Deserialize<T>(entry, GlobalJsonOptions.Options);
|
||||
if (item == null) continue;
|
||||
if (_entries.Any(x => x.EqualId(item.Id())))
|
||||
_entries.RemoveAll(x => x.EqualId(item.Id()));
|
||||
_entries.Add(item);
|
||||
Console.WriteLine($"Posting {item.Id()}");
|
||||
await _client.PostJsonAsync<T>(_restEndpoint, item);
|
||||
item = JsonSerializer.Deserialize<T>(entry, GlobalJsonOptions.Options);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to parse: {entry}");
|
||||
var jobj = JsonSerializer.Deserialize<JsonObject>(entry, GlobalJsonOptions.Options);
|
||||
item = new T();
|
||||
item.MarshalData(jobj["_id"].GetValue<string>(), entry);
|
||||
}
|
||||
if (item == null) continue;
|
||||
item.MarshalData(item.Id(), entry);
|
||||
if (_entries.Any(x => x.EqualId(item.Id())))
|
||||
_entries.RemoveAll(x => x.EqualId(item.Id()));
|
||||
_entries.Add(item);
|
||||
Console.WriteLine($"Posting {item.Id()}");
|
||||
await _client.PostJsonAsync<T>(_restEndpoint, item);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -66,9 +73,25 @@ public class Syncer<T> where T : class, IDataModel
|
|||
}
|
||||
}
|
||||
|
||||
private async void HandleDatabaseChange(string dbName, object entryObject)
|
||||
private async void HandleDatabaseChange(string dbName, string entryObject)
|
||||
{
|
||||
var entry = (T)entryObject;
|
||||
if (dbName != _dbName)
|
||||
return;
|
||||
|
||||
T? entry;
|
||||
try
|
||||
{
|
||||
entry = JsonSerializer.Deserialize<T>(entryObject, GlobalJsonOptions.Options);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var jobj = JsonSerializer.Deserialize<JsonObject>(entryObject, GlobalJsonOptions.Options);
|
||||
entry = new T();
|
||||
entry.MarshalData(jobj["_id"].GetValue<string>(), entryObject);
|
||||
}
|
||||
|
||||
if (entry == null) return;
|
||||
entry.MarshalData(entry.Id(), entryObject);
|
||||
if (_entries.Any(x => x.EqualId(entry.Id())))
|
||||
_entries.RemoveAll(x => x.EqualId(entry.Id()));
|
||||
_entries.Add(entry);
|
||||
|
|
@ -78,11 +101,14 @@ public class Syncer<T> where T : class, IDataModel
|
|||
|
||||
public void Sync()
|
||||
{
|
||||
if (!IsDirty)
|
||||
return;
|
||||
Console.WriteLine($"Syncing {_dbPath}...");
|
||||
var json = new List<string>();
|
||||
foreach (var entry in _entries)
|
||||
json.Add(JsonSerializer.Serialize(entry));
|
||||
json.Add(entry.JsonData());
|
||||
File.WriteAllLines(_dbPath, json);
|
||||
Console.WriteLine($"Updated {_dbPath}");
|
||||
Console.WriteLine($"Updated {_dbPath}.");
|
||||
IsDirty = false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue