private async Task MergeApiToExternal(IReadOnlyList <Profile> profiles) { using (var cached = await _cachedProfiles.LockedProfileData()) { var external = cached.External; foreach (var profile in profiles) { var candidate = profile.WithStatus(ProfileStatus.Synced); candidate.ModifiedAt = DateTime.UtcNow; var existing = external.FirstOrDefault(p => ProfileByExternalIdEqualityComparer.Equals(p, profile)); if (existing == null) { var notSynced = cached.Local.FirstOrDefault(p => p.Status == ProfileStatus.Created && ProfileByEssentialPropertiesEqualityComparer.Equals(p, profile)) ?? cached.Sync.FirstOrDefault(p => p.Status == ProfileStatus.Created && ProfileByEssentialPropertiesEqualityComparer.Equals(p, profile)); if (notSynced != null) { candidate = candidate.WithIdFrom(notSynced); cached.Local.Remove(notSynced); cached.Sync.Remove(notSynced); } external.Add(candidate.WithSyncStatus(ProfileSyncStatus.Succeeded)); continue; } if (ProfileByPropertiesEqualityComparer.Equals(profile, existing)) { continue; } candidate = candidate .WithIdFrom(existing) .WithSyncStatus(existing.SyncStatus); external.AddOrReplace(candidate); } external .Except(profiles, ProfileByExternalIdEqualityComparer) .ToList() .ForEach(external.Remove); _changesSynced = _changesSynced || cached.HasChanges; } }
private async Task Skip(Profile profile) { using (var cached = await _cachedProfiles.LockedProfileData()) { cached.Sync.Remove(profile); } }
private async Task AddOrReplaceLocal(Profile profile, ProfileStatus status) { using (var cached = await _cachedProfiles.LockedProfileData()) { var local = cached.Local.Get(profile); if (local?.Status == ProfileStatus.Created && status == ProfileStatus.Deleted) { cached.Local.Remove(local); return; } var mapped = Map(profile) .WithStatus(status) .WithSyncStatus(ProfileSyncStatus.InProgress) .WithModifiedAt(DateTime.UtcNow); cached.Local.AddOrReplace( mapped.WithStatusMergedFrom(local)); } }