/// <summary> /// Convert a given voteQuestion to a ViewModel /// </summary> public static ViewModels.VoteQuestion ToViewModel(this Models.VoteQuestion voteQuestion) { ViewModels.VoteQuestion result = null; if (voteQuestion != null) { result = new ViewModels.VoteQuestion(); result.id = voteQuestion.Id.ToString(); List <ViewModels.VoteOption> options = new List <ViewModels.VoteOption>(); foreach (Models.VoteOption option in voteQuestion.Options) { options.Add(option.ToViewModel()); } // sort the options by display order. options.Sort( (x, y) => x == null ? (y == null ? 0 : -1) : (y == null ? 1 : x.displayOrder.CompareTo(y.displayOrder)) ); result.options = options.ToArray(); result.question = voteQuestion.Question; result.slug = voteQuestion.Slug; result.title = voteQuestion.Title; } return(result); }
public static void UpdateVoteQuestion(this AppDbContext context, ViewModels.VoteQuestion voteQuestion) { VoteQuestion _voteQuestion = context.VoteQuestions.FirstOrDefault(x => x.Slug == voteQuestion.slug); _voteQuestion.Question = voteQuestion.question; _voteQuestion.Title = voteQuestion.title; context.VoteQuestions.Update(_voteQuestion); context.SaveChanges(); }
/// <summary> /// Update region /// </summary> /// <param name="context"></param> /// <param name="regionInfo"></param> public static void UpdateSeedVoteQuestionInfo(this AppDbContext context, ViewModels.VoteQuestion voteQuestionInfo) { VoteQuestion voteQuestion = context.GetVoteQuestionBySlug(voteQuestionInfo.slug); if (voteQuestion == null) { context.AddInitialVoteQuestion(voteQuestionInfo); } else { voteQuestion.Question = voteQuestionInfo.question; voteQuestion.Title = voteQuestionInfo.title; // update the options. if (voteQuestion.Options != null) { // first pass to add new items. foreach (var option in voteQuestionInfo.options) { VoteOption voteOption = voteQuestion.Options.FirstOrDefault(x => x != null && x.Option.Equals(option.option)); if (voteOption == null) { voteQuestion.Options.Add(option.ToModel()); } else { voteOption.DisplayOrder = option.displayOrder; } } List <VoteOption> itemsToRemove = new List <VoteOption>(); // second pass to identify items that are no longer present. foreach (var option in voteQuestion.Options) { if (option != null) { if (voteQuestionInfo.options.FirstOrDefault(x => x != null && x.option.Equals(option.Option)) == null) { itemsToRemove.Add(option); } } } // third pass to remove the items. foreach (var option in itemsToRemove) { voteQuestion.Options.Remove(option); } } context.VoteQuestions.Update(voteQuestion); context.SaveChanges(); } }
/// <summary> /// Adds a jurisdiction to the system, only if it does not exist. /// </summary> private static void AddInitialVoteQuestion(this AppDbContext context, ViewModels.VoteQuestion initialVoteQuestion) { VoteQuestion voteQuestion = context.GetVoteQuestionBySlug(initialVoteQuestion.slug); if (voteQuestion != null) { return; } voteQuestion = new VoteQuestion ( initialVoteQuestion.question, initialVoteQuestion.slug, initialVoteQuestion.title, initialVoteQuestion.options ); context.AddVoteQuestion(voteQuestion); }
public static ViewModels.VoteQuestion GetViewModelVoteQuestionBySlug(this AppDbContext context, string slug) { ViewModels.VoteQuestion voteQuestion = context.VoteQuestions.Include(x => x.Options).FirstOrDefault(x => x.Slug == slug).ToViewModel(); return(voteQuestion); }