Esempio n. 1
0
        public List <PostViewModel> GetPosts(PostSearchMidel searchModel)
        {
            var searchedPosts = _postService.GetPosts(searchModel);

            return(searchedPosts);
        }
Esempio n. 2
0
        public List <PostViewModel> GetPosts(PostSearchMidel searchModel)
        {
            var results = new List <PostViewModel>();
            var query   = _repository.GetList <Post>(x => x.Status == (int)PostStatus.Approved, i => i.Include(x => x.PostTags.Select(y => y.Tag)).Include(x => x.User).Include(x => x.PostMediaContents.Select(y => y.MediaContent)).Include(x => x.PostRatings));

            if (searchModel == null)
            {
                return(query.ToList().Select(x => CreatePostViewModel(x, 1)).OrderByDescending(x => x.CreatedOn).ToList());
            }
            if (searchModel.TagId != null)
            {
                query   = query.Where(x => x.PostTags.Any(y => y.TagId == searchModel.TagId));
                results = query.ToList().Select(x => CreatePostViewModel(x, 1)).ToList();
            }
            if (!string.IsNullOrEmpty(searchModel.SaerchText))
            {
                var splitedText = searchModel.SaerchText.Split(' ');
                var splitedBy4  = new List <string>();
                var matches     = Regex.Matches(searchModel.SaerchText, @"\w+ \w+ \w+ \w+");
                foreach (var match in matches)
                {
                    splitedBy4.Add(match.ToString());
                }

                var searchByHeader    = query.Where(x => x.Tittle.Contains(searchModel.SaerchText));
                var searchByInnerText = query.Where(x => x.ContentText.Contains(searchModel.SaerchText));
                var searchByTags      = query.Where(x => x.PostTags.Any(y => splitedText.Any(s => s == y.Tag.Name)));
                var union             = searchByHeader.Union(searchByInnerText).Union(searchByTags);
                foreach (var oneSplitedPiece in splitedBy4)
                {
                    var searchByPieces = query.Where(x => x.ContentText.Contains(oneSplitedPiece));
                    union = union.Union(searchByPieces);
                }
                results = union.Distinct().ToList().Select(x => CreatePostViewModel(x, 1)).ToList();
            }
            if (searchModel.DisplayType.HasValue)
            {
                results = query.ToList().Select(x => CreatePostViewModel(x, 1)).ToList();
                switch (searchModel.DisplayType.Value)
                {
                case PostDisplayType.Hot:
                {
                    results =
                        results.Where(x => x.CreatedOn > DateTime.Now.AddDays(-1))
                        .OrderByDescending(x => x.Rating)
                        .ThenByDescending(x => x.CreatedOn)
                        .ToList();
                    break;
                }

                case PostDisplayType.Fresh:
                {
                    results = results.OrderByDescending(x => x.CreatedOn).ToList();
                    break;
                }

                case PostDisplayType.Best:
                {
                    results = results.OrderByDescending(x => x.Rating).ToList();
                    break;
                }
                }
            }
            else
            {
                results = results.OrderBy(x => x.CreatedOn).ToList();
            }

            return(results);
        }