private static QuestionModel PopulateQuestionModel(DomainModels.Question question) { QuestionModel qmodel = new QuestionModel { Id = question.QuestionID, Text = question.Text, IsPredefined = question.IsSuggested, TimeCreatedUtc = question.CreateTimeUtc, }; return(qmodel); }
public void PostQuestion(AskLawQuestionModel model, string userID) { using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create()) { var user = context.Users.Where(x => x.Id == userID).FirstOrDefault(); if (user == null) { return; } var law = context.Laws.Where(x => x.LawID == model.LawID).FirstOrDefault(); if (law == null) { return; } List <int> selectedPredefinedQuestionIDs = new List <int>(); if (model.Questions != null) { selectedPredefinedQuestionIDs = model.Questions.Where(x => x.IsSelected).Select(x => x.ID).ToList(); } var predefinedQuestions = context.Questions.Where(x => selectedPredefinedQuestionIDs.Contains(x.QuestionID)); var selectedRepresentativeIDs = model.SuggestedRepresentatives.Where(x => x.IsSelected).Select(x => x.ID).ToList(); selectedRepresentativeIDs.AddRange(model.OtherRepresentatives.Where(x => x.IsSelected).Select(x => x.ID).ToList()); if (selectedRepresentativeIDs.Count == 0) { return; } // If user asked any of given questions var existingUserQuestions = context.UserRepresentativeQuestions .Where(x => x.ApplicationUserID == user.Id && selectedPredefinedQuestionIDs.Contains(x.QuestionID) && selectedRepresentativeIDs.Contains(x.RepresentativeID)) .ToList(); // should check if any of users asked same question to skip creating token var existingQuestions = context.UserRepresentativeQuestions .Where(x => selectedPredefinedQuestionIDs.Contains(x.QuestionID) && selectedRepresentativeIDs.Contains(x.RepresentativeID)) .ToList(); // Prepare custom question if needed DomainModels.Question customQuestion = null; if (!string.IsNullOrWhiteSpace(model.Text)) { customQuestion = new DomainModels.Question { LawID = law.LawID, Text = model.Text, IsSuggested = false, CreateTimeUtc = DateTime.UtcNow }; context.Questions.Add(customQuestion); } var newQuestions = new List <DomainModels.UserRepresentativeQuestion>(); AnswerToken answerToken = null; foreach (var rep in selectedRepresentativeIDs.Distinct()) { foreach (var question in selectedPredefinedQuestionIDs) { answerToken = null; if (existingUserQuestions.Where(x => x.RepresentativeID == rep && x.QuestionID == question).Any()) { continue; } if (!existingQuestions.Where(x => x.RepresentativeID == rep && x.QuestionID == question).Any()) { answerToken = new AnswerToken { RepresentativeID = rep, QuestionID = question, Token = Guid.NewGuid(), CreateTimeUtc = DateTime.UtcNow }; context.AnswerTokens.Add(answerToken); } var newPredefinedQuestion = new DomainModels.UserRepresentativeQuestion { ApplicationUserID = user.Id, QuestionID = question, RepresentativeID = rep, CreateTimeUtc = DateTime.UtcNow, AnswerToken = answerToken }; context.UserRepresentativeQuestions.Add(newPredefinedQuestion); // Notifications are being processed only for predefined questions at this point. if (answerToken != null) { newQuestions.Add(newPredefinedQuestion); } } if (!string.IsNullOrWhiteSpace(model.Text)) { answerToken = new AnswerToken { RepresentativeID = rep, Question = customQuestion, Token = Guid.NewGuid(), CreateTimeUtc = DateTime.UtcNow }; context.AnswerTokens.Add(answerToken); context.UserRepresentativeQuestions.Add( new DomainModels.UserRepresentativeQuestion { ApplicationUserID = user.Id, RepresentativeID = rep, CreateTimeUtc = DateTime.UtcNow, Question = customQuestion, AnswerToken = answerToken }); } } context.SaveChanges(); new NotificationService().ProcessNewQuestions(newQuestions); } }