public async Task <IActionResult> SetPostStatus(long postId, PostStatusEnum postStatusEnum)
        {
            var session = HttpContext.Session;
            var userId  = session.GetString(CommonConstants.USER_SESSION);

            if (userId == null)
            {
                return(Unauthorized());
            }
            var accessId = Convert.ToInt64(userId);
            var result   = await postService.SetPostStatus(postId, accessId, postStatusEnum);

            return(Ok(result));
        }
Beispiel #2
0
        public void UpdatePostStatus(PostStatusEnum postStatus, string user)
        {
            switch (postStatus)
            {
            case PostStatusEnum.Draft:
                PostStatusId   = (int)postStatus;
                LastUpdateDate = DateTime.Now;
                break;

            case PostStatusEnum.PendingPublishApproval:
                PostStatusId   = (int)postStatus;
                LastUpdateDate = DateTime.Now;
                break;

            case PostStatusEnum.Published:
                PostStatusId   = (int)postStatus;
                LastUpdateDate = DateTime.Now;
                ApprovalDate   = DateTime.Now;
                ApproverName   = user;
                break;
            }
        }
        /// <summary>
        /// Update the status for a post (only editor)
        /// </summary>
        /// <param name="postid">id post</param>
        /// <param name="editorId">id editor that updates</param>
        /// <param name="state">status indicated for post</param>
        /// <returns></returns>
        public ResponseResult <Post> SetStatusPost(Guid postid, Guid editorId, PostStatusEnum state)
        {
            try
            {
                var post = PostRepository.GetById(postid);
                if (post == null)
                {
                    return(ResponseResult <Post> .Error("Post does not exist!."));
                }

                post.IdPublisher = editorId;
                post.State       = (int)state;
                PostRepository.Update(post);
                PostRepository.Save();

                return(ResponseResult <Post> .Success());
            }
            catch (Exception ex)
            {
                return(ResponseResult <Post> .Error(ex.Message));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> SetStatusPost(long postId, PostStatusEnum postStatusEnum)
        {
            var result = await postService.SetPostStatus(postId, postStatusEnum);

            return(Ok(result));
        }
Beispiel #5
0
        /// <summary>
        /// Gets paginated, sorted and filtered list of posts.
        /// </summary>
        /// <param name="context" >An instance of DataContext which is required.</param>
        /// <param name="Limit" >Number of the posts passes through the page an it can be null.</param>
        /// <param name="Offset" >Number of the posts you want to skip before taking requested numbers of records and it can be null.</param>
        /// <param name="Field" >Name of requested column to sort.</param>
        /// <param name="Order" >Order of sorting. It can be 'ascend', 'descend' or empty</param>
        /// <param name="FilterKey" >Name of the column you want to filter.</param>
        /// <param name="FilterValue" >The value you want the posts to be filtered base on that.</param>
        /// <param name="StartDate" >Starting date for range date filter.</param>
        /// <param name="EndDate" >Ending date for range date filter.</param>
        /// <param name="StartNumber" >Starting number for slider filter.</param>
        /// <param name="EndNumber" >Ending number for slider filter.</param>
        public static async Task <HelperTDO> PaginateAndFilterAndSort(DataContext context, int?Limit, int?Offset, string Field, string Order, string FilterKey, string FilterValue, DateTime?StartDate, DateTime?EndDate, int?StartNumber, int?EndNumber)
        {
            var queryable = context.Posts.Where(x => x.Type == PostTypeEnum.Posts).AsQueryable();
            var postCount = queryable.Count();

            List <Post> posts;

            // Sorting and pagination logic
            if (Field != null && Order != null && Order != null && FilterValue == null)
            {
                switch (Field)
                {
                case "title":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.Title)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.Title)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "postCategory":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.TaxonomyPosts.FirstOrDefault(x => x.Taxonomy.Type == TaxonomyTypeEnum.category))
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.TaxonomyPosts.FirstOrDefault(x => x.Taxonomy.Type == TaxonomyTypeEnum.category))
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "postStatus":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.PostStatus)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.PostStatus)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "postAttachments":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.PostAttachments.Count)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.PostAttachments.Count)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "commentAllowed":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.CommentAllowed)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CommentAllowed)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "viewCount":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.ViewCount)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ViewCount)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "comments":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.Comments.Count)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.Comments.Count)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "createdAt":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.CreatedAt)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "createdBy":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.CreatedBy)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedBy)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "modifiedAt":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.ModifiedAt)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ModifiedAt)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "modifiedBy":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.ModifiedBy)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ModifiedBy)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                case "userIPAddress":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.UserIPAddress)
                            .ToListAsync();

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.UserIPAddress)
                            .ToListAsync();

                        break;

                    default:
                        posts =
                            await queryable
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ThenByDescending(x => x.ModifiedAt)
                            .ThenByDescending(x => x.Title)
                            .ToListAsync();

                        break;
                    }
                    break;

                default:
                    posts =
                        await queryable
                        .Skip(Offset ?? 0)
                        .Take(Limit ?? 3)
                        .OrderByDescending(x => x.CreatedAt)
                        .ThenByDescending(x => x.ModifiedAt)
                        .ThenByDescending(x => x.Title)
                        .ToListAsync();

                    break;
                }
            }

            // Filtering and pagination logic
            else if (FilterKey != null && FilterValue != null)
            {
                switch (FilterKey)
                {
                case "title":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.Title.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.Title)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.Title.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.Title)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.Title.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.Title)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "postCategory":
                    switch (Order)
                    {
                    case "ascend":
                        posts = await(from p in context.Posts
                                      from tp in p.TaxonomyPosts
                                      where tp.Taxonomy.Type == TaxonomyTypeEnum.category
                                      where tp.Taxonomy.Term.Name.ToLower().Contains(FilterValue.ToLower())
                                      orderby p.Title
                                      select p).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts = await(from p in context.Posts
                                      from tp in p.TaxonomyPosts
                                      where tp.Taxonomy.Type == TaxonomyTypeEnum.category
                                      where tp.Taxonomy.Term.Name.ToLower().Contains(FilterValue.ToLower())
                                      orderby p.Title descending
                                      select p).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await(from p in context.Posts
                                      from tp in p.TaxonomyPosts
                                      where tp.Taxonomy.Type == TaxonomyTypeEnum.category
                                      where tp.Taxonomy.Term.Name.ToLower().Contains(FilterValue.ToLower())
                                      orderby p.Title
                                      select p).ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "createdBy":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.CreatedBy.UserName.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.CreatedBy.UserName)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.CreatedBy.UserName.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedBy.UserName)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.CreatedBy.UserName.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedBy.UserName)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "modifiedBy":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.ModifiedBy.UserName.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.ModifiedBy.UserName)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.ModifiedBy.UserName.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ModifiedBy.UserName)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.ModifiedBy.UserName.ToLower().Contains(FilterValue.ToLower()))
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ModifiedBy.UserName)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "userIPAddress":
                    switch (Order)
                    {
                    case "ascend":
                        posts = await queryable.Where(x => x.UserIPAddress == FilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts = await queryable.Where(x => x.UserIPAddress == FilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.Where(x => x.UserIPAddress == FilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "device":
                    switch (FilterValue)
                    {
                    case "desktop":
                        posts = await queryable.Where(x => !x.UserAgent.ToLower().Contains("mobile") && !x.UserAgent.ToLower().Contains("ipad")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "tablet":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("ipad") ||
                                                      x.UserAgent.ToLower().Contains("tablet") ||
                                                      (x.UserAgent.ToLower().Contains("android") &&
                                                       !x.UserAgent.ToLower().Contains("mobile"))
                                                      )
                                .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "mobile":
                        posts = await queryable.Where(x => (x.UserAgent.ToLower().Contains("mobile") &&
                                                            x.UserAgent.ToLower().Contains("android")) ||
                                                      x.UserAgent.ToLower().Contains("iphone")
                                                      ).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "os":
                    switch (FilterValue)
                    {
                    case "macOS":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("macintosh")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "windows":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("windows")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "linux":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("linux") && !x.UserAgent.ToLower().Contains("android")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "iPadOS":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("ipad")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "iPhoneOS":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("iphone")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "android":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("android")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "browser":
                    switch (FilterValue)
                    {
                    case "chrome":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("chrome") && !x.UserAgent.ToLower().Contains("edge")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "safari":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("safari") &&
                                                      !x.UserAgent.ToLower().Contains("chrome") &&
                                                      !x.UserAgent.ToLower().Contains("android")
                                                      ).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "firefox":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("firefox")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "edge":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("edge")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "IE":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("trident") || x.UserAgent.ToLower().Contains("msie")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "opera":
                        posts = await queryable.Where(x => x.UserAgent.ToLower().Contains("opr") || x.UserAgent.ToLower().Contains("opera")).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "postStatus":
                    PostStatusEnum _valueToEnum = (PostStatusEnum)Enum.Parse(typeof(PostStatusEnum), FilterValue);
                    switch (Order)
                    {
                    case "ascend":

                        posts = await queryable.Where(x => x.PostStatus == _valueToEnum).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts = await queryable.Where(x => x.PostStatus == _valueToEnum).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.Where(x => x.PostStatus == _valueToEnum).ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "commentAllowed":
                    bool commentAllowedFilterValue = Convert.ToBoolean(FilterValue);
                    switch (Order)
                    {
                    case "ascend":

                        posts = await queryable.Where(x => x.CommentAllowed == commentAllowedFilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts = await queryable.Where(x => x.CommentAllowed == commentAllowedFilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.Where(x => x.CommentAllowed == commentAllowedFilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "pinned":
                    bool pinFilterValue = Convert.ToBoolean(FilterValue);
                    switch (Order)
                    {
                    case "ascend":

                        posts = await queryable.Where(x => x.IsPinned == pinFilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts = await queryable.Where(x => x.IsPinned == pinFilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts = await queryable.Where(x => x.IsPinned == pinFilterValue).ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                default:
                    posts =
                        await queryable
                        .Skip(Offset ?? 0)
                        .Take(Limit ?? 3)
                        .OrderByDescending(x => x.CreatedAt)
                        .ThenByDescending(x => x.ModifiedAt)
                        .ThenByDescending(x => x.Title)
                        .ToListAsync();

                    break;
                }
            }
            else if (FilterKey != null && StartDate != null && EndDate != null)
            {
                switch (FilterKey)
                {
                case "createdAt":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.CreatedAt >= StartDate && x.CreatedAt <= EndDate)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.CreatedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.CreatedAt >= StartDate && x.CreatedAt <= EndDate)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.CreatedAt >= StartDate && x.CreatedAt <= EndDate)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.CreatedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "modifiedAt":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.ModifiedAt >= StartDate && x.CreatedAt <= EndDate)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.ModifiedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.ModifiedAt >= StartDate && x.CreatedAt <= EndDate)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ModifiedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.ModifiedAt >= StartDate && x.CreatedAt <= EndDate)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ModifiedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                default:
                    posts =
                        await queryable
                        .Skip(Offset ?? 0)
                        .Take(Limit ?? 3)
                        .OrderByDescending(x => x.CreatedAt)
                        .ThenByDescending(x => x.ModifiedAt)
                        .ThenByDescending(x => x.Title)
                        .ToListAsync();

                    break;
                }
            }
            else if (FilterKey != null && StartNumber != null && EndNumber != null)
            {
                switch (FilterKey)
                {
                case "postAttachments":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.PostAttachments.Count >= StartNumber && x.PostAttachments.Count <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.PostAttachments.Count)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.PostAttachments.Count >= StartNumber && x.PostAttachments.Count <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.PostAttachments.Count)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.PostAttachments.Count >= StartNumber && x.PostAttachments.Count <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.PostAttachments.Count)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "viewCount":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.ViewCount >= StartNumber && x.ViewCount <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.ViewCount)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.ViewCount >= StartNumber && x.ViewCount <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ViewCount)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.ViewCount >= StartNumber && x.ViewCount <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.ViewCount)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                case "comments":
                    switch (Order)
                    {
                    case "ascend":
                        posts =
                            await queryable
                            .Where(x => x.Comments.Count >= StartNumber && x.Comments.Count <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderBy(x => x.CreatedAt)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    case "descend":
                        posts =
                            await queryable
                            .Where(x => x.Comments.Count >= StartNumber && x.Comments.Count <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.Comments.Count)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;

                    default:
                        posts =
                            await queryable
                            .Where(x => x.Comments.Count >= StartNumber && x.Comments.Count <= EndNumber)
                            .Skip(Offset ?? 0)
                            .Take(Limit ?? 3)
                            .OrderByDescending(x => x.Comments.Count)
                            .ToListAsync();

                        postCount = posts.Count;

                        break;
                    }
                    break;

                default:
                    posts =
                        await queryable
                        .Skip(Offset ?? 0)
                        .Take(Limit ?? 3)
                        .OrderByDescending(x => x.CreatedAt)
                        .ThenByDescending(x => x.ModifiedAt)
                        .ThenByDescending(x => x.Title)
                        .ToListAsync();

                    break;
                }
            }
            else
            {
                posts =
                    await queryable
                    .Skip(Offset ?? 0)
                    .Take(Limit ?? 3)
                    .OrderByDescending(x => x.CreatedAt)
                    .ThenByDescending(x => x.ModifiedAt)
                    .ThenByDescending(x => x.Title)
                    .ToListAsync();
            }

            return(new HelperTDO
            {
                PostsEnvelope = posts,
                PostCount = postCount
            });
        }
        public ActionResult PostComment(int postId, string content, string type = "comment")
        {
            PostStatusEnum status = PostStatusEnum.Active;

            if (type == "comment")
            {
                status = dService.GetDiscussion(postId).Status.Value;
            }
            else
            {
                status = cService.GetComment(postId, "Post").Post.Status.Value;
            }

            if (status != PostStatusEnum.Closed)
            {
                Comment comment = new Comment();
                comment.Content        = content;
                comment.UserId         = User.Identity.GetUserId <int>();
                comment.CreatedDate    = DateTime.Now;
                comment.LastEditedDate = comment.CreatedDate;
                comment.PostId         = postId;
                comment.Status         = PostStatusEnum.Active;
                comment.VoteDown       = 0;
                comment.VoteUp         = 0;

                cService.CommentPost(comment);
                List <Comment> comments = cService.GetComments(postId);
                ICollection <CommentViewModel> commentsVM;
                Discussion   discussion;
                Notification notification;
                switch (type)
                {
                //case "comment":
                //    dService.IncludeUserForComments(comments);
                //    dService.IncludeReplyForComments(comments);
                //    commentsVM = comments.Select(Mapper.Map<Comment, CommentViewModel>).ToList();
                //    return PartialView("../CommonWidget/_CommentListPartialView", commentsVM);
                case "reply":
                    dService.IncludeUserForComments(comments);
                    commentsVM = comments.Select(Mapper.Map <Comment, CommentViewModel>).ToList();

                    Comment cm = cService.GetComment(comment.PostId);
                    if (cm.UserId != _currentUserId)
                    {
                        discussion               = dService.GetDiscussion(cm.PostId);
                        notification             = new Notification();
                        notification.AuthorId    = _currentUserId;
                        notification.CreatedDate = DateTime.Now;
                        notification.Content     = discussion.Title;
                        notification.Seen        = false;
                        notification.Type        = NotificationSettingEnum.UserComment;
                        notification.UserId      = cm.UserId;
                        notification.Link        = Url.Action("Detail", "Discussion", new { id = discussion.Id });
                        cService.AddNotification(notification);

                        using (RealTimeService rService = new RealTimeService(new CPMathHubModelContainer(), notification.UserId))
                        {
                            IEnumerable <string> connectionIds = RealTimeHub.Connections.GetConnections(notification.UserId);
                            foreach (string conId in connectionIds)
                            {
                                _hub.Clients.Client(conId).notifyNewActivity(rService.CountNewActivityNotification());
                            }
                        }
                    }

                    return(PartialView("../CommonWidget/_ReplyListPartialView", commentsVM));

                default:
                    dService.IncludeUserForComments(comments);
                    dService.IncludeReplyForComments(comments);
                    commentsVM = comments.Select(Mapper.Map <Comment, CommentViewModel>).ToList();

                    discussion = dService.GetDiscussion(comment.PostId);
                    if (discussion.UserId != _currentUserId)
                    {
                        notification             = new Notification();
                        notification.AuthorId    = _currentUserId;
                        notification.CreatedDate = DateTime.Now;
                        notification.Content     = discussion.Title;
                        notification.Seen        = false;
                        notification.Type        = NotificationSettingEnum.UserCommentMainPost;
                        notification.UserId      = discussion.UserId;
                        notification.Link        = Url.Action("Detail", "Discussion", new { id = discussion.Id });
                        cService.AddNotification(notification);

                        using (RealTimeService rService = new RealTimeService(new CPMathHubModelContainer(), notification.UserId))
                        {
                            IEnumerable <string> connectionIds = RealTimeHub.Connections.GetConnections(notification.UserId);
                            foreach (string conId in connectionIds)
                            {
                                _hub.Clients.Client(conId).notifyNewActivity(rService.CountNewActivityNotification());
                            }
                        }
                    }
                    return(PartialView("../CommonWidget/_CommentListPartialView", commentsVM));
                }
            }
            return(null);
        }