public void PostTest(PostTestForCourse postTestForCourse) { var mainInfo = postTestForCourse.AdditionalInfo; if (postTestForCourse.Topic != null) { _tests.Create(new Tests { Topic = postTestForCourse.Topic, Description = postTestForCourse.Description, TheoriesId = Convert.ToInt32(mainInfo) }); var idTest = _tests.Get().Where(res => res.Topic == postTestForCourse.Topic).Select(res => res.Id).FirstOrDefault(); var questions = postTestForCourse.AnswersForTests.Split("---end---"); foreach (var question in questions) { var questionTypeAnswers = question.Split("---main---"); var typeQuestion = questionTypeAnswers[0].Split("|"); if (typeQuestion.Length == 2) { if (typeQuestion[0] != null && typeQuestion[1] != null) { var type = (QuestionType)Enum.Parse(typeof(QuestionType), typeQuestion[0]); _question.Create(new Questions { Topic = typeQuestion[1], QuestionType = type, TestId = idTest }); var questionId = _question.Get().Where(res => res.QuestionType == type && res.TestId == idTest && res.Topic == typeQuestion[1]).Select(res => res.Id).FirstOrDefault(); foreach (var answers in questionTypeAnswers[1].Split("|n|")) { if (typeQuestion[0] == "Flags" || typeQuestion[0] == "Point") { var pair = answers.Split("|"); if (pair[1] != null && pair[0] != null) { var correct = bool.Parse(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(pair[0].ToLower())); int? answerId = _answers.Get().Where(res => res.Topic == pair[1] && res.IsCorrect == correct).Select(res => res.Id).FirstOrDefault(); if (answerId == null) { _answers.Create(new Answers { Topic = pair[1], IsCorrect = correct, }); answerId = _answers.Get().Where(res => res.Topic == pair[1] && res.IsCorrect == correct).Select(res => res.Id).FirstOrDefault(); } int? qaIsCreated = _questionAnswers.Get().Where(res => res.AnswerId == answerId && res.QuestionId == questionId).Select(res => res.Id).FirstOrDefault(); if (qaIsCreated == null && answerId != null) { _questionAnswers.Create(new QuestionsAnswers { AnswerId = answerId ?? -1, QuestionId = questionId, }); } } } if (typeQuestion[0] == "Custom") { if (answers != null) { int? answerId = _answers.Get().Where(res => res.Topic == answers).Select(res => res.Id).FirstOrDefault(); if (answerId == null) { _answers.Create(new Answers { Topic = answers, IsCorrect = true, }); answerId = _answers.Get().Where(res => res.Topic == answers).Select(res => res.Id).FirstOrDefault(); } _questionAnswers.Create(new QuestionsAnswers { AnswerId = answerId ?? -1, QuestionId = questionId, }); } } } } } } } }
public IActionResult PostTestForCourse(PostTestForCourse test) { _logic.PostTest(test); return(Json(string.Empty)); }