Esempio n. 1
0
        public async Task <IEnumerable <Post> > GetListByFilter(PostSearchFilter filter)
        {
            try
            {
                var query = db.Posts
                            .Include(f => f.User)
                            .Include(f => f.PostComments)
                            .ThenInclude(y => y.CommentVotings)
                            .Where(d =>
                                   filter.SearchTerm == null || filter.SearchTerm == "" ||
                                   d.PostDescription.Contains(filter.SearchTerm))
                ;

                filter.TotalCount = await query.CountAsync();

                var finalQuery = query.OrderByDescending(f => f.CreatedDate);

                var posts = await finalQuery.Skip((filter.PageNumber - 1) *filter.PageSize)
                            .Take(filter.PageSize)
                            .ToListAsync();

                return(posts);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> GetPostByFilter([FromForm] PostSearchFilter filter)
        {
            var posts = await _postService.GetListByFilter(filter);

            var currentUser = CurrentLoginUser;

            var model = new PostListViewModel
            {
                Posts        = posts,
                SearchFilter = filter
            };

            return(Ok(new
            {
                model = model
            }));
        }