Example #1
0
 private void Init()
 {
     PivotIndex   = 0;
     GalleryVM    = new GalleryViewModel();
     SubredditsVM = new SubredditsViewModel();
     SearchVM     = new SearchViewModel(SubredditsVM);
     AccountVM    = new AccountViewModel();
 }
        public async Task <SubredditsViewModel> GetOrderedSubredditsFilterByKeyWordsAsync(
            string[] keyWords,
            SubredditSortType sortType)
        {
            var sortStrategy = SortSubredditStrategyFactory
                               .GetSubredditSortStrategy(this.redditCloneUnitOfWork, sortType);

            var filteredSubreddits = await this.redditCloneUnitOfWork.Subreddits
                                     .GetByKeyWordsSortedByAsync(keyWords, sortStrategy);

            var model = new SubredditsViewModel()
            {
                SubrreditSortType = sortType,
                Subreddits        = this.mapper.Map <IEnumerable <SubredditConciseViewModel> >(filteredSubreddits)
            };

            return(model);
        }
        public IActionResult _Subreddits(SubredditPageViewModel vm)
        {
            if (!HasPasswordAlready())
            {
                return(RedirectToAction("Index", "Password", new { redirectTo = "Subreddits" }));
            }

            IQueryable <RedditPost> postsQuery = from m in _redditPostContext.RedditPost select m;
            var subredditNames = postsQuery.Select(post => post.Subreddit).Distinct().OrderBy(subredditName => subredditName.ToLower()).ToList();

            var iconQuery = from m in _subredditInfoContext.SubredditInfo select m;
            var subredditsAlreadyStored = iconQuery.Select(subreddit => subreddit.SubredditName).ToList();
            var subredditsToRetrieve    = subredditNames.Where(subName => !subredditsAlreadyStored.Contains(subName)).ToList();

            foreach (string subredditName in subredditsToRetrieve)
            {
                GetSubredditInfo(subredditName);
            }

            SubredditsViewModel itemsVm = CreateViewModel(postsQuery, vm.SortingSetting, vm.NsfwSetting);

            return(PartialView(itemsVm));
        }
        private SubredditsViewModel CreateViewModel(IQueryable <RedditPost> postsQuery, SubredditSortingSettings sortingSetting, NsfwSettings nsfwSetting)
        {
            var iconQuery = from m in _subredditInfoContext.SubredditInfo select m;

            if (GetCookieString("AllowNsfw") == "false")
            {
                iconQuery = iconQuery.Where(sub => !sub.IsNsfw);
            }

            switch (nsfwSetting)
            {
            case NsfwSettings.No_Nsfw:
                iconQuery = iconQuery.Where(sub => !sub.IsNsfw);
                break;

            case NsfwSettings.Nsfw_Only:
                iconQuery = iconQuery.Where(sub => sub.IsNsfw);
                break;
            }

            Dictionary <SubredditInfo, int> subredditCountDict = new Dictionary <SubredditInfo, int>();

            List <SubredditInfo> mostUpvotedInfo = new List <SubredditInfo>()
            {
                Utility.MakeDefaultSubredditInfo()
            };
            List <SubredditInfo> leastUpvotedInfo = new List <SubredditInfo>()
            {
                Utility.MakeDefaultSubredditInfo()
            };

            int mostUpvotedCount  = -1;
            int leastUpvotedCount = 999999;
            int totalSubCount     = 0;

            foreach (var subreddit in iconQuery)
            {
                totalSubCount++;
                int count = postsQuery.Count(post => post.Subreddit == subreddit.SubredditName);

                if (count >= mostUpvotedCount)
                {
                    if (count > mostUpvotedCount)
                    {
                        mostUpvotedInfo.Clear();
                    }

                    mostUpvotedCount = count;
                    mostUpvotedInfo.Add(subreddit);
                }

                else if (count <= leastUpvotedCount)
                {
                    if (count < leastUpvotedCount)
                    {
                        leastUpvotedInfo.Clear();
                    }

                    leastUpvotedCount = count;
                    leastUpvotedInfo.Add(subreddit);
                }

                subredditCountDict.Add(subreddit, count);
            }

            var iconEnumerable = iconQuery.AsEnumerable();

            switch (sortingSetting)
            {
            case SubredditSortingSettings.Alphabetical:
                iconEnumerable = iconEnumerable.OrderBy(subreddit => subreddit.SubredditName.ToLower());
                break;

            case SubredditSortingSettings.Reverse_Alphabetical:
                iconEnumerable = iconEnumerable.OrderByDescending(subreddit => subreddit.SubredditName.ToLower());
                break;

            case SubredditSortingSettings.Count:
                iconEnumerable = iconEnumerable.OrderByDescending(subreddit => subredditCountDict.GetValueOrDefault(subreddit));
                break;

            case SubredditSortingSettings.Reverse_Count:
                iconEnumerable = iconEnumerable.OrderBy(subreddit => subredditCountDict.GetValueOrDefault(subreddit));
                break;
            }

            SubredditsViewModel vm = new SubredditsViewModel
            {
                Subreddits          = iconEnumerable.ToList(),
                SubredditCountDict  = subredditCountDict,
                MostUpvoted         = mostUpvotedInfo,
                MostUpvotedCount    = mostUpvotedCount,
                LeastUpvoted        = leastUpvotedInfo,
                LeastUpvotedCount   = leastUpvotedCount,
                TotalSubredditCount = totalSubCount
            };

            return(vm);
        }
 public SearchViewModel(SubredditsViewModel subredditsVM, GalaSoft.MvvmLight.Views.INavigationService nav) : base(subredditsVM, nav, DesignMode.DesignModeEnabled)
 { }