Ejemplo n.º 1
0
 public IQueryable <Post> GetAll(PostCategoryType postCategoryType)
 {
     return(_context.Posts
            .Include(x => x.PostCategory)
            .Include(x => x.User)
            .Where(x => x.PostCategory.Type == postCategoryType)
            .AsQueryable());
 }
Ejemplo n.º 2
0
        public PagedList<Post> GetPosts(
            PostCategoryType postCategoryType, PostSearch postSearchDto = null, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _unitOfWork.PostRepository.GetAll(postCategoryType);

            if (postSearchDto != null)
            {
                if (postSearchDto.PostCategoryId != null)
                {
                    query = query.Where(x => x.PostCategoryId == postSearchDto.PostCategoryId);
                }

                if (!string.IsNullOrWhiteSpace(postSearchDto.PostTag))
                {
                    query = query.Where(x => x.Tags.Contains(postSearchDto.PostTag));
                }

                if (!string.IsNullOrWhiteSpace(postSearchDto.SearchString))
                {
                    string searchString = postSearchDto.SearchString?.ToLower();
                    query = query.Where(
                            x => (x.Title.Contains(searchString) ||
                                  x.Tags.Contains(searchString) ||
                                  x.PostCategory.Title.Contains(searchString) ||
                                  x.Body.Contains(searchString))
                        );
                }

                switch (postSearchDto.PostSortFilterType)
                {
                    case PostSortFilterType.SortByDateAsc:
                        query = query.OrderBy(x => x.CreateDate);
                        break;
                    case PostSortFilterType.SortByDateDesc:
                        query = query.OrderByDescending(x => x.CreateDate);
                        break;
                    default:
                        query = query.OrderByDescending(x => x.CreateDate);
                        break;
                }
            }
            else
            {
                query = query.OrderByDescending(x => x.CreateDate);
            }

            return new PagedList<Post>(query, pageIndex, pageSize);
        }
 public IQueryable <PostCategory> GetAll(PostCategoryType postCategoryType)
 {
     return(_context.PostCategories
            .Where(x => x.Type == postCategoryType)
            .AsQueryable());
 }