Ejemplo n.º 1
0
        private void MapQuestions(EditQuizModel source, Quiz destination)
        {
            var existingQuestions = new List <Question>(destination.Questions);

            destination.Questions.Clear();

            foreach (var question in source.Questions)
            {
                var existingQuestion = existingQuestions.FirstOrDefault(q => q.Id == question.Id);
                if (existingQuestion == null)
                {
                    destination.Questions.Add(this.Mapper.Map <Question>(question));
                }
                else
                {
                    destination.Questions.Add(existingQuestion);

                    this.Mapper.Map(question, existingQuestion);
                    var existingAnswers = new List <Answer>(existingQuestion.Answers);
                    existingQuestion.Answers.Clear();

                    foreach (var answer in question.Answers)
                    {
                        var existingAnswer = existingAnswers.FirstOrDefault(a => a.Id == answer.Id);
                        if (existingAnswer == null)
                        {
                            existingQuestion.Answers.Add(this.Mapper.Map <Answer>(answer));
                        }
                        else
                        {
                            this.Mapper.Map(answer, existingAnswer);
                            existingQuestion.Answers.Add(existingAnswer);
                        }
                    }

                    this.Mapper.Map(question, existingQuestion);
                }
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult Update(int id, EditQuizModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var  dataQuiz    = this.quizzes.GetById(id);
            bool userIsAdmin = this.User.IsInRole(GlobalConstants.AdministratorRoleName);

            if (dataQuiz == null || (this.UserId != dataQuiz.CreatedById && !userIsAdmin))
            {
                return(this.NotFound());
            }

            var category = this.categories.GetById(model.Category.Id);

            if (category == null)
            {
                return(this.BadRequest(this.ModelState));
            }

            this.Mapper.Map(model, dataQuiz);
            this.MapQuestions(model, dataQuiz);
            dataQuiz.Category = category;

            try
            {
                this.quizzes.Save();
                return(this.Ok(new { message = "Quiz updated successfully" }));
            }
            catch (QuizCreationException ex)
            {
                return(this.BadRequest(ex.Message));
            }
        }
Ejemplo n.º 3
0
        public IHttpActionResult Update(int id, EditQuizModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var dataQuiz = this.quizzes.GetById(id);
            bool userIsAdmin = this.User.IsInRole(GlobalConstants.AdministratorRoleName);

            if (dataQuiz == null || (this.UserId != dataQuiz.CreatedById && !userIsAdmin))
            {
                return this.NotFound();
            }

            var category = this.categories.GetById(model.Category.Id);
            if (category == null)
            {
                return this.BadRequest(this.ModelState);
            }

            this.Mapper.Map(model, dataQuiz);
            this.MapQuestions(model, dataQuiz);
            dataQuiz.Category = category;

            try
            {
                this.quizzes.Save();
                return this.Ok(new { message = "Quiz updated successfully" });
            }
            catch (QuizCreationException ex)
            {
                return this.BadRequest(ex.Message);
            }
        }
Ejemplo n.º 4
0
        private void MapQuestions(EditQuizModel source, Quiz destination)
        {
            var existingQuestions = new List<Question>(destination.Questions);
            destination.Questions.Clear();

            foreach (var question in source.Questions)
            {
                var existingQuestion = existingQuestions.FirstOrDefault(q => q.Id == question.Id);
                if (existingQuestion == null)
                {
                    destination.Questions.Add(this.Mapper.Map<Question>(question));
                }
                else
                {
                    destination.Questions.Add(existingQuestion);

                    this.Mapper.Map(question, existingQuestion);
                    var existingAnswers = new List<Answer>(existingQuestion.Answers);
                    existingQuestion.Answers.Clear();

                    foreach (var answer in question.Answers)
                    {
                        var existingAnswer = existingAnswers.FirstOrDefault(a => a.Id == answer.Id);
                        if (existingAnswer == null)
                        {
                            existingQuestion.Answers.Add(this.Mapper.Map<Answer>(answer));
                        }
                        else
                        {
                            this.Mapper.Map(answer, existingAnswer);
                            existingQuestion.Answers.Add(existingAnswer);
                        }
                    }

                    this.Mapper.Map(question, existingQuestion);
                }
            }
        }