public async Task SaveSession(Session quizSession)
        {
            var creationDate = DateTime.Now;

            string codedToken = _httpContext.HttpContext
                                .Request
                                .Headers["Authorization"];

            codedToken = codedToken.Replace("Bearer ", "");

            quizSession.User = await _accountService.GetUserFromJwtToken(codedToken);

            quizSession.CreationDate = creationDate;

            quizSession.ChallengeId = Guid.Empty;
            quizSession.Challenge   = await _challengeService.GetById(quizSession.Challenge.Id);

            foreach (ClientQuiz quiz in quizSession.ClientQuiz)
            {
                quiz.Id           = Guid.Empty;
                quiz.CreationDate = creationDate;
                quiz.QuestionId   = Guid.Empty;
                quiz.Question     = await _questionService.GetById(quiz.Question.Id);

                quiz.SelectedAnswerId = null;
                if (quiz.SelectedAnswer != null)
                {
                    quiz.SelectedAnswer = (await _answerService.GetList()).Where(a => a.Id == quiz.SelectedAnswer.Id).FirstOrDefault();
                }
            }

            _context.Sessions.Add(quizSession);

            await _context.SaveChangesAsync();
        }
        public async Task Update(Question newQuestion)
        {
            var      date     = DateTime.Now;
            Question question = await GetById(newQuestion.Id);

            question.CreationDate = date;
            question.Title        = newQuestion.Title;

            Guid correctAnswerId = newQuestion.Answers
                                   .Where(a => a.IsCorrect)
                                   .Select(a => a.Id)
                                   .SingleOrDefault();

            foreach (Answer answer in question.Answers)
            {
                if (answer.Id == correctAnswerId)
                {
                    answer.IsCorrect = true;
                }
                else
                {
                    answer.IsCorrect = false;
                }
            }

            List <CategoryQuestion> newCategories = newQuestion.CategoryList;
            List <CategoryQuestion> oldCategories = question.CategoryList;

            question.CategoryList = null;

            _context.Questions.Update(question);
            await _context.SaveChangesAsync();

            foreach (CategoryQuestion cat in newCategories)
            {
                _context.CategoryQuestions.Add(new CategoryQuestion
                {
                    CreationDate = date,
                    Category     = await _categoryService.GetById(cat.Category.Id),
                    Question     = question
                });
                await _context.SaveChangesAsync();
            }
        }
        public async Task Add(Challenge challenge)
        {
            var date = DateTime.Now;

            challenge.QuizType = await _context.QuizTypes
                                 .Where(qt => qt.Id == challenge.QuizType.Id)
                                 .FirstOrDefaultAsync();

            challenge.Color = await _context.Colors
                              .Where(c => c.Id == challenge.Color.Id)
                              .FirstOrDefaultAsync();

            challenge.CreationDate = date;

            foreach (ChallengeCategory cat in challenge.CategoryList)
            {
                cat.CategoryId  = Guid.Empty;
                cat.ChallengeId = Guid.Empty;
                cat.Category    = await _categoryService.GetById(cat.Category.Id);
            }

            _context.Challenges.Add(challenge);
            await _context.SaveChangesAsync();
        }
Esempio n. 4
0
        public async Task Add(Category category)
        {
            var date = DateTime.Now;

            category.CreationDate = date;

            _context.Categories.Add(category);

            await _context.SaveChangesAsync();
        }
        public async Task Add(QuizType quizType)
        {
            _context.QuizTypes.Add(quizType);

            await _context.SaveChangesAsync();
        }
Esempio n. 6
0
        public async Task Add(Color color)
        {
            _context.Colors.Add(color);

            await _context.SaveChangesAsync();
        }