private IEnumerable <Post> SortPosts(PagingQueryPostRequest request, IEnumerable <Post> posts) { if (!string.IsNullOrEmpty(request.Order) && request.Order == AppConstant.ASC) { posts = posts.OrderBy(x => x.CreatedTime); } else { posts = posts.OrderByDescending(x => x.CreatedTime); } return(posts); }
private List <T> GetPagedPosts(string userId, PagingQueryPostRequest request, string platform, out int total) { IEnumerable <Post> posts; try { posts = _postService.Include(x => x.Category).Include(x => x.Images).Include(x => x.ProvinceCity).Include(x => x.User).Include(x => x.Requests).Include(x => x.Comments); posts = SortPosts(request, posts); } catch { throw new InternalServerErrorException(CommonConstant.Error.InternalServerError); } if (string.IsNullOrEmpty(userId)) { if (platform == WebConstant.Platform.CMS) { //display Posts that were not deleted to Admin in CMS posts = posts.Where(x => x.EntityStatus != EntityStatus.Deleted); } else { //display Posts that weren't deleted and their categories have activated status to User in App's newfeed posts = posts.Where(x => x.EntityStatus != EntityStatus.Deleted & x.Category.EntityStatus == EntityStatus.Activated); } } else { try { Guid id = Guid.Parse(userId); posts = posts.Where(x => x.EntityStatus != EntityStatus.Deleted && x.UserId == id); } catch { throw new BadRequestException(CommonConstant.Error.NotFound); } } //filter post by properties posts = FilterPost(request, posts); total = posts.Count(); return(posts .Skip(request.Limit * (request.Page - 1)) .Take(request.Limit) .Select(post => Mapper.Map <T>(post)) .ToList()); }
private IEnumerable <Post> FilterPost(PagingQueryPostRequest request, IEnumerable <Post> posts) { if (!string.IsNullOrEmpty(request.Title)) { posts = posts.Where(x => x.Title.Contains(request.Title)); } if (!string.IsNullOrEmpty(request.ProvinceCityId)) { posts = posts.Where(x => x.ProvinceCityId.Equals(Guid.Parse(request.ProvinceCityId))); } if (!string.IsNullOrEmpty(request.CategoryId)) { posts = posts.Where(x => x.CategoryId.Equals(Guid.Parse(request.CategoryId))); } return(posts); }