Exemplo n.º 1
0
        public Result AddResult(QuizAnswerDTO quizAnswer)
        {
            float point = CalculatePoint(quizAnswer.QuizId, quizAnswer.AnswerSelect);

            var result = new Result
            {
                QuizId          = quizAnswer.QuizId,
                AccountUsername = quizAnswer.Username,
                Point           = point,
                StartTime       = DateTime.Parse(quizAnswer.StartTime),
                FinishTime      = DateTime.Parse(quizAnswer.FinishTime),
            };

            _letQuizContext.Results.Add(result);
            if (_letQuizContext.SaveChanges() > 0)
            {
                foreach (var answerSelect in quizAnswer.AnswerSelect)
                {
                    if (answerSelect.AnswerId != 0)
                    {
                        _letQuizContext.ResultDetail.Add(new ResultDetail
                        {
                            ResultId = result.ResultId,
                            AnswerId = answerSelect.AnswerId
                        });
                    }
                }

                _letQuizContext.SaveChanges();

                return(result);
            }

            return(null);
        }
Exemplo n.º 2
0
        public ActionResult AddResultQuiz([FromBody] QuizAnswerDTO quizAnswer)
        {
            var result = _resultsRepository.AddResult(quizAnswer);

            if (result != null)
            {
                return(Created("https://localhost:44300/api/results", result));
            }

            return(BadRequest());
        }