Пример #1
0
        public static IQueryable <Post> Filtering(this IQueryable <Post> posts, PostListCriterion criterion)
        {
            if (criterion.FilterBy == null)
            {
                return(posts);
            }
            switch (criterion.FilterBy)
            {
            case FilterBy.Title:
                if (criterion.ExactMatch)
                {
                    posts = posts.Where(x => string.Compare(x.Title, criterion.SearchText) == 0);
                }
                else
                {
                    posts = posts.Where(x => x.Title.Contains(criterion.SearchText));
                }
                break;

            case FilterBy.Author:
                if (criterion.ExactMatch)
                {
                    posts = posts.Where(x => string.Compare(x.UserProfile.Username, criterion.SearchText) == 0);
                }
                else
                {
                    posts = posts.Where(x => x.UserProfile.Username.Contains(criterion.SearchText));
                }
                break;
            }
            return(posts);
        }
Пример #2
0
 public async Task <IEnumerable <PostPreviewDTO> > GetPostsAsync(PostListCriterion criterion)
 {
     if (criterion == null)
     {
         criterion = new PostListCriterion();
     }
     return(await Task.Run(() =>
                           _dataStore.Posts.GetAll()
                           .Filtering(criterion)
                           .Sorting(criterion)
                           .Paging(criterion.PageNumber, criterion.PageSize)
                           .Select(x => x.GetDTO())));
 }
Пример #3
0
        public static IQueryable <Post> Sorting(this IQueryable <Post> posts, PostListCriterion criterion)
        {
            switch (criterion.SortingBy)
            {
            case SortingBy.Newer:
                return(posts.OrderByDescending(x => x.Date));

            case SortingBy.Older:
                return(posts.OrderBy(x => x.Date));

            case SortingBy.Popular:
                return(posts.OrderByDescending(x => x.Visits));
            }
            return(posts);
        }