public ActionResult MyAnswer(Guid Id) { Guid QId = Id; ApplicationDbContext ctx = new ApplicationDbContext(); //1.fetch the question from db or return not found Question question = ctx.Questions.Find(QId); //2. make sure the client requesting the question is the right person string ClietId = User.Identity.GetUserId(); if (question.AskedTo_Id != ClietId) { return(HttpNotFound()); } //3. construct the model and return it to the view answerViewModel model = new answerViewModel() { QuestionId = question.Id, Q = question.question, IsAnon = question.IsAnon, AnsweredBy_Id = question.AskedTo_Id }; if (!model.IsAnon) { model.AskedBy_Id = question.AskedBy_Id; } return(View(model)); }
public ActionResult MyAnswer(answerViewModel model) { HttpPostedFileBase img = model.img; ApplicationDbContext ctx = new ApplicationDbContext(); //1.validate //no validation //2.insert answer Answer a = new Answer() { Id = Guid.NewGuid(), Date = DateTime.Now, Answer_question = model.Q, Answer_answer = model.A, IsAnon = model.IsAnon, AnsweredBy_Id = User.Identity.GetUserId(), }; if (!model.IsAnon) { a.AskedBy_Id = model.AskedBy_Id; } if (img != null) { a.ContainsPhoto = true; Answer_Photo ph = new Answer_Photo(); ph.Id = a.Id; ph.Photo = new byte[img.ContentLength]; img.InputStream.Read(ph.Photo, 0, img.ContentLength); ctx.Answers.Add(a); ctx.SaveChanges(); ctx.answerPhotos.Add(ph); } else { a.ContainsPhoto = false; ctx.Answers.Add(a); } ctx.SaveChanges(); //3.delete Question ctx.Questions.Remove(ctx.Questions.Find(model.QuestionId)); ctx.SaveChanges(); //4. make notification to the asker if not anon if (!model.IsAnon) { var temp = ctx.Users.Find(a.AnsweredBy_Id); Notification notification = new Notification() { Id = Guid.NewGuid(), message = temp.FirstName + " " + temp.LastName + " answered your Question.", UrlType = NotUrlType.ToAnswer, AnswerId = a.Id, UserId = model.AskedBy_Id }; ctx.notifications.Add(notification); ctx.SaveChanges(); } return(RedirectToAction("MyQuestions")); }