public virtual MvcMailMessage NewQuestion(Question question, UserProfile to) { string subject; if (EmailConfig.IsReceivingEmailSetted) { var hash = UserManager.SentEmailHash(to.UserId, question.Id); subject = Resources.Global.NewQuestionSubject + " - Id:" + hash; } else { subject = Resources.Global.NewQuestionSubject; } ViewBag.UserName = to.UserName; ViewBag.Question = question; return Populate(x => { x.Subject = subject; x.ViewName = "NewQuestion"; x.To.Add(to.Email); }); }
public ActionResult EditQuestion(Question data, string QuesID) { data.Id = Int32.Parse(QuesID); data.UserId = WebSecurity.CurrentUserId; if (ModelState.IsValid) { QuestionManager.EditQuestion(data); } return RedirectToAction("Index"); }
public ActionResult CreateQuestion(Question data, string tags) { if (Request.IsAuthenticated && (!tags.Equals(""))) { //Create a list for the tags var tagList = new List<String>(); //Split the given string by commas var boxtags = tags.Split(','); //If the last character is a comma, create a list without that if (boxtags[boxtags.Length - 1].Equals(" ")) tagList = boxtags.Take(boxtags.Length - 1).ToList(); else tagList = boxtags.ToList(); //Remove all leading and trailing whitespaces from the tage var tagsWithoutWhitespaces = new List<String>(); foreach (var item in tagList) { tagsWithoutWhitespaces.Add(item.Trim()); } //If the created question is valid, add a new question to the database if (ModelState.IsValid) { QuestionManager.AddQuestion(data, WebSecurity.GetUserId(User.Identity.Name), tagsWithoutWhitespaces); } } return RedirectToAction("Index"); }
/// <summary> /// Question edit /// </summary> /// <param name="data">Question's data</param> public static void EditQuestion(Question data) { using (var db = new QaAContext()) { var q = from question in db.Questions where (question.Id == data.Id) select question; var editableData = q.SingleOrDefault(); editableData.Title = data.Title; editableData.Content = data.Content; db.SaveChanges(); HttpContext.Current.Cache.UpdateCache("GetQuestion" + data.Id, data); } }
/// <summary> /// Add Question to the Database /// </summary> /// <param name="data">Question's data</param> public static void AddQuestion(Question data, int userID, List<string> tagList) { using (var db = new QaAContext()) { //Set the actual datetime data.Date = DateTime.Now; data.UserId = userID; db.Questions.Add(data); db.SaveChanges(); //Tag handling //Check the tag's existence, if tag does not exist, then we insert the tag Tag tag = null; foreach (var item in tagList) { var q = (from t in db.Tags where item.Equals(t.Name) select t).ToArray(); //Insert if (q.Length == 0) { tag = new Tag(); tag.Name = item; db.Tags.Add(tag); db.SaveChanges(); //Add tag to the question var qt = new QuestionHasTag(); qt.QuestionId = data.Id; qt.TagId = tag.Id; db.QuestionHasTags.Add(qt); db.SaveChanges(); } //In the case, the tag exist else { var query = from t in db.Tags where item.Equals(t.Name) select t; tag = query.Single(); var qt = new QuestionHasTag(); qt.QuestionId = data.Id; qt.TagId = tag.Id; db.QuestionHasTags.Add(qt); db.SaveChanges(); } HttpContext.Current.Cache.UpdateCache("Tags", item); } db.SaveChanges(); int pageCache = 5; HttpContext.Current.Cache.UpdateCache("AllQuestions", data); HttpContext.Current.Cache.UpdateCache("LatestQuestions", data); HttpContext.Current.Cache.UpdateCache("QuestionsByUser" + data.UserId, data); for (int i = 1; i < pageCache; i++) { HttpContext.Current.Cache.UpdateCache("PageQuestions" + i, data); } foreach (var qht in data.QuestionHasTags) { for (int i = 1; i < pageCache; i++) { HttpContext.Current.Cache.UpdateCache("QuestionsByTag" + qht.TagId + "Page" + i, data); } } HttpContext.Current.Cache.UpdateCache("QuestionsByUser" + data.UserId, data); //Send mails to the subscripted users IUserMailer usermailer = new UserMailer(); var tagsOfQuestion = data.QuestionHasTags.Select(s=>s.TagId).ToList(); var tos = db.UserProfiles.Where(up => (up.Subscriptions.Any(u => tagsOfQuestion.Any(x => x == u.TagId)) && up.Email!=null && up.IsVerified==true)).ToList(); foreach (var user in tos) { usermailer.NewQuestion(data, user).SendAsync(); } } }