Ejemplo n.º 1
0
        public async Task <bool> UpdateRoundAsync(long categoryId, long typeId, long roundId)
        {
            var round = await _context.QuizRound.Where(qr => qr.Id == roundId).FirstOrDefaultAsync();

            var roundQuestions = await _context.RoundQuestion.Where(rq => rq.RoundId == roundId).ToListAsync();

            _context.QuizRound.Attach(round);
            round.CategoryId            = categoryId;
            round.TypeId                = typeId;
            _context.Entry(round).State = EntityState.Modified;

            _context.RoundQuestion.RemoveRange(roundQuestions);

            var questions = await _context.Question.Where(q => q.CategoryId == categoryId && q.TypeId == typeId).OrderBy(x => Guid.NewGuid()).Take(round.NumberOfQuestions == 0 ? 5 : round.NumberOfQuestions).ToListAsync();

            foreach (var question in questions)
            {
                var roundQuestion = new DAL.Models.RoundQuestion
                {
                    QuestionId = question.Id,
                    RoundId    = roundId
                };
                await _context.RoundQuestion.AddAsync(roundQuestion);
            }

            var saveResult = await _context.SaveChangesAsync();

            if (saveResult > 0)
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        public async Task <bool> AddNewRoundAsync(long quizId, int numberOfQuestions, long categoryId, long typeId)
        {
            var newRound = new DAL.Models.QuizRound
            {
                QuizId            = quizId,
                NumberOfQuestions = numberOfQuestions,
                CategoryId        = categoryId,
                TypeId            = typeId
            };

            await _context.QuizRound.AddAsync(newRound);

            var saveResult = await _context.SaveChangesAsync();

            if (saveResult > 0)
            {
                var questions = await _context.Question.Where(q => q.CategoryId == categoryId && q.TypeId == typeId).OrderBy(x => Guid.NewGuid()).Take(numberOfQuestions).ToListAsync();

                foreach (var question in questions)
                {
                    var roundQuestion = new DAL.Models.RoundQuestion
                    {
                        QuestionId = question.Id,
                        RoundId    = newRound.Id
                    };
                    await _context.RoundQuestion.AddAsync(roundQuestion);
                }
                await _context.SaveChangesAsync();

                return(true);
            }
            return(false);
        }