/// <summary> /// Add the answer to Database /// </summary> /// <param name="data">Answer</param> public static void AddAnswer(Answer data, int questionID, int userid) { using (var db = new QaAContext()) { data.Date = DateTime.Now; data.UserId = userid; data.QuestionId = questionID; db.Answers.Add(data); db.SaveChanges(); HttpContext.Current.Cache.UpdateCache("AnsweredQuestions" + userid, data); HttpContext.Current.Cache.UpdateCache("AnswersToQuestion"+questionID, data); HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + questionID+"User"+userid, data); //Send mails to the subscripted users var question = db.Questions.Where(q => q.Id == questionID).SingleOrDefault(); IUserMailer usermailer = new UserMailer(); var tagsOfQuestion = question.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.NewAnswer(question, user, data).SendAsync(); } } }
/// <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(); } } }