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() }); }
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))); }
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)); } }
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")); } }