Example #1
0
        public async Task HandleAsync(ExamUpdateCommand command)
        {
            var exam = await _context.Exam
                       .Include(x => x.Question)
                       .ThenInclude(x => x.Answer)
                       .FirstOrDefaultAsync(t => t.Id == command.Id);

            exam.Name       = command.Name;
            exam.TimeNeeded = command.TimeNeeded;
            exam.DueDate    = command.DueDate;

            exam.Question.ToList().ForEach(x =>
            {
                _context.Answer.RemoveRange(x.Answer);
            });

            _context.Question.RemoveRange(exam.Question);

            await _context.SaveChangesAsync();

            command.Questions.ForEach(x =>
            {
                var question = new Question()
                {
                    Content = x.Content,
                    Title   = x.Title,
                    TypeId  = x.TypeId,
                    ExamId  = exam.Id,
                    Value   = x.Value
                };
                _context.Question.Add(question);
                _context.SaveChanges();
                x.Answers.ForEach(y =>
                {
                    _context.Answer.Add(new Answer()
                    {
                        Content    = y.Content,
                        Correct    = y.Correct,
                        QuestionId = question.Id
                    });
                });
                _context.SaveChanges();
            });
        }
Example #2
0
        public async Task <IActionResult> Update(ExamUpdateCommand command)
        {
            await _commandBus.ExecuteAsync(command);

            return(Ok());
        }