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; } }
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; } }
internal QuizItemEditModel InitializeEditQuizItemModel(int id, int?itemId) { using (var context = ApplicationDbContext.Create()) { var quiz = context.Quizes .Include(x => x.Items) .Where(x => x.QuizId == id) .FirstOrDefault(); if (quiz == null) { return(null); } var result = new QuizItemEditModel { QuizId = id, QuizTitle = quiz.Title, Order = quiz.Items.Count == 0 ? 1 : quiz.Items.Max(x => x.Order) + 1 }; if (itemId.HasValue) { var existingItem = quiz.Items.SingleOrDefault(x => x.QuizItemId == itemId); if (existingItem == null) { return(null); } result.QuizItemId = itemId.Value; result.Order = existingItem.Order; } result.Laws = context.Laws .Where(x => x.ParliamentID == quiz.ParliamentId) .OrderBy(x => x.Title) .Select(x => new System.Web.Mvc.SelectListItem { Text = x.Title, Value = x.LawID.ToString() }) .ToList(); return(result); } }