private async Task AddOrUpdateSet(string apiKey, Theme theme, Subtheme subtheme, Sets bricksetSet, short?year = null)
        {
            _messageHub.Publish(new SynchronizingSetStart {
                Theme = theme.Name, Subtheme = subtheme?.Name, Name = bricksetSet.Name, Number = bricksetSet.Number, NumberVariant = bricksetSet.NumberVariant, Year = year
            });

            try
            {
                var set = await MapSet(apiKey, theme, subtheme, bricksetSet);

                _setRepository.AddOrUpdate(set);

                await _thumbnailSynchronizer.Synchronize(set, true);
            }
            catch (Exception ex)
            {
                _messageHub.Publish(new SynchronizingSetException {
                    Theme = theme.Name, Subtheme = subtheme?.Name, Name = bricksetSet.Name, Number = bricksetSet.Number, NumberVariant = bricksetSet.NumberVariant, Exception = ex
                });
            }

            _messageHub.Publish(new SynchronizingSetEnd {
                Theme = theme.Name, Subtheme = subtheme?.Name, Name = bricksetSet.Name, Number = bricksetSet.Number, NumberVariant = bricksetSet.NumberVariant
            });
        }
        public void AddOrUpdate_NullSet_ReturnsNull()
        {
            var set = _setRepository.AddOrUpdate(null);

            Check.That(set).IsNull();
        }
        public async Task SynchronizeBricksetPrimaryUser_SynchronizationTimestampNotSetAndHasRemoteSets_UpdatesLocalSetsAndUpdatesUserSynchronizationTimestamp()
        {
            const string apiKey   = "APIKEY";
            const string userHash = "USERHASH";
            const string testUser = "******";

            _bricksetUserRepository.Add(BricksetUserType.Primary, testUser);

            var themesList    = JsonConvert.DeserializeObject <List <Themes> >(GetResultFileFromResource(Constants.JsonFileGetThemes));
            var subthemesList = JsonConvert.DeserializeObject <List <Subthemes> >(GetResultFileFromResource(Constants.JsonFileGetSubthemes));
            var setsList      = JsonConvert.DeserializeObject <List <Sets> >(GetResultFileFromResource(Constants.JsonFileGetSets));

            var testSetOwned = setsList[0];

            testSetOwned.Collection = new SetCollection
            {
                Owned    = true,
                QtyOwned = 2
            };

            var ownedTheme = themesList.First(theme => theme.Theme == testSetOwned.Theme).ToTheme();

            ownedTheme = _themeRepository.AddOrUpdate(ownedTheme);

            var ownedSubtheme = subthemesList.First(subtheme => subtheme.Theme == testSetOwned.Theme && subtheme.Subtheme == testSetOwned.Subtheme).ToSubtheme();

            ownedSubtheme.Theme = ownedTheme;

            ownedSubtheme = _subthemeRepository.AddOrUpdate(ownedSubtheme);

            var ownedSet = testSetOwned.ToSet();

            ownedSet.Theme    = ownedTheme;
            ownedSet.Subtheme = ownedSubtheme;

            _setRepository.AddOrUpdate(ownedSet);

            var testSetWanted = setsList[1];

            testSetWanted.Collection = new SetCollection
            {
                Wanted = true
            };

            var wantedTheme = themesList.First(theme => theme.Theme == testSetWanted.Theme).ToTheme();

            wantedTheme = wantedTheme.Name == ownedTheme.Name
                ? ownedTheme
                : _themeRepository.AddOrUpdate(wantedTheme);

            var wantedSubtheme = subthemesList.First(subtheme => subtheme.Theme == testSetWanted.Theme && subtheme.Subtheme == testSetWanted.Subtheme).ToSubtheme();

            wantedSubtheme.Theme = wantedTheme;

            wantedSubtheme       = wantedSubtheme.Name == ownedSubtheme.Name && wantedSubtheme.Theme.Name == ownedSubtheme.Theme.Name
                ? wantedSubtheme = ownedSubtheme
                : _subthemeRepository.AddOrUpdate(wantedSubtheme);

            var wantedSet = testSetWanted.ToSet();

            wantedSet.Theme    = wantedTheme;
            wantedSet.Subtheme = wantedSubtheme;

            _setRepository.AddOrUpdate(wantedSet);

            var bricksetApiService = Substitute.For <IBricksetApiService>();

            bricksetApiService
            .GetSets(Arg.Is <GetSetsParameters>(parameter => parameter.Owned.Value))
            .Returns(new List <Sets> {
                testSetOwned
            });

            bricksetApiService
            .GetSets(Arg.Is <GetSetsParameters>(parameter => parameter.Wanted.Value))
            .Returns(new List <Sets> {
                testSetWanted
            });

            var userSynchronizer = CreateTarget(bricksetApiService);

            await userSynchronizer.SynchronizeBricksetPrimaryUser(apiKey, testUser, userHash).ConfigureAwait(false);

            var user = _bricksetUserRepository.Get(testUser);

            Check.That(user.Sets).Not.IsEmpty();
            Check.That(user.Sets.Where(userSet => userSet.Set.SetId == testSetOwned.SetId && userSet.Owned == testSetOwned.Collection.Owned && userSet.QuantityOwned == testSetOwned.Collection.QtyOwned)).Not.IsEmpty();
            Check.That(user.Sets.Where(userSet => userSet.Set.SetId == testSetWanted.SetId && userSet.Wanted == testSetWanted.Collection.Wanted)).Not.IsEmpty();
            Check.That(user.UserSynchronizationTimestamp).HasAValue();
        }