public async Task <IHttpActionResult> PostAnswerReply(AnswerReply question)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model state"));
                }
                question.time     = DateTime.UtcNow;
                question.postedBy = User.Identity.GetUserId();
                db.AnswerReplies.Add(question);
                await db.SaveChangesAsync();

                var ret = await db.AnswerReplies.Where(x => x.Id == question.Id).Select(x => new
                {
                    id           = x.Id,
                    description  = x.description,
                    postedById   = x.AspNetUser.Id,
                    postedByName = x.AspNetUser.Email,
                    time         = x.time
                }).FirstOrDefaultAsync();

                return(Ok(ret));
            }
            return(BadRequest("Not login"));
        }
Exemple #2
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                var answer = await _context.Answers.FindAsync(request.AnswerId);

                var answerReply = new AnswerReply
                {
                    Id        = request.Id,
                    Message   = request.Message,
                    Author    = user,
                    Answer    = answer,
                    CreatedAt = request.CreatedAt,
                };

                user.AnswersReplies.Add(answerReply);
                answer.Replies.Add(answerReply);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Error in saving changes");
            }
Exemple #3
0
        public ActionResult DeleteConfirmed(int id)
        {
            AnswerReply answerReply = db.AnswerReplies.Find(id);

            db.AnswerReplies.Remove(answerReply);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public async Task <IHttpActionResult> VoteAnswerReply(int id, bool isup)
        {
            var userId = User.Identity.GetUserId();

            if (userId != null)
            {
                AnswerReply comment = await db.AnswerReplies.FindAsync(id);

                var commentVoteByUser = await db.AnswerReplyVotes.FirstOrDefaultAsync(x => x.answerReplyId == id && x.votedBy == userId);

                if (comment == null)
                {
                    return(NotFound());
                }
                var vote = commentVoteByUser;
                if (vote != null)
                {
                    if (vote.isUp && isup)
                    {
                        return(BadRequest("You have already voteup"));
                    }
                    else if (vote.isUp && !isup)
                    {
                        vote.isUp = false;
                    }
                    else if (!vote.isUp && !isup)
                    {
                        return(BadRequest("You have already votedown"));
                    }
                    else if (!vote.isUp && isup)
                    {
                        vote.isUp = true;
                    }
                }
                else
                {
                    AnswerReplyVote repvote = new  AnswerReplyVote();
                    repvote.isUp          = isup;
                    repvote.votedBy       = userId;
                    repvote.answerReplyId = id;
                    db.AnswerReplyVotes.Add(repvote);
                }
                await db.SaveChangesAsync();

                var q = (from x in comment.AnswerReplyVotes.Where(x => x.answerReplyId == comment.Id)
                         let voteUp = comment.AnswerReplyVotes.Count(m => m.isUp)
                                      let voteDown = comment.AnswerReplyVotes.Count(m => m.isUp == false)
                                                     select new { voteUpCount = voteUp, voteDownCount = voteDown }).FirstOrDefault();

                return(Ok(q));
            }
            else
            {
                return(BadRequest("You are not login"));
            }
        }
Exemple #5
0
 public ActionResult Edit([Bind(Include = "Id,content,userId,answerId,createTime")] AnswerReply answerReply)
 {
     if (ModelState.IsValid)
     {
         db.Entry(answerReply).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.answerId = new SelectList(db.Answers, "Id", "content", answerReply.answerId);
     ViewBag.userId   = new SelectList(db.Users, "Id", "Name", answerReply.userId);
     return(View(answerReply));
 }
Exemple #6
0
        // GET: AnswerReplies/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AnswerReply answerReply = db.AnswerReplies.Find(id);

            if (answerReply == null)
            {
                return(HttpNotFound());
            }
            return(View(answerReply));
        }
        public async Task <IHttpActionResult> DeleteAnswerReply(int id)
        {
            AnswerReply comment = await db.AnswerReplies.FindAsync(id);

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

            db.AnswerReplies.Remove(comment);
            await db.SaveChangesAsync();

            return(Ok(comment));
        }
Exemple #8
0
        // GET: AnswerReplies/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AnswerReply answerReply = db.AnswerReplies.Find(id);

            if (answerReply == null)
            {
                return(HttpNotFound());
            }
            ViewBag.answerId = new SelectList(db.Answers, "Id", "content", answerReply.answerId);
            ViewBag.userId   = new SelectList(db.Users, "Id", "Name", answerReply.userId);
            return(View(answerReply));
        }
Exemple #9
0
        public ActionResult AddReply(int askId, int userId, int answerId, string c)
        {
            if (userId == 2)
            {
                return(Redirect("/Home/longin"));
            }
            var x = new AnswerReply();

            x.content    = c;
            x.userId     = userId;
            x.answerId   = answerId;
            x.createTime = DateTime.Now;
            db.AnswerReplies.Add(x);
            db.SaveChanges();
            return(RedirectToAction("DisplayQuestion", new { id = askId, userId = userId }));
        }
        public async Task <IHttpActionResult> updateAnswerReply(AnswerReply comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            db.Entry(comment).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }