Updated Syncer

Added GetLastUpdated() to ISyncer interface.
Updated code to use new /ping endpoint.
Added GetLastUpdated() REST API call to fetch when the database was last
updated.
Clarified logging for when an Update is from a file, or from the REST
API Server.
Added logging for when a nw File Entry is processed, or when a File
Entry is Updated.
This commit is contained in:
Mario Steele 2025-07-31 14:26:20 -05:00
parent 0e07f21ffe
commit a88da7f6d0

View file

@ -27,6 +27,7 @@ public interface ISyncer
void UpdateBaseUrl(string baseUrl);
void SetEnabled(bool enabled);
Task<bool> PingApi();
Task<DateTime> GetLastUpdated();
}
public class Syncer<T> : ISyncer where T : class, IDataModel, new()
@ -71,11 +72,18 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
Console.WriteLine($"Pinging API at {_client.BuildUri(new RestRequest("/ping"))}...");
try
{
var res = await _client.ExecuteHeadAsync(new RestRequest("/ping"));
if (res.StatusCode == HttpStatusCode.NotFound)
var res = await _client.GetAsync<Ping>(new RestRequest("/ping"));
if (res == null)
{
// TODO: Replace with Logger
Console.WriteLine($"Ping response 404 Not Found, Server Online!");
Console.WriteLine($"Ping returned null, not the server we are looking for!");
return false;
}
if (res.AppVersion == "0.1.3")
{
// TODO: Replace with Logger
Console.WriteLine($"Server Online! {res.AppVersion}");
return true;
}
}
@ -91,6 +99,12 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
return false;
}
public async Task<DateTime> GetLastUpdated()
{
var res = await _client.GetAsync<UpdateCheck>("/ping/lastUpdated");
return res?.LastUpdated ?? DateTime.MinValue;
}
public async Task ReadDatabase()
{
if (!_enabled) return;
@ -115,7 +129,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
_entries.RemoveAll(x => x.EqualId(item.Id()));
_entries.Add(item);
// TODO: Replace with Logger
Console.WriteLine($"Posting {item.Id()}");
Console.WriteLine($"Posting to REST: {item.Id()}");
await _client.PostJsonAsync<T>(_restEndpoint, item);
}
}
@ -134,13 +148,13 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (data.Equals(entry)) continue;
// TODO: Replace with Logger
Console.WriteLine($"Updated Entry for {_dbName} - {entry.Id()}");
Console.WriteLine($"Updated Entry from REST for {_dbName} - {entry.Id()}");
_entries.RemoveAll(x => x.EqualId(entry.Id()));
}
else
{
// TODO: Replace with Logger
Console.WriteLine($"New Entry for {_dbName} - {entry.Id()}");
Console.WriteLine($"New Entry from REST for {_dbName} - {entry.Id()}");
}
_entries.Add(entry);
@ -185,9 +199,15 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
{
var data = _entries.First(x => x.EqualId(entry.Id()));
if (data.Equals(entry)) return;
// TODO: Replace with Logger
Console.WriteLine($"File Entry {entry.Id()} updated for {_dbName}");
_entries.RemoveAll(x => x.EqualId(entry.Id()));
}
else
{
// TODO: Replace with Logger
Console.WriteLine($"New File Entry {entry.Id()} for {_dbName}");
}
_entries.Add(entry);
await _client.PostJsonAsync<T>(_restEndpoint, entry);