public static PersonTestResult ParseQuizResultApiIn(QuizResultApiIn quizResultApiIn) { Person person = new Person { Email = quizResultApiIn.Email, Name = quizResultApiIn.Name }; List <Answer> answers = new List <Answer>(); foreach (var questionResult in quizResultApiIn.QuestionResults) { List <string> list = new List <string>(); uint? correctAnswerIndex = null; uint? userAnswerIndex = null; for (uint i = 0; i < questionResult.AnswerResults.Count; i++) { var answerResult = questionResult.AnswerResults[(int)i]; var answer = answerResult.Substring(answerResult.LastIndexOf(')') + 1); list.Add(answer); if (answerResult.Contains("(+)")) { correctAnswerIndex = i; } if (answerResult.Contains("(v)")) { userAnswerIndex = i; } } if (!correctAnswerIndex.HasValue) { throw new Exception("Question with no correct answer"); } if (!userAnswerIndex.HasValue) { throw new Exception("Question with no user answer"); } var question = new Question { QuestionText = questionResult.QuestionText, AnswersList = list, CorrectAnswerIndex = correctAnswerIndex.Value }; answers.Add(new Answer { Question = question, AnswerIndex = userAnswerIndex.Value }); } return(new PersonTestResult { Person = person, Notes = quizResultApiIn.Comment, Result = quizResultApiIn.PercentUserAnswersCorrect, Answers = answers }); }
public object Post([FromBody] QuizResultApiIn quizResultApiIn) { try { List <string> errors = new List <string>(); if (quizResultApiIn.Name == null) { errors.Add("Parameter \"name\" is empty"); } if (quizResultApiIn.Email == null) { errors.Add("Parameter \"email\" is empty"); } if (quizResultApiIn.QuestionResults == null) { errors.Add("Parameter \"quizresults\" is empty"); } if (errors.Count != 0) { return(new Status { Result = "Fail", Errors = errors }); } dataStorage.SavePersonTestResult(Parser.ParseQuizResultApiIn(quizResultApiIn)); return(new Status { Result = "Success" }); } catch (Exception e) { return(new Status { Result = "Error", Errors = new List <string> { e.Message } }); } }