コード例 #1
0
        public IHttpActionResult EndExam(ViewModels.ExamDetail exam)
        {
            ViewModels.ExamDetail existingExam = examRepository.GetExamByID(exam.ID);

            if (existingExam == null)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }

            if (existingExam.EndDate < DateTime.Now || existingExam.IsEnded)
            {
                return(StatusCode(HttpStatusCode.NotAcceptable));
            }

            if (existingExam.Answers.Length != exam.Answers.Length || !existingExam.Answers.All(ea => exam.Answers.Any(a => a.QuestionID == ea.QuestionID)))
            {
                return(StatusCode(HttpStatusCode.NotAcceptable));
            }

            examService.EvaluateAnswers(existingExam, exam);
            existingExam.EndDate = DateTime.Now;
            examRepository.UpdateExam(existingExam);

            ViewModels.TestDefinitionDetail test = testRepository.GetTestByID(exam.TestID);

            return(Ok(test.ShowScoreWhenCompleted ? existingExam.TotalScore.ToString() : string.Empty));
        }