using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using gehGassiApp.Core.Data; using gehGassiApp.Core.Interfaces.Synchronization; using gehGassiApp.Domain.Common; namespace gehGassiApp.Core.Services.Synchronization { /// /// Service der die Verwaltung von Synch-Infos ermöglicht. /// public class SyncInfoPullService : ServiceBase, ISyncInfoPullService { /// /// Erstellt eine Instanz /// /// Instanz eines IUnitOfWork public SyncInfoPullService(IUnitOfWork unitOfWork) : base(unitOfWork) { } /// /// Gibt eine Entität anhand der eindeutigen Id zurück /// /// Id der Entität /// Gibt an ob NoTRacking verwendet werden soll. Es werden keine Entitäten im EF-Speicher gehalten /// Entität oder null, wenn nicht gefunden public override SyncInfoPull Get(object id, bool noTracking = true) { return Repository.SingleOrDefault(c => c.Id == (int)id, noTracking); } /// /// Gibt eine Entität anhand der eindeutigen Id zurück /// /// Id der Entität /// Gibt an ob NoTRacking verwendet werden soll. Es werden keine Entitäten im EF-Speicher gehalten /// Entität oder null, wenn nicht gefunden public override async Task GetAsync(object id, bool noTracking = true) { return await Repository.SingleOrDefaultAsync(c => c.Id == (int)id, noTracking).ConfigureAwait(false); } /// /// Hinzufügen oder Aktualisieren einer SyncInfo zu einem Objekt. /// /// Tabelle bzw. Name des Objektes /// Wann wurde das letzte Mal ein Sync durchgeführt /// Task public async Task AddOrUpddateAsync(string table, DateTimeOffset lastUpdate) { var syncInfo = await Repository.FirstOrDefaultAsync(c => c.Table == table, false).ConfigureAwait(false); if (syncInfo != null && lastUpdate > syncInfo.LastUpdate) { syncInfo.LastUpdate = lastUpdate; Repository.Update(syncInfo); } else { syncInfo = new SyncInfoPull() { Table = table, LastUpdate = lastUpdate }; Repository.Add(syncInfo); } await CommitAsync().ConfigureAwait(false); } /// /// Abfragen einer Sync-Info zu einem Objekt /// /// Tabelle bzw. Name des Objektes /// SncInfo wenn gefunden, oder null wenn es noch keine gibt public async Task GetAsync(string table) { var syncInfo = await Repository.FirstOrDefaultAsync(c => c.Table == table).ConfigureAwait(false); if (syncInfo != null) { //syncInfo.LastUpdate = syncInfo.LastUpdate.AddMilliseconds(999); } return syncInfo; } } }