示例#1
0
        public async Task <IActionResult> PutAnswerVote(AnswerVoteViewModel model)
        {
            if (model.Vote != 1 && model.Vote != -1)
            {
                return(BadRequest());
            }

            var user = await userManager.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                return(Unauthorized());
            }

            var answer = await _context.Answers.Where(x => x.ID == model.AnswerId).FirstOrDefaultAsync();

            if (answer == null)
            {
                return(NotFound());
            }

            var vote = await _context.AnswerVotes.Where(x => x.UserId == user.Id && x.AnswerId == model.AnswerId).FirstOrDefaultAsync();

            if (vote == null)
            {
                return(NotFound("Answer has not been voted on yet."));
            }

            if (vote.UserId != user.Id)
            {
                return(Unauthorized());
            }

            if (vote.Vote == model.Vote)
            {
                return(BadRequest($"The vote already is {vote.Vote}"));
            }

            var previousVote = vote.Vote;

            vote.Vote = model.Vote;

            if (previousVote == 1)
            {
                answer.Upvotes   -= 1;
                answer.Downvotes += 1;
            }
            else
            {
                answer.Upvotes   += 1;
                answer.Downvotes -= 1;
            }

            await _context.SaveChangesAsync();

            return(NoContent());
        }
示例#2
0
        public async Task <IActionResult> PostAnswerVote(AnswerVoteViewModel model)
        {
            var user = await userManager.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                return(Unauthorized());
            }

            var answer = await _context.Answers.Where(x => x.ID == model.AnswerId).FirstOrDefaultAsync();

            if (answer == null)
            {
                return(NotFound());
            }

            var vote = await _context.AnswerVotes.Where(x => x.UserId == user.Id && x.AnswerId == model.AnswerId).FirstOrDefaultAsync();

            if (vote != null)
            {
                return(BadRequest("Answer has already been voted on."));
            }

            var answerVote = new AnswerVote
            {
                AnswerId = model.AnswerId,
                UserId   = user.Id,
                Vote     = model.Vote
            };

            _context.AnswerVotes.Add(answerVote);

            if (model.Vote == 1)
            {
                answer.Upvotes += 1;
            }
            else
            {
                answer.Downvotes += 1;
            }

            await _context.SaveChangesAsync();

            return(NoContent());
        }
示例#3
0
        public async Task <ActionResult> VoteAnswer(AnswerVoteViewModel answerVoteModel)
        {
            var answerVote = JsonConvert.SerializeObject(answerVoteModel);
            HttpResponseMessage egsResponse = await client.PostAsync("api/newanswervote", new StringContent(answerVote, Encoding.UTF8, "application/json"));

            if (egsResponse.IsSuccessStatusCode)
            {
                var contents = await egsResponse.Content.ReadAsStringAsync();

                return(Json(new
                {
                    success = true,
                    message = "OK"
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(new { success = false, message = "Qualcosa non ha funzionato. Riprova più tardi" }, JsonRequestBehavior.AllowGet));
            }
        }