/// <summary> /// ctor /// </summary> /// <param name="uof"></param> public SyncService(IUnitOfWork <IClientContext> uof, IConnectivityService connectivityService, Lazy <IErrorService> errorService, SyncConfiguration configuration) { this.uof = uof; this.connectivityService = connectivityService; this.errorService = errorService; this.configuration = configuration; this.context = uof.Context; Set = context.Set <T>(); }
/// <summary> /// Sync deleted /// </summary> /// <returns></returns> public async Task SyncDeletedAsync() { Debug.WriteLine("DeleteAsync"); try { using (var client = new HttpClient { BaseAddress = new Uri(configuration.ApiBaseUrl), Timeout = new TimeSpan(0, 0, 5) }) { var uri = new Uri($"{configuration.TombstoneUri}/{typeof(T).Name}", UriKind.Relative); var response = await client.GetAsync(uri); response.EnsureSuccessStatusCode(); var deletedRecords = JsonConvert.DeserializeObject <IEnumerable <DeletedEntity> >(await response.Content.ReadAsStringAsync()); var deletedIds = deletedRecords.Select(f => f.EntityId).AsEnumerable(); Debug.WriteLine($"{deletedIds.Count()} éléments a supprimer"); var recordsToDelete = context.Set <T>().Where(f => deletedIds.Contains(f.Id)).AsEnumerable(); if (recordsToDelete.Any()) { context.Set <T>().RemoveRange(recordsToDelete); await context.CommitAsync(); } var operationsToDelete = context.Operations.Where(f => deletedIds.Contains(f.EntityId)).AsEnumerable(); if (operationsToDelete.Any()) { context.Operations.RemoveRange(operationsToDelete); await context.CommitAsync(); } } } catch (Exception ex) { Debug.WriteLine($"Exception en suppression : {ex.Message}"); Debug.WriteLine($"Exception en suppression inner : {ex.InnerException}"); Debug.WriteLine($"{ex.StackTrace}"); } }