Esempio n. 1
0
        internal void UpdateQuizItem(QuizItemEditModel model)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var quiz = context.Quizes
                           .Include(x => x.Items)
                           .Where(x => x.QuizId == model.QuizId)
                           .FirstOrDefault();

                if (quiz == null)
                {
                    return;
                }

                var item = quiz.Items.SingleOrDefault(x => x.QuizItemId == model.QuizItemId);
                if (item == null)
                {
                    return;
                }

                var law = context.Laws.SingleOrDefault(x => x.LawID == model.LawId);
                if (law == null)
                {
                    return;
                }
                LawSection section = null;

                if (model.LawSectionId > 0)
                {
                    section = context.LawSections.SingleOrDefault(x => x.LawSectionID == model.LawSectionId);
                    if (section == null)
                    {
                        return;
                    }
                }



                item.Law        = section != null ? null : law;
                item.LawSection = section != null ? section : null;
                item.Type       = section != null ? QuizItemType.LawSection : QuizItemType.Law;

                context.SaveChanges();


                return;
            }
        }
Esempio n. 2
0
        internal void CreateNewQuizItemModel(QuizItemEditModel model)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var quiz = context.Quizes
                           .Include(x => x.Items)
                           .Where(x => x.QuizId == model.QuizId)
                           .FirstOrDefault();

                if (quiz == null)
                {
                    return;
                }

                var law = context.Laws.SingleOrDefault(x => x.LawID == model.LawId);
                if (law == null)
                {
                    return;
                }
                LawSection section = null;

                if (model.LawSectionId > 0)
                {
                    section = context.LawSections.SingleOrDefault(x => x.LawSectionID == model.LawSectionId);
                    if (section == null)
                    {
                        return;
                    }
                }

                var newItem = new QuizItem
                {
                    Quiz       = quiz,
                    Order      = model.Order,
                    Law        = section != null ? null : law,
                    LawSection = section != null ? section : null,
                    Type       = section != null ? QuizItemType.LawSection : QuizItemType.Law,
                };

                context.QuizItems.Add(newItem);
                context.SaveChanges();


                return;
            }
        }