コード例 #1
0
        private async void GetVoteList()
        {
            if (string.IsNullOrEmpty(_username) || _password.Length <= 0)
            {
                return;
            }
            Globals.StatusBar.IsDbProcessing   = true;
            Globals.StatusBar.IsWorkProcessing = true;
            Globals.StatusBar.ProgressText     = "Loading Data...";
            UInt32[] dbIdList = { };
            using (var context = new DatabaseContext())
            {
                //gets a list of all vnids that are in the VnVoteList where the userId is the logged in user
                List <uint> idEntry = (from v in context.VnVoteList where v.UserId.Equals(_userId) select v.VnId).ToList();
                List <uint> idList  = idEntry.ToList();

                //gets a list of titles of all items in VnIdList which contain an id from the above vnlist,
                //which is any item in the votelist table
                List <string> entry = (from first in context.VnIdList
                                       join second in idList on first.VnId equals second
                                       select first.Title).ToList();

                if (entry.Count > 0)
                {
                    _userListCollection.InsertRange(entry);
                }
                if (idList.Count > 0)
                {
                    dbIdList = idList.ToArray();
                }
            }
            List <Tuple <uint, string> > dbItemsToAdd  = new List <Tuple <uint, string> >();
            List <VnVoteList>            voteListItems = new List <VnVoteList>();
            bool removeItems = false;

            using (Vndb client = new Vndb(Username, Password))
            {
                bool           hasMore = true;
                RequestOptions ro      = new RequestOptions();
                int            page    = 1;
                List <UInt32>  idList  = new List <uint>();
                //get the list of all ids on the votelist
                int errorCounter = 0;
                while (hasMore)
                {
                    ro.Page  = page;
                    ro.Count = 100;
                    try
                    {
                        if (dbIdList.Length > 0)
                        {
                            var voteList = await client.GetVoteListAsync(VndbFilters.UserId.Equals(_userId)& VndbFilters.VisualNovel.NotEquals(dbIdList), VndbFlags.FullVotelist, ro);

                            if (voteList != null && voteList.Count > 0)
                            {
                                hasMore = voteList.HasMore;
                                idList.AddRange(voteList.Select(vote => vote.VisualNovelId));
                                voteListItems.AddRange(voteList.Select(item => new VnVoteList()
                                {
                                    UserId = item.UserId,
                                    VnId   = item.VisualNovelId,
                                    Vote   = item.Vote,
                                    Added  = item.AddedOn.ToString(CultureInfo.InvariantCulture)
                                }));
                                page++;
                            }
                            if (voteList != null && voteList.Count == 0)
                            {
                                voteList = await client.GetVoteListAsync(VndbFilters.UserId.Equals(_userId), VndbFlags.FullVotelist, ro);

                                if (voteList != null)
                                {
                                    hasMore = voteList.HasMore;
                                    idList.AddRange(voteList.Select(vote => vote.VisualNovelId));
                                    voteListItems.AddRange(voteList.Select(item => new VnVoteList()
                                    {
                                        UserId = item.UserId,
                                        VnId   = item.VisualNovelId,
                                        Vote   = item.Vote,
                                        Added  = item.AddedOn.ToString(CultureInfo.InvariantCulture)
                                    }));
                                    page++;
                                    removeItems = true;
                                }
                            }
                            else
                            {
                                HandleError.HandleErrors(client.GetLastError(), errorCounter);
                                errorCounter++;
                            }
                        }
                        else
                        {
                            var voteList = await client.GetVoteListAsync(VndbFilters.UserId.Equals(_userId), VndbFlags.FullVotelist, ro);

                            if (voteList != null)
                            {
                                hasMore = voteList.HasMore;
                                idList.AddRange(voteList.Select(wish => wish.VisualNovelId));
                                //dbWishlistToAdd.AddRange(votelist);
                                voteListItems.AddRange(voteList.Select(item => new VnVoteList()
                                {
                                    UserId = item.UserId,
                                    VnId   = item.VisualNovelId,
                                    Vote   = item.Vote,
                                    Added  = item.AddedOn.ToString(CultureInfo.InvariantCulture)
                                }));
                                page++;
                                removeItems = true;
                            }
                            else
                            {
                                HandleError.HandleErrors(client.GetLastError(), errorCounter);
                                errorCounter++;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Globals.Logger.Error(ex);
                        throw;
                    }
                }
                //get names from ids on votelist, and add them to ObservableCollection
                hasMore = true;
                page    = 1;
                while (hasMore)
                {
                    ro.Count = 25;
                    ro.Page  = page;
                    try
                    {
                        var data = await client.GetVisualNovelAsync(VndbFilters.Id.Equals(idList.ToArray()), VndbFlags.Basic, ro);

                        if (data != null)
                        {
                            hasMore = data.HasMore;
                            foreach (var item in data)
                            {
                                _userListCollection.Add(item.Name);
                                dbItemsToAdd.Add(new Tuple <uint, string>(item.Id, item.Name));
                            }
                            page++;
                        }
                        else
                        {
                            HandleError.HandleErrors(client.GetLastError(), errorCounter);
                            errorCounter++;
                        }
                    }
                    catch (Exception ex)
                    {
                        Globals.Logger.Error(ex);
                        throw;
                    }
                }
                client.Dispose();
            }
            AddToIdListDb(dbItemsToAdd);
            AddVotelistToDb(voteListItems, removeItems);

            Globals.StatusBar.ProgressText     = "Done";
            Globals.StatusBar.IsDbProcessing   = false;
            Globals.StatusBar.IsWorkProcessing = false;
            await Task.Delay(1500);

            Globals.StatusBar.ProgressText = string.Empty;
            IsUserInputEnabled             = true;
        }