예제 #1
0
        public IHttpActionResult GetTags([FromUri] int?draw, [FromUri] int?start, [FromUri] int?length)
        {
            IQueryable <Tag> query = db.Tags.Include("Posts");

            int recordsTotal = 0;

            //Partitioning from [start] take [length] objects
            if (start != null && length != null)
            {
                recordsTotal = query.Count(); //total objects
                query        = query.OrderBy(x => x.Name).Skip((int)start).Take((int)length);
            }

            var tagsDtos = query
                           .ToList()
                           .Select(x => new {
                TagId    = x.TagId,
                Name     = x.Name,
                Slug     = x.Slug,
                NumPosts = x.Posts.Count
            });

            var list = new TablePartitioningResponseViewModels
            {
                draw            = (int)draw,
                recordsTotal    = recordsTotal,
                recordsFiltered = recordsTotal,
                aaData          = tagsDtos
            };

            return(Ok(list));
        }
예제 #2
0
        public IHttpActionResult GetPosts([FromUri] int?draw, [FromUri] int?start, [FromUri] int?length, [FromUri] string content, [FromUri] int?tag, [FromUri] int?blog)
        {
            IQueryable <Post> query = db.Posts.Include("Like").Include("Blogger").Include("Tags");

            //filter by tag
            if (tag != null && tag != 0)
            {
                query = query.Where(x => x.Tags.Select(t => t.TagId).Contains((int)tag));
            }

            //filter by Content
            if (content != null && content != "")
            {
                query = query.Where(x => x.Content.Contains(content));
            }

            //filter by blog
            if (blog != null && blog != 0)
            {
                query = query.Where(x => x.BlogId == blog);
            }

            int recordsTotal = 0;

            //Partitioning from [start] take [length] objects
            if (start != null && length != null)
            {
                recordsTotal = query.Count(); //total object
                query        = query.OrderByDescending(x => x.LastUpdated).Skip((int)start).Take((int)length);
            }

            ApplicationUser user = db.Users.SingleOrDefault(c => c.UserName == User.Identity.Name);

            var postsDtos = query
                            .ToList()
                            .Select(x => new {
                PostId      = x.PostId,
                BloggerName = x.Blogger.Name,
                Title       = x.Title,
                Content     = x.Content,
                LastUpdated = x.LastUpdated.ToShortDateString(),
                TagNames    = string.Join(", ", x.Tags.Select(t => t.Name)),
                CanMakeLike = !x.Like.Contains(user),
                LikeCount   = x.Like.Count
            });

            var list = new TablePartitioningResponseViewModels
            {
                draw            = (int)draw,
                recordsTotal    = recordsTotal,
                recordsFiltered = recordsTotal,
                aaData          = postsDtos
            };

            return(Ok(list));
        }
예제 #3
0
        public ActionResult Index(int?id, [System.Web.Http.FromUri] string content)
        {
            IQueryable <Post> query = db.Posts.Include("Like").Include("Blogger").Include("Tags").Include("Comment");

            //filter by Content
            if (content != null && content != "")
            {
                query = query.Where(x => x.Content.Contains(content));
            }

            int recordsTotal = 0;
            //Partitioning from [start] take [length] objects
            int start  = id == null || id < 1 ? 0 : ((int)id - 1) * 10;
            int length = 10;

            recordsTotal = query.Count(); //total object
            query        = query.OrderByDescending(x => x.LastUpdated).Skip(start).Take(length);

            ApplicationUser user = db.Users.SingleOrDefault(c => c.UserName == User.Identity.Name);

            var listPost = query
                           .ToList()
                           .Select(x => new PostViewModel
            {
                PostId       = x.PostId,
                BloggerName  = x.Blogger.Name,
                Title        = x.Title,
                Content      = x.Content,
                LastUpdated  = x.LastUpdated.ToShortDateString(),
                TagNames     = string.Join(", ", x.Tags.Select(t => t.Name)),
                CanMakeLike  = !x.Like.Contains(user),
                LikeCount    = x.Like.Count,
                CommentCount = x.Comment.Count
            });

            //list to show lasts most liked Post
            var listPostMostLike = db.Posts.Include("Like").Include("Blogger").Include("Tags").Include("Comment")
                                   .OrderByDescending(x => x.LastUpdated.Year)
                                   .ThenByDescending(x => x.LastUpdated.Month)
                                   .ThenByDescending(x => x.LastUpdated.Day)
                                   .ThenByDescending(x => x.Like.Count).Skip(0).Take(3)
                                   .ToList()
                                   .Select(x => new PostViewModel
            {
                PostId      = x.PostId,
                BloggerName = x.Blogger.Name,
                Title       = x.Title,
                Content     = x.Content,
                LastUpdated = x.LastUpdated.ToShortDateString(),
                TagNames    = string.Join(", ", x.Tags.Select(t => t.Name)),
                CanMakeLike = !x.Like.Contains(user),
                LikeCount   = x.Like.Count
            });

            var listToReturn = new TablePartitioningResponseViewModels
            {
                draw         = id == null || id < 1 ? 1 : (int)id,
                recordsTotal = recordsTotal,
                aaData       = listPost,
                aaData1      = listPostMostLike,
                content      = content
            };

            return(View(listToReturn));
        }