211 lines
7.8 KiB
C#
211 lines
7.8 KiB
C#
using gehGassiApp.Core.Interfaces.Synchronization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassiApp.Core.Data;
|
|
using gehGassiApp.Domain.Common;
|
|
using System.Text.Json;
|
|
|
|
namespace gehGassiApp.Core.Services.Synchronization
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Push Synch-Infos ermöglicht.
|
|
/// </summary>
|
|
public class SyncInfoPushService : ServiceBase<SyncInfoPush>, ISyncInfoPushService
|
|
{
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public SyncInfoPushService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität anhand der eindeutigen Id zurück
|
|
/// </summary>
|
|
/// <param name="id">Id der Entität</param>
|
|
/// <param name="noTracking">Gibt an ob NoTRacking verwendet werden soll. Es werden keine Entitäten im EF-Speicher gehalten</param>
|
|
/// <returns>Entität oder null, wenn nicht gefunden</returns>
|
|
public override SyncInfoPush Get(object id, bool noTracking = true)
|
|
{
|
|
return Repository.SingleOrDefault(c => c.Id == (Guid)id, noTracking);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Entität anhand der eindeutigen Id zurück
|
|
/// </summary>
|
|
/// <param name="id">Id der Entität</param>
|
|
/// <param name="noTracking">Gibt an ob NoTRacking verwendet werden soll. Es werden keine Entitäten im EF-Speicher gehalten</param>
|
|
/// <returns>Entität oder null, wenn nicht gefunden</returns>
|
|
public override async Task<SyncInfoPush> GetAsync(object id, bool noTracking = true)
|
|
{
|
|
return await Repository.SingleOrDefaultAsync(c => c.Id == (Guid)id, noTracking).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hinzufügen eines Push-Deltas
|
|
/// </summary>
|
|
/// <typeparam name="T">Typ des Objekts</typeparam>
|
|
/// <param name="table">Name der Tabelle = nameof(objekt)</param>
|
|
/// <param name="key">ID des Objektes als string</param>
|
|
/// <param name="operation">Operationstyp. Hinzufügen, Aktualisieren, Löschen</param>
|
|
/// <param name="value">Objekt das Serialisiert werden soll</param>
|
|
/// <returns>SyncInfoPush</returns>
|
|
public async Task<SyncInfoPush> AddAsync<T>(string table, string key, SyncOperation operation, T value)
|
|
{
|
|
if (value == null)
|
|
return null;
|
|
|
|
string jsonString;
|
|
try
|
|
{
|
|
jsonString = JsonSerializer.Serialize<T>(value, new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine(ex.Message);
|
|
jsonString = string.Empty;
|
|
}
|
|
|
|
var pushInfo = new SyncInfoPush
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Table = table,
|
|
Key = key,
|
|
Operation = operation,
|
|
DateTime = DateTimeOffset.UtcNow,
|
|
Value = jsonString
|
|
};
|
|
Repository.Add(pushInfo);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
|
|
return pushInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste aller offenen SyncInfoPushs einer Tabelle zurück
|
|
/// </summary>
|
|
/// <param name="table">Tabelle = nameof(objcet)</param>
|
|
/// <param name="noTracking">Gibt an ob NoTRacking verwendet werden soll. Es werden keine Entitäten im EF-Speicher gehalten</param>
|
|
/// <returns>Liste von SyncInfoPushs</returns>
|
|
public async Task<List<SyncInfoPush>> GetAllAsync(string table, bool noTracking = true)
|
|
{
|
|
return (await Repository.FindAsync(c => c.Table == table, noTracking).ConfigureAwait(false)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernen einer SyncInfoPush
|
|
/// </summary>
|
|
/// <param name="id">Eindeutige Id</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveAsync(Guid id)
|
|
{
|
|
var item = await Repository.FirstOrDefaultAsync(c => c.Id == id);
|
|
if (item != null)
|
|
{
|
|
Repository.Remove(item);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernen einer SyncInfoPush
|
|
/// </summary>
|
|
/// <param name="item">SyncInfoPush</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveAsync(SyncInfoPush item)
|
|
{
|
|
if (item != null)
|
|
{
|
|
Repository.Remove(item);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen aller Sync-Infos
|
|
/// </summary>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveAllAsync()
|
|
{
|
|
var items = await GetAllAsync().ConfigureAwait(false);
|
|
if (items.Any())
|
|
{
|
|
Repository.RemoveRange(items);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen aller Sync-Infos
|
|
/// </summary>
|
|
/// <param name="before">Datum vor welchem alle Sync-Infos gelöscht werden sollen</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveAllAsync(DateTimeOffset before)
|
|
{
|
|
var items = await FindAsync(c => c.DateTime < before).ConfigureAwait(false);
|
|
var syncInfoPushes = items.ToList();
|
|
if (syncInfoPushes.Any())
|
|
{
|
|
Repository.RemoveRange(syncInfoPushes);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen aller Sync-Infos einer Tabelle
|
|
/// </summary>
|
|
/// <param name="table">Tabelle = nameof(objcet)</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveAllAsync(string table)
|
|
{
|
|
var items = await FindAsync(c => c.Table == table).ConfigureAwait(false);
|
|
var syncInfoPushes = items.ToList();
|
|
if (syncInfoPushes.Any())
|
|
{
|
|
Repository.RemoveRange(syncInfoPushes);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen aller Sync-Infos einer Tabelle
|
|
/// </summary>
|
|
/// <param name="table">Tabelle = nameof(objcet)</param>
|
|
/// <param name="before">Datum vor welchem alle Sync-Infos gelöscht werden sollen</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveAllAsync(string table, DateTimeOffset before)
|
|
{
|
|
var items = await FindAsync(c => c.Table == table && c.DateTime < before).ConfigureAwait(false);
|
|
var syncInfoPushes = items.ToList();
|
|
if (syncInfoPushes.Any())
|
|
{
|
|
Repository.RemoveRange(syncInfoPushes);
|
|
await CommitAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt zurück ob offene Push-Infos anliegen
|
|
/// </summary>
|
|
/// <returns>Anzahl offene Push-Infos</returns>
|
|
public async Task<int> HasOpenAsync()
|
|
{
|
|
return await Repository.CountAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt zurück ob für eine Tabelle offene Push-Infos anliegen
|
|
/// </summary>
|
|
/// <param name="table">Tabelle = nameof(objcet)</param>
|
|
/// <returns>Anzahl offene Push-Infos</returns>
|
|
public async Task<int> HasOpenAsync(string table)
|
|
{
|
|
return await Repository.CountAsync(c => c.Table == table).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|