Ejemplo n.º 1
0
        public async Task <SaveQuizResponseDTO> SaveQuizAsync(SaveQuiz_QuizDTO quizReceived)
        {
            var result = new SaveQuizResponseDTO();

            var quiz = _mapper.Map <Quiz>(quizReceived);

            quiz.Owner = await _characterRepo.GetCharacterAllAsync(quizReceived.OwnerId);

            if (quiz.Owner == null)
            {
                result._result = SaveQuizResponseDTO.RequestResult.OwnerNotFound;
                return(result);
            }

            if (quiz.Questions.Count == 0)
            {
                result._result = SaveQuizResponseDTO.RequestResult.EmptyAtribute;
                return(result);
            }

            foreach (var question in quiz.Questions)
            {
                if (question.Answers.Count == 0)
                {
                    result._result = SaveQuizResponseDTO.RequestResult.EmptyAtribute;
                    return(result);
                }

                if (!ValidateQuestion(question))
                {
                    result._result = SaveQuizResponseDTO.RequestResult.QuestionWithoutCorrectAnswer;
                    return(result);
                }
            }

            _quizRepo.AddQuiz(quiz);

            try
            {
                await _quizRepo.SaveChangesAsync();

                result._result = SaveQuizResponseDTO.RequestResult.Success;
                result.Quiz    = _mapper.Map <QuizReadDTO>(quiz);
            }
            catch (Exception)
            {
                result._result = SaveQuizResponseDTO.RequestResult.BadRequest;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <SaveQuizFeedbackResponseDTO.RequestResult> SaveQuizfeedback(QuizFeedback quizFeedback)
        {
            var character = await _characterRepo.GetCharacterAllAsync(quizFeedback.Character.Id);

            if (character == null)
            {
                return(SaveQuizFeedbackResponseDTO.RequestResult.CharacterNotFound);
            }

            quizFeedback.Character = character;
            character.QuizFeedbacks.Add(quizFeedback);

            if (character.QuizFeedbacks.Where(c => c.Quiz.Id == quizFeedback.Quiz.Id).Count() == 1)
            {
                quizFeedback.ExperienceGained = (int)quizFeedback.PercentageOfCorrectChosenAnswers;
                quizFeedback.GoldGained       = (int)quizFeedback.PercentageOfCorrectChosenAnswers;

                var prevLevel = character.Level;

                character.TotalXP += quizFeedback.ExperienceGained;
                character.Gold    += quizFeedback.GoldGained;

                quizFeedback.LevelGained = character.Level - prevLevel;
            }

            try
            {
                await _characterRepo.SaveChangesAsync();

                return(SaveQuizFeedbackResponseDTO.RequestResult.Success);
            }
            catch (Exception)
            {
                return(SaveQuizFeedbackResponseDTO.RequestResult.BadRequest);
            }
        }