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.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FreeTubeSyncer.Library;
|
using FreeTubeSyncer.Library;
|
||||||
using FreeTubeSyncer.Models.DatabaseModels;
|
using FreeTubeSyncer.Models.DatabaseModels;
|
||||||
|
|
@ -10,11 +11,12 @@ using RestSharp;
|
||||||
|
|
||||||
namespace FreeTubeSyncer.REST;
|
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 List<T> _entries = new List<T>();
|
||||||
private RestClient _client;
|
private RestClient _client;
|
||||||
private string _dbPath;
|
private string _dbPath;
|
||||||
|
private string _dbName;
|
||||||
private string _restEndpoint;
|
private string _restEndpoint;
|
||||||
|
|
||||||
public bool IsDirty = false;
|
public bool IsDirty = false;
|
||||||
|
|
@ -25,6 +27,7 @@ public class Syncer<T> where T : class, IDataModel
|
||||||
watcher.OnDatabaseChange += HandleDatabaseChange;
|
watcher.OnDatabaseChange += HandleDatabaseChange;
|
||||||
_client = new RestClient(new RestClientOptions("http://localhost:5183"));
|
_client = new RestClient(new RestClientOptions("http://localhost:5183"));
|
||||||
_dbPath = dbPath;
|
_dbPath = dbPath;
|
||||||
|
_dbName = dbName;
|
||||||
_restEndpoint = restEndpoint;
|
_restEndpoint = restEndpoint;
|
||||||
ReadDatabase().Wait();
|
ReadDatabase().Wait();
|
||||||
FetchDatabase().Wait();
|
FetchDatabase().Wait();
|
||||||
|
|
@ -36,21 +39,25 @@ public class Syncer<T> where T : class, IDataModel
|
||||||
foreach (var entry in lines)
|
foreach (var entry in lines)
|
||||||
{
|
{
|
||||||
if (entry == "") continue;
|
if (entry == "") continue;
|
||||||
|
T? item;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var item = JsonSerializer.Deserialize<T>(entry, GlobalJsonOptions.Options);
|
item = JsonSerializer.Deserialize<T>(entry, GlobalJsonOptions.Options);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var jobj = JsonSerializer.Deserialize<JsonObject>(entry, GlobalJsonOptions.Options);
|
||||||
|
item = new T();
|
||||||
|
item.MarshalData(jobj["_id"].GetValue<string>(), entry);
|
||||||
|
}
|
||||||
if (item == null) continue;
|
if (item == null) continue;
|
||||||
|
item.MarshalData(item.Id(), entry);
|
||||||
if (_entries.Any(x => x.EqualId(item.Id())))
|
if (_entries.Any(x => x.EqualId(item.Id())))
|
||||||
_entries.RemoveAll(x => x.EqualId(item.Id()));
|
_entries.RemoveAll(x => x.EqualId(item.Id()));
|
||||||
_entries.Add(item);
|
_entries.Add(item);
|
||||||
Console.WriteLine($"Posting {item.Id()}");
|
Console.WriteLine($"Posting {item.Id()}");
|
||||||
await _client.PostJsonAsync<T>(_restEndpoint, item);
|
await _client.PostJsonAsync<T>(_restEndpoint, item);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Failed to parse: {entry}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task FetchDatabase()
|
private async Task FetchDatabase()
|
||||||
|
|
@ -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())))
|
if (_entries.Any(x => x.EqualId(entry.Id())))
|
||||||
_entries.RemoveAll(x => x.EqualId(entry.Id()));
|
_entries.RemoveAll(x => x.EqualId(entry.Id()));
|
||||||
_entries.Add(entry);
|
_entries.Add(entry);
|
||||||
|
|
@ -78,11 +101,14 @@ public class Syncer<T> where T : class, IDataModel
|
||||||
|
|
||||||
public void Sync()
|
public void Sync()
|
||||||
{
|
{
|
||||||
|
if (!IsDirty)
|
||||||
|
return;
|
||||||
|
Console.WriteLine($"Syncing {_dbPath}...");
|
||||||
var json = new List<string>();
|
var json = new List<string>();
|
||||||
foreach (var entry in _entries)
|
foreach (var entry in _entries)
|
||||||
json.Add(JsonSerializer.Serialize(entry));
|
json.Add(entry.JsonData());
|
||||||
File.WriteAllLines(_dbPath, json);
|
File.WriteAllLines(_dbPath, json);
|
||||||
Console.WriteLine($"Updated {_dbPath}");
|
Console.WriteLine($"Updated {_dbPath}.");
|
||||||
IsDirty = false;
|
IsDirty = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue