示例#1
0
        public async Task <IndexSearchServiceModel> Index(string searchTerm)
        {
            var searchItems = SearchHelpers.GetSearchItems(searchTerm);

            var articles = this.articleRepository.All();
            var videos   = this.videoRepository.All();

            foreach (var item in searchItems)
            {
                var tempItem = item;

                articles = articles.Where(p =>
                                          p.Content.ToLower().Contains(tempItem) ||
                                          p.Title.ToLower().Contains(tempItem));

                videos = videos.Where(p =>
                                      p.AssociatedTerms.ToLower().Contains(tempItem) ||
                                      p.Title.ToLower().Contains(tempItem));
            }

            var serviceModel = new IndexSearchServiceModel
            {
                Articles      = await articles.Distinct().Take(DefaultArticlesPerPage).To <ArticleServiceModel>().ToListAsync(),
                Videos        = await videos.Distinct().Take(DefaultVideosPerPage).To <VideoServiceModel>().ToListAsync(),
                ArticlesCount = await articles.CountAsync(),
                VideosCount   = await videos.CountAsync(),
            };

            this.SortOutCommentHierarchy(serviceModel.Videos);

            return(serviceModel);
        }
示例#2
0
        public async Task <VideosSearchServiceModel> GetVideos(string searchTerm, int take = DefaultVideosPerPage, int skip = 0)
        {
            var searchItems = SearchHelpers.GetSearchItems(searchTerm);

            var videosQuery = this.videoRepository.All();

            foreach (var item in searchItems)
            {
                var tempItem = item;

                videosQuery = videosQuery.Where(p =>
                                                p.AssociatedTerms.ToLower().Contains(tempItem) ||
                                                p.Title.ToLower().Contains(tempItem));
            }

            var videosCount = await videosQuery.Distinct().CountAsync();

            // TODO: Why does distinct create error in below query?? now working fine but distinct blows up
            var videos = await videosQuery
                         .Include(a => a.User)
                         .Include(a => a.VideoComments)
                         .Skip(skip)
                         .Take(take)
                         .To <VideoServiceModel>()
                         .ToListAsync();

            this.SortOutCommentHierarchy(videos);

            return(new VideosSearchServiceModel
            {
                Videos = videos,
                VideosCount = videosCount,
            });
        }
示例#3
0
        public async Task <ArticlesSearchServiceModel> GetArticles(string searchTerm, int take = DefaultArticlesPerPage, int skip = 0)
        {
            var searchItems = SearchHelpers.GetSearchItems(searchTerm);

            var articlesQuery = this.articleRepository.All();

            foreach (var item in searchItems)
            {
                var tempItem = item;

                articlesQuery = articlesQuery.Where(p =>
                                                    p.Content.ToLower().Contains(tempItem) ||
                                                    p.Title.ToLower().Contains(tempItem));
            }

            var articlesCount = await articlesQuery.CountAsync();

            var articles = await articlesQuery
                           .Skip(skip)
                           .Take(take)
                           .Select(a => new ArticleServiceModel
            {
                Id        = a.Id,
                Title     = a.Title,
                CreatedOn = a.CreatedOn,
                Content   = a.Content,
                User      = new ApplicationUserServiceModel
                {
                    UserName = a.User.UserName,
                },
                ImageUrl = a.ImageUrl,
            })
                           .ToListAsync();

            return(new ArticlesSearchServiceModel
            {
                Articles = articles,
                ArticlesCount = articlesCount,
            });
        }