public ActionResult Save(int id, Question[] questions, QuestionAnswer[][] answers, Tag[][] tags) { if (questions == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Can not save quiz without question.")); } using (var context = new eQuizEntities(System.Configuration.ConfigurationManager.ConnectionStrings["eQuizDB"].ConnectionString)) { var topicId = context.Topics.First().Id; var quizVariantId = 1; /*context.QuizVariants.First(x => x.QuizId == id).Id;*/ //we don't have fk on tblQuiz var blockId = context.QuizBlocks.First(x => x.QuizId == id).Id; var newQuestions = questions.Where(q => q.Id == 0).ToList(); for (int i = 0; i < questions.Length; i++) { var question = questions[i]; if (question.Id != 0) { var existedQuestion = context.Questions.FirstOrDefault(x => x.Id == question.Id); if (existedQuestion == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Question is not found")); } existedQuestion.IsActive = question.IsActive; existedQuestion.QuestionComplexity = question.QuestionComplexity; existedQuestion.QuestionText = question.QuestionText; existedQuestion.QuestionTypeId = question.QuestionTypeId; } else { question.TopicId = topicId; question.IsActive = true; context.Questions.Add(question); } } context.SaveChanges(); for (int i = 0; i < questions.Length; i++) { var question = questions[i]; if (newQuestions.Contains(question)) { var quizQuestion = new QuizQuestion { QuestionId = question.Id, QuizVariantId = quizVariantId, QuestionOrder = (short)(i + 1), QuizBlockId = blockId }; context.QuizQuestions.Add(quizQuestion); } } //for delete answer for (int i = 0; i < answers.Length; i++) { var questionId = questions[i].Id; var questionAnswer = context.Questions.Include("QuestionAnswers").FirstOrDefault(y => y.Id == questionId).QuestionAnswers.ToList(); if (answers[i][0] != null) { for (var qa = 0; qa < answers[i].Length; qa++) { var answer = answers[i][qa]; if (answer.Id != 0) { var existedAnswer = context.QuestionAnswers.FirstOrDefault(x => x.Id == answer.Id); if (existedAnswer == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Answer is not found")); } if (questionAnswer.Contains(existedAnswer)) { questionAnswer.Remove(existedAnswer); } //existedAnswer.AnswerOrder = answer.AnswerOrder; //existedAnswer.AnswerText = answer.AnswerText; //existedAnswer.IsRight = answer.IsRight; existedAnswer.QuestionId = answer.QuestionId; } else { //answer.AnswerOrder = (byte)(qa + 1); answer.QuestionId = questions[i].Id; context.QuestionAnswers.Add(answer); } } if (questionAnswer != null) { foreach (var item in questionAnswer) { context.QuestionAnswers.Remove(item);//todo } } } //todo doesn't delete tags if (tags[i][0] != null) { for (int qt = 0; qt < tags[i].Length; qt++) { var tag = tags[i][qt]; var existedTag = context.Tags.FirstOrDefault(x => x.Name == tag.Name); var question = context.Questions.FirstOrDefault(x => x.Id == questionId); if (existedTag == null) { context.Tags.Add(tag); question.QuestionTags.Add(new QuestionTag { Tag = tag }); } } } } context.SaveChanges(); } return(RedirectToAction("Get", new { id = id })); }
public ActionResult Save(int id, Question[] questions, Answer[][] answers, Tag[][] tags, int[] questionScores) { using (var context = new eQuizEntities(System.Configuration.ConfigurationManager.ConnectionStrings["eQuizDB"].ConnectionString)) { var quiz = context.Quizs.FirstOrDefault(x => x.Id == id); var quizState = context.QuizStates.FirstOrDefault(state => state.Id == quiz.QuizStateId).Name; var quizBlock = context.QuizBlocks.First(x => x.QuizId == id); if (questions == null && answers == null && tags == null && quizState == "Draft") { return(RedirectToAction("Get", new { id = id })); } IEnumerable <string> errors = QuestionsValidate(id, questions, answers, tags, questionScores); if (errors != null) { string mergedErrors = string.Join(". ", errors.ToArray()); return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, mergedErrors)); } if (quizBlock.QuestionCount != questions.Length && quizState != "Draft") { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Not enough question had created.")); } var topicId = context.Topics.First().Id; var quizVariantId = _repository.GetSingle <QuizVariant>(x => x.QuizId == id).Id; var blockId = quizBlock.Id; var newQuestions = questions.Where(q => q.Id == 0).ToList(); DeleteQuestions(id, questions); for (int i = 0; i < questions.Length; i++) { var question = questions[i]; if (question.Id != 0) { var existedQuestion = context.Questions.FirstOrDefault(x => x.Id == question.Id); if (existedQuestion == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Question is not found")); } var existedQuizQuestion = context.QuizQuestions.FirstOrDefault(x => x.QuestionId == existedQuestion.Id); existedQuizQuestion.QuestionScore = (byte)questionScores[i]; existedQuestion.IsActive = question.IsActive; existedQuestion.QuestionComplexity = question.QuestionComplexity; existedQuestion.QuestionText = question.QuestionText; existedQuestion.QuestionTypeId = question.QuestionTypeId; } else { question.TopicId = topicId; question.IsActive = true; context.Questions.Add(question); } } context.SaveChanges(); for (int i = 0; i < questions.Length; i++) { var question = questions[i]; if (newQuestions.Contains(question)) { var quizQuestion = new QuizQuestion { QuestionId = question.Id, QuizVariantId = quizVariantId, QuestionOrder = (short)(i + 1), QuizBlockId = blockId, QuestionScore = (byte)questionScores[i] }; context.QuizQuestions.Add(quizQuestion); } } //for delete answer for (int i = 0; i < answers.Length; i++) { var questionId = questions[i].Id; var questionAnswer = context.QuestionAnswers.Where(y => y.QuestionId == questionId).ToList(); // list of answers for current question if (answers[i][0] != null) { for (var qa = 0; qa < answers[i].Length; qa++) { var answer = answers[i][qa]; if (answer.Id != 0) { var existedAnswer = context.Answers.Include("QuestionAnswers").FirstOrDefault(x => x.Id == answer.Id); if (existedAnswer == null) { return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Answer is not found")); } var temp = context.QuestionAnswers.Where(x => x.AnswerId == answer.Id && x.QuestionId == questionId).FirstOrDefault(); if (temp != null) { var answerFromList = questionAnswer.Where(a => a.Id == temp.Id).FirstOrDefault(); if (answerFromList != null) { questionAnswer.Remove(answerFromList); } } existedAnswer.AnswerOrder = answer.AnswerOrder; existedAnswer.AnswerText = answer.AnswerText; existedAnswer.IsRight = answer.IsRight; } else { answer.AnswerOrder = (byte)(qa + 1); context.Answers.Add(answer); context.QuestionAnswers.Add(new QuestionAnswer { QuestionId = questionId, Answer = answer, }); } } if (questionAnswer != null) { foreach (var item in questionAnswer) { context.QuestionAnswers.Remove(item);//test it } } } if (tags[i][0] != null) { var question = context.Questions.FirstOrDefault(x => x.Id == questionId); var questionTags = context.QuestionTags.Where(x => x.QuestionId == questionId).ToList(); for (int qt = 0; qt < tags[i].Length; qt++) { var tag = tags[i][qt]; var existedTag = context.Tags.FirstOrDefault(x => x.Name == tag.Name); if (existedTag == null) { context.Tags.Add(tag); question.QuestionTags.Add(new QuestionTag { Tag = tag }); } else { var temp = context.QuestionTags.FirstOrDefault(x => x.TagId == existedTag.Id && x.QuestionId == questionId); if (temp != null) { if (questionTags.Contains(temp)) { questionTags.Remove(temp); } } else { question.QuestionTags.Add(new QuestionTag { Tag = existedTag }); } } } if (questionTags != null) { foreach (var item in questionTags) { context.QuestionTags.Remove(item);//test it } } } else { var questionTags = context.QuestionTags.Where(x => x.QuestionId == questionId).ToList(); foreach (var item in questionTags) { context.QuestionTags.Remove(item); } } context.SaveChanges(); } } return(RedirectToAction("Get", new { id = id })); }