public void SubmitAnswer(Models.QuestionInfo questioninfo) { Answer a = new Answer(); a.Text = questioninfo.NewAnswer; a.AnswerByUser = questioninfo.UserId; a.AnsweredOn = DateTime.Now; a.QuestionId = questioninfo.QuestionId; a.UpDownVote = 0; base.context.Answers.Add(a); base.context.SaveChanges(); var getQuestion = base.context.Questions.SingleOrDefault(y => y.QuestionId == a.QuestionId); var UserAskedQuestion = getQuestion.UserId; if (UserAskedQuestion != questioninfo.UserId) { var getUser = base.context.Users.SingleOrDefault(y => y.UserId == questioninfo.UserId); getUser.ReputaionPoints = getUser.ReputaionPoints + 1; context.Entry(getUser).State = EntityState.Modified; context.SaveChanges(); } }
public void Update(Models.QuestionInfo questioninfo) { try { //var uid = se.Users.FirstOrDefault(x => x.Name.ToLower() == User.Identity.Name.ToLower()); Question question = questioninfo.question; question.UpdatedOn = DateTime.Now; this.Update(question); string[] UITags = questioninfo.CommaSeperatedTags.Split(','); List <TagMaster> tagsMasters = context.TagMasters.ToList(); List <TagMaster> newItems = new List <TagMaster>(); List <TagMaster> existingTags = new List <TagMaster>(); List <TagQuestion> qTags = new List <TagQuestion>(); foreach (var item in UITags) { if (!string.IsNullOrWhiteSpace(item)) { if (tagsMasters.Any(x => string.Equals(x.TagDescription, item.Trim(), StringComparison.OrdinalIgnoreCase))) { TagMaster T = tagsMasters.FirstOrDefault(x => string.Equals(x.TagDescription, item.Trim(), StringComparison.OrdinalIgnoreCase)); existingTags.Add(T); //to acoid repeatation if (!qTags.Any(x => x.TagId == T.TagId)) { qTags.Add(new TagQuestion { TagId = T.TagId, QuestionId = question.QuestionId }); } } else { TagMaster newTag = new TagMaster { TagDescription = item.Trim() }; newItems.Add(newTag); } } } if (newItems != null && newItems.Any()) { context.TagMasters.AddRange(newItems); context.SaveChanges(); } foreach (var item in newItems) { qTags.Add(new TagQuestion { TagId = item.TagId, QuestionId = question.QuestionId }); } var removetags = context.TagQuestions.Where(x => x.QuestionId == question.QuestionId).ToList(); if (removetags != null && removetags.Any()) { context.TagQuestions.RemoveRange(removetags); context.SaveChanges(); } if (qTags != null && qTags.Any()) { context.TagQuestions.AddRange(qTags); context.SaveChanges(); } } catch (Exception ex) { ex.ToString(); } }
public void Create(Models.QuestionInfo questioninfo) { try { //var uid = se.Users.FirstOrDefault(x => x.Name.ToLower() == User.Identity.Name.ToLower()); Question question = new Question(); question.UserId = questioninfo.CurrentUser.UserId; question.Title = questioninfo.Title; question.Description = questioninfo.Description; question.CreatedOn = DateTime.Now; question.UpdatedOn = DateTime.Now; question.ViewCount = 0; question.IsOpen = true; string[] UITags = questioninfo.CommaSeperatedTags.Split(','); List <TagMaster> tagsMasters = context.TagMasters.ToList(); List <TagMaster> newItems = new List <TagMaster>(); List <TagMaster> existingTags = new List <TagMaster>(); List <TagQuestion> qTags = new List <TagQuestion>(); foreach (var item in UITags) { if (tagsMasters.Any(x => x.TagDescription == item.Trim())) { TagMaster T = tagsMasters.FirstOrDefault(x => x.TagDescription == item.Trim()); existingTags.Add(T); qTags.Add(new TagQuestion { TagId = T.TagId }); } else { TagMaster newTag = new TagMaster { TagDescription = item.Trim() }; newItems.Add(newTag); } } context.TagMasters.AddRange(newItems); context.SaveChanges(); foreach (var item in newItems) { qTags.Add(new TagQuestion { TagId = item.TagId }); } question.TagQuestions = qTags; context.Questions.Add(question); // context.TagQuestions.AddRange(qTags); context.SaveChanges(); // return RedirectToAction("List"); } catch (Exception ex) { ex.ToString(); } }