public ActionResult Create(List <InsertAnswer_AnswerCommentViewModel> insertViewModel) { if (ModelState.IsValid) { for (int i = 0; i < insertViewModel.Count(); i++) { var answerComment = new AnswerComment { QCommentID = insertViewModel[i].QCommentID, Content = insertViewModel[i].QContent }; db.QComments.Add(answerComment); db.SaveChanges(); var answer = new Answer { AnswerID = insertViewModel[i].AnswerID, Content = insertViewModel[i].Content, IsCorrect = insertViewModel[i].IsCorrect, LinkedQuestion = db.Questions.Find(insertViewModel[i].QuestionID), CommentID = answerComment.QCommentID }; db.Answers.Add(answer); db.SaveChanges(); } return(RedirectToAction("Index", "Questions")); } //ViewBag.CommentID = new SelectList(db.QComments, "QCommentID", "Content", answer.CommentID); //ViewBag.QuestionID = new SelectList(db.Questions, "QuestionID", "Wording", answer.QuestionID); return(View(insertViewModel)); }
public async Task <ActionResult> CreateAnswerComment(AnswerComment answercom) { var getQuestionIdByAnswerToQuestionId = _applicationDbContext.AnswerToQuestions.SingleOrDefault(i => i.AnswerToQuestionId == answercom.AnswerToQuestionId); if (ModelState.IsValid) { answercom.UserId = User.Identity.GetUserId(); answercom.DateCommented = DateTime.UtcNow; _applicationDbContext.AnswerComments.Add(answercom); await _applicationDbContext.SaveChangesAsync(); var question = _applicationDbContext.Questions.SingleOrDefault(i => i.QuestionId == getQuestionIdByAnswerToQuestionId.QuestionId); string title = question.Title; string titleC = Regex.Replace(title, "[^A-Za-z0-9]", "-"); string cat = question.DiscussionCategory.Name; string catC = Regex.Replace(cat, "[^A-Za-z0-9]", "-"); string titleCat = titleC + "&" + catC; string id = Guid.NewGuid().ToString(); return(RedirectToAction("Thread", new { Controller = "Question", action = "Thread", page = question.QuestionId, title = titleCat, iD = id })); //return Json(new { success = true }); } else { string iD = Guid.NewGuid().ToString(); return(RedirectToAction("CreateAnswerComment", new { Controller = "Question", action = "CreateAnswerComment", page = answercom.AnswerToQuestionId, id = iD })); } }
// GET: Questions/Details/5 /// <summary> /// Single reusable action for displaying question /// </summary> /// <param name="model"> The question answer to display (with answer) </param> /// <param name="_View"> Eg: Details for display, Edit for editing, Delete for deleting </param> /// <param name="question"> The question to display </param> /// <returns> The view specified by _View </returns> //[ImportModelStateFromTempData] //[PageView] public ActionResult Details(int QuestionId, string _View) { QAModel model = new QAModel(); QuestionComment questionComment = new QuestionComment(); AnswerComment answerComment = new AnswerComment(); dbUtil = new DBUtil(3); if (_View == "Edit") { if (!Request.IsAuthenticated) { TempData["RedirectMsg"] = "You must be logged in first"; return(RedirectToActionPermanent("Login", "Account")); } } if (QuestionId > 0) { model.question = new Question(); QuestionWS Qws = new QuestionWS(); model.question = Qws.GetQuestionById(QuestionId, this.GetVisitorIP(Session["VisitorIP"])); if (model.question.AnswerCount > 0) { AnswerWS Aws = new AnswerWS(); model.answer = Aws.GenModel4mDS(Answers_ds: Aws.GetAnswersForQuestion(null, QuestionId)); } else { model.answer = Answer.InitializeIfNone(model.answer); } } if (TempData["StatusMsg"] == null) { TempData["Title"] = model.question.Q_Title; } else { TempData["Title"] = TempData["StatusMsg"]; } if (Request.IsAuthenticated) { TempData["LoggedInUser"] = User.Identity.GetCurUserNumber(); } else { TempData["LoggedInUser"] = 0; } return(View(_View, model)); //TempData["ErrorPrevention"] = "Maybe the data required to display the question was incomplete. Do you want to <a href='/Questions/Create'> ask a question </a>?"; //ModelState.AddModelError("", "An error occured while displaying question"); //return View("Error"); }
public async Task <ActionResult> DeleteConfirmed(int id) { AnswerComment answerComment = (AnswerComment)await db.Comments.FindAsync(id); db.Comments.Remove(answerComment); await db.SaveChangesAsync(); return(RedirectToAction("Index")); }
public void PopulateFromAnswer(Answer answer, AnswerComment answerComment) { AnswerID = answer.AnswerID; Content = answer.Content; IsCorrect = answer.IsCorrect; CommentID = answer.CommentID; QCommentID = answerComment.QCommentID; QContent = answerComment.Content; }
public async Task <ActionResult> Edit([Bind(Include = "CommentId,CommentTxt,CommentBy,CommentDt,CommentStatus,CommentStatusBy,CommentMarksEarned,CommentEdits,CommentType,postType,AnswerId")] AnswerComment answerComment) { if (ModelState.IsValid) { db.Entry(answerComment).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(answerComment)); }
// GET: AnswerComments/Edit/5 public async Task <ActionResult> Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AnswerComment answerComment = (AnswerComment)await db.Comments.FindAsync(id); if (answerComment == null) { return(HttpNotFound()); } return(View(answerComment)); }
protected void btnWriteComment_Click(object sender, EventArgs e) { var comment = new AnswerComment(); comment.ArticleId = Convert.ToInt32(Request["Id"]); // 부모글(AnswerDetails.aspx?Id=<부모글>) comment.Name = txtName.Text; // 이름 comment.Password = txtPassword.Text; // 암호 comment.Opinion = txtOpinion.Text; // 댓글 // 데이터 입력 repository.AddComment(comment); Response.Redirect($"{Request.ServerVariables["SCRIPT_NAME"]}?Id={Request["Id"]}"); }
public ActionResult AddAnswerComment(int QuestionId, int AnswerId, string Comment) { AnswerComment answerComment = new AnswerComment(); answerComment.AnswerId = AnswerId; answerComment.Comment = Comment; answerComment.UserId = User.Identity.GetUserId(); if (ModelState.IsValid) { db.AnswerComments.Add(answerComment); db.SaveChanges(); return(RedirectToAction("Details", new { id = QuestionId })); } return(View(answerComment)); }
public List <AnswerComment> GetCommentsOfanswer(int answerId) { var ans = new List <AnswerComment>(); var cmd = new MySqlCommand("select * from main_answercomment where answer_id=" + answerId + " order by id desc", DBConnection.Instance().MySqlConnection); var rd = cmd.ExecuteReader(); while (rd.Read()) { var id = (int)rd["id"]; var content = (string)rd["content"]; var day = (string)rd["day"]; var hour = (string)rd["hour"]; var userId = (int)rd["user_id"]; var answerComment = new AnswerComment(id, content, day, hour, answerId, userId); ans.Add(answerComment); } rd.Close(); return(ans); }
public IHttpActionResult NewAnswerComment([FromBody] AnswerComment ac) { ac.UpdateDate = DateTime.Now; Repository.Entities.User user = _repository.GetUserByUserName(User.Identity.Name); if (user != null) { ac.UserId = user.UserId; ac.NickName = user.NickName; } else { ac.UserId = new Guid("0e5f76d4-7180-44e9-bc34-ced22a6d2bed"); } Answer answerSource = _repository.GetAnswer(ac.AnswerSource.Id); ac.AnswerSource = answerSource; RepositoryActionResult <AnswerComment> result = _repository.AddAnswerComment(ac); switch (result.Status) { case RepositoryActionStatus.Created: { int newID = result.Entity.Id; String UserName = result.Entity.AnswerSource.UserName; String UserId = result.Entity.UserId.ToString(); String Description = result.Entity.Description; String UpdateDate = result.Entity.UpdateDate.HasValue ? result.Entity.UpdateDate.Value.ToShortDateString() : ""; return(Content(System.Net.HttpStatusCode.OK, newID + "*" + UserName + "*" + UserId + "*" + Description + "*" + UpdateDate)); } case RepositoryActionStatus.NothingModified: return(Content(System.Net.HttpStatusCode.OK, "Risultato Not Modified?")); //TODO have I to return OK? case RepositoryActionStatus.Error: return(Content(System.Net.HttpStatusCode.InternalServerError, result.Exception.InnerException.Message)); default: break; } return(Content(System.Net.HttpStatusCode.Ambiguous, result.Status.ToString() + " " + result.Entity.Description)); }
public async Task <BaseResponseViewModel> AddCommentAsync(int answerId, string comment) { var answerComment = new AnswerComment(); answerComment.Comment = comment; answerComment.AnswerId = answerId; answerComment.CreatedAt = DateTime.Now; answerComment.UpdatedAt = DateTime.Now; answerComment.Author = this.currentUser; await this.answerCommentRepository.AddAsync(answerComment); await this.uow.SaveChangesAsync(); var response = new BaseResponseViewModel() { Id = answerComment.Id }; return(response); }
public async Task <AnswerComment> AddAnswerCommentAsync(AnswerComment answerComment) { await using var transaction = await _dbContext.Database.BeginTransactionAsync(); try { answerComment.DateAdded = DateTime.Now; await _dbContext.AnswerComments.AddAsync(answerComment); await _dbContext.SaveChangesAsync(); // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails await transaction.CommitAsync(); } catch (Exception) { // TODO: Handle failure - UX message transaction.Rollback(); } return(answerComment); }
public RepositoryActionResult <AnswerComment> AddAnswerComment(AnswerComment ac) { #region try try { _ctx.AnswerComments.Add(ac); var result = _ctx.SaveChanges(); if (result > 0) { return(new RepositoryActionResult <AnswerComment>(ac, RepositoryActionStatus.Created)); } else { return(new RepositoryActionResult <AnswerComment>(ac, RepositoryActionStatus.NothingModified, null)); } } catch (Exception ex) { return(new RepositoryActionResult <AnswerComment>(null, RepositoryActionStatus.Error, ex)); } #endregion }
public async Task EditAnswerCommentAsync(AnswerComment answerComment) { await using var transaction = await _dbContext.Database.BeginTransactionAsync(); try { var answerCommentFromDb = await _dbContext.AnswerComments.Where(c => c.Id == answerComment.Id && c.IsRemoved == false).FirstOrDefaultAsync(); answerCommentFromDb.Body = answerComment.Body; answerCommentFromDb.IsEdited = answerComment.IsEdited; answerCommentFromDb.DateAdded = answerComment.DateAdded; _dbContext.AnswerComments.Attach(answerCommentFromDb); _dbContext.Entry(answerCommentFromDb).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); await transaction.CommitAsync(); } catch (Exception) { // TODO: Handle failure - UX message transaction.Rollback(); } }
public ActionResult Edit(List <InsertAnswer_AnswerCommentViewModel> answer) { if (ModelState.IsValid) { foreach (var answ in answer) { Answer t = new Answer { AnswerID = answ.AnswerID, Content = answ.Content, IsCorrect = answ.IsCorrect, QuestionID = answ.QuestionID, CommentID = answ.CommentID }; db.Entry(t).State = EntityState.Modified; db.SaveChanges(); var answerComment = new AnswerComment { QCommentID = answ.QCommentID, Content = answ.QContent }; db.Entry(answerComment).State = EntityState.Modified; db.SaveChanges(); } //foreach (var answ in answer) //{ // db.Entry(answ).State = EntityState.Modified; // db.SaveChanges(); // //} return(RedirectToAction("Index", "Questions")); } //ViewBag.CommentID = new SelectList(db.QComments, "QCommentID", "Content", answ.CommentID); //ViewBag.QuestionID = new SelectList(db.Questions, "QuestionID", "Wording", answ.QuestionID); return(View(answer)); }