예제 #1
0
        public async Task <PostsWithCommentsAndVotesModel> GetPostWithCommentsAndVotes(int postPage, int commentPage)
        {
            var posts = await _repository.GetAllPosts((postPage - 1) * 20, 20);

            var totalPosts = await _repository.AllPostCount();

            var postIds  = posts.Select(s => s.Id).ToArray();
            var comments = await _commentService.GetAllComments(postIds, (commentPage - 1) * 5, 5);

            var votes = await _voteService.GetVote(comments.Select(s => s.Id).ToArray());

            return(new PostsWithCommentsAndVotesModel
            {
                TotalPost = totalPosts,
                Posts = posts.Select(s => new Post
                {
                    PostData = s.Post,
                    PostedDate = s.InsertedOn,
                    TotalComment = _commentService.GetCommentCount(s.Id).Result,
                    PostId = s.Id,
                    UserName = s.InsertedBy.ToString(),
                    Comments = comments.Where(w => w.PostId == s.Id).Select(c => new Comment
                    {
                        CommentData = c.Comment,
                        CommentId = c.Id,
                        PostedDate = c.InsertedOn,
                        UpVotes = votes.Where(w => w.IsUpVoted && w.CommentId == c.Id).ToArray().Count(),
                        DownVotes = votes.Where(w => !w.IsUpVoted && w.CommentId == c.Id).ToArray().Count(),
                        UserName = c.InsertedBy.ToString()
                    }).ToList()
                }).ToList()
            });
        }
예제 #2
0
        public ActionResult VoteBook(int id = 0)
        {
            BVote vote = voteService.GetVote(id);

            vote.Votes += 1;
            voteService.CreateOrUpdate(vote);
            return(PartialView(AutoMapper <IEnumerable <BVote>, List <VoteModel> > .Map(voteService.GetVotes)));
        }
예제 #3
0
 public ActionResult <VoteModel> GetVote(int songId, int id)
 {
     try
     {
         return(Ok(service.GetVote(songId, id)));
     }
     catch (NotFoundException ex)
     {
         return(NotFound(ex.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
     }
 }
예제 #4
0
        public async Task <IActionResult> Delete(VoteModel model)
        {
            try
            {
                var user = await GetCurrentUser();

                Vote vote;
                if (model.Id != 0)
                {
                    vote = await _voteService.GetVote(model.Id);
                }
                else
                {
                    vote = await _voteService.GetUserVoteForArticle(user.Id, model.ArticleId, model.VoteType);
                }

                if (vote == null)
                {
                    _logger.LogInformation("Vote not found");
                    return(NotFound("Vote not found"));
                }

                _dbContext.Votes.Remove(vote);
                await _dbContext.SaveChangesAsync();

                await _voteService.UpdateArticleScore(model.ArticleId);

                var artVoteCount = await GetVoteCountForArticle(model.ArticleId, model.VoteType);

                var score = (await _dbContext.Articles.FindAsync(model.ArticleId))?.Score;

                var voteResultModel = new
                {
                    model.ArticleId,
                    model.VoteType,
                    VoteCount = artVoteCount,
                    Score     = score
                };

                return(Ok(voteResultModel));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error deleting vote", model);

                return(StatusCode((int)HttpStatusCode.InternalServerError, "Error deleting vote"));
            }
        }