public async Task SynchronizeBricksetPrimaryUsersSets(string username = null)
        {
            _messageHub.Publish(new UserSynchronizationServiceStart {
                UserType = BricksetUserType.Primary
            });

            try
            {
                var apiKey = await _onboardingService.GetBricksetApiKey();

                if (string.IsNullOrWhiteSpace(apiKey))
                {
                    return;
                }

                if (string.IsNullOrWhiteSpace(username))
                {
                    var tasks = new List <Task>();

                    _bricksetUserRepository
                    .GetAllUsernames(BricksetUserType.Primary)
                    .ToList()
                    .ForEach(bricksetUsername => tasks.Add(SynchronizeBricksetPrimaryUser(apiKey, bricksetUsername)));

                    _messageHub.Publish(new UsersAcquired {
                        UserType = BricksetUserType.Primary, Count = tasks.Count
                    });

                    await Task.WhenAll(tasks);
                }
                else if (_bricksetUserRepository.Exists(username))
                {
                    _messageHub.Publish(new UsersAcquired {
                        UserType = BricksetUserType.Primary, Count = 1
                    });

                    await SynchronizeBricksetPrimaryUser(apiKey, username);
                }
                else
                {
                    throw new ArgumentException("Parameter was not found the list of primary users", nameof(username));
                }
            }
            catch (AggregateException aggEx)
            {
                _messageHub.Publish(new UserSynchronizationServiceException {
                    UserType = BricksetUserType.Primary, Exceptions = aggEx.InnerExceptions
                });
            }
            catch (Exception ex)
            {
                _messageHub.Publish(new UserSynchronizationServiceException {
                    UserType = BricksetUserType.Primary, Exceptions = new[] { ex }
                });
            }

            _messageHub.Publish(new UserSynchronizationServiceEnd {
                UserType = BricksetUserType.Primary
            });
        }
Beispiel #2
0
        public async Task SynchronizeAllSets()
        {
            _messageHub.Publish(new SetSynchronizationServiceStart());

            try
            {
                var apiKey = await _onboardingService.GetBricksetApiKey().ConfigureAwait(false);

                if (string.IsNullOrWhiteSpace(apiKey))
                {
                    return;
                }

                var dataSynchronizationTimestamp = _insightsRepository.GetDataSynchronizationTimestamp();

                _messageHub.Publish(new InsightsAcquired {
                    SynchronizationTimestamp = dataSynchronizationTimestamp
                });

                foreach (var theme in await _themeSynchronizer.Synchronize(apiKey).ConfigureAwait(false))
                {
                    _messageHub.Publish(new ProcessingThemeStart {
                        Name = theme.Name
                    });

                    try
                    {
                        var subthemes = await _subthemeSynchronizer.Synchronize(apiKey, theme).ConfigureAwait(false);

                        if (!dataSynchronizationTimestamp.HasValue)
                        {
                            foreach (var subtheme in subthemes)
                            {
                                _messageHub.Publish(new ProcessingSubthemeStart {
                                    Name = subtheme.Name
                                });

                                await _setSynchronizer.Synchronize(apiKey, theme, subtheme).ConfigureAwait(false);

                                _messageHub.Publish(new ProcessingSubthemeEnd {
                                    Name = subtheme.Name
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _messageHub.Publish(new ProcessingThemeException {
                            Name = theme.Name, Exception = ex
                        });
                    }

                    _messageHub.Publish(new ProcessingThemeEnd {
                        Name = theme.Name
                    });
                }

                if (dataSynchronizationTimestamp.HasValue)
                {
                    await _setSynchronizer.Synchronize(apiKey, dataSynchronizationTimestamp.Value).ConfigureAwait(false);
                }

                _insightsRepository.UpdateDataSynchronizationTimestamp(DateTimeOffset.Now);
            }
            catch (Exception ex)
            {
                _messageHub.Publish(new SetSynchronizationServiceException {
                    Exception = ex
                });
            }

            _messageHub.Publish(new SetSynchronizationServiceEnd());
        }