public static void BuildSection(AspergillosisContext context,
                                        CaseReportFormSectionViewModel formSectionVM)
        {
            var formSection = new CaseReportFormSection()
            {
                Name = formSectionVM.Name
            };

            formSection.CaseReportFormResultFields = new List <CaseReportFormField>();
            foreach (var field in formSectionVM.Fields)
            {
                formSection.CaseReportFormResultFields.Add(field);
                context.CaseReportFormFields.Add(field);
                if (field.SelectedOptionsIds == null)
                {
                    continue;
                }
                foreach (var fieldOptionId in field.SelectedOptionsIds)
                {
                    var sectionOption = new CaseReportFormFieldOption();
                    sectionOption.CaseReportFormOptionChoiceId = fieldOptionId;
                    sectionOption.Field = field;
                    context.CaseReportFormFieldOptions.Add(sectionOption);
                }
            }
            context.CaseReportFormSections.Add(formSection);
        }
 public IActionResult EditFormSection(int?id,
                                      CaseReportFormSection section,
                                      CaseReportFormField[] fields)
 {
     section.CaseReportFormResultFields = fields;
     foreach (var field in section.CaseReportFormResultFields.OrderBy(f => f.ID))
     {
         var dbOptionsIds = _context.CaseReportFormFieldOptions.Where(fo => fo.CaseReportFormFieldId == field.ID).Select(fo => fo.CaseReportFormOptionChoiceId).ToList();
         var formIds      = new List <int>();
         if (field.SelectedOptionsIds != null)
         {
             formIds = field.SelectedOptionsIds.ToList();
         }
         var toDeleteOptions = dbOptionsIds.Except(formIds);
         var toInsertOptions = formIds.Except(dbOptionsIds);
         if (toDeleteOptions.Count() > 0)
         {
             var fieldOptions = _context.CaseReportFormFieldOptions.Where(fo => fo.CaseReportFormFieldId == field.ID &&
                                                                          toDeleteOptions.Contains(fo.CaseReportFormOptionChoiceId));
             _context.RemoveRange(fieldOptions);
         }
         if (toInsertOptions.Count() > 0)
         {
             foreach (var optionId in toInsertOptions)
             {
                 var fieldOption = new CaseReportFormFieldOption();
                 fieldOption.CaseReportFormFieldId        = field.ID;
                 fieldOption.CaseReportFormOptionChoiceId = optionId;
                 _context.Add(fieldOption);
             }
         }
     }
     if (ModelState.IsValid)
     {
         _context.Update(section);
         _context.SaveChanges();
         return(Json(new { valid = true }));
     }
     else
     {
         var errors = ModelStateHelper.Errors(ModelState);
         return(Json(new { errors = errors, valid = false }));
     }
 }
 private void BuildFormWithSections(CaseReportFormViewModel caseReportFormViewModel,
                                    out CaseReportForm caseReportForm,
                                    out List <CaseReportFormFormSection> sections)
 {
     caseReportForm        = new CaseReportForm();
     sections              = new List <CaseReportFormFormSection>();
     caseReportForm.Fields = new List <CaseReportFormField>();
     if (caseReportFormViewModel.SectionsIds != null)
     {
         foreach (var sectionId in caseReportFormViewModel.SectionsIds)
         {
             var section = _context.CaseReportFormSections
                           .Where(s => s.ID == sectionId)
                           .SingleOrDefault();
             var formSection = new CaseReportFormFormSection();
             formSection.CaseReportFormSectionId = sectionId;
             sections.Add(formSection);
         }
     }
     if (caseReportFormViewModel.Fields != null)
     {
         foreach (var field in caseReportFormViewModel.Fields)
         {
             if (field.SelectedOptionsIds == null)
             {
                 continue;
             }
             foreach (var fieldOptionId in field.SelectedOptionsIds)
             {
                 var sectionOption = new CaseReportFormFieldOption();
                 sectionOption.CaseReportFormOptionChoiceId = fieldOptionId;
                 sectionOption.Field = field;
                 _context.CaseReportFormFieldOptions.Add(sectionOption);
             }
         }
     }
     caseReportForm.Name = caseReportFormViewModel.Name;
     caseReportForm.CaseReportFormCategoryId = caseReportFormViewModel.CaseReportFormCategoryId;
     caseReportForm.Fields   = caseReportFormViewModel.Fields;
     caseReportForm.Sections = sections;
 }