示例#1
0
        public ActionResult Vote(int voteScore, int postId, IndexBlogPostViewModel model)
        {
            var currentUserId = this.User.Identity.GetUserId();

            var vote = this.votes.All().FirstOrDefault(v => v.UserId == currentUserId && v.PostId == postId);

            if (vote == null)
            {
                var newVote = new Vote
                {
                    UserId = currentUserId,
                    PostId = postId,
                    Value  = voteScore
                };

                this.votes.Add(newVote);
                this.votes.SaveChanges();

                var post = this.posts.GetById(postId);
                post.VoteScore += voteScore;
                this.posts.Update(post);
                voteScore = post.VoteScore;

                this.posts.SaveChanges();
            }

            return(this.Json(voteScore));
        }
        // GET: Administration/BlogPosts
        public IActionResult Index(int page = 1)
        {
            var result = this.blogPostService
                         .GetAll <BlogPostViewModel>();

            var count = result.Count();

            var viewModel = new IndexBlogPostViewModel
            {
                BlogPosts   = result.Skip(PostsPerPageDefaultValue * (page - 1)).Take(PostsPerPageDefaultValue),
                PagesCount  = (int)Math.Ceiling((double)count / PostsPerPageDefaultValue),
                CurrentPage = page,
            };

            return(this.View(viewModel));
        }