public async Task <IActionResult> Create(CaseReportFormViewModel caseReportFormViewModel)
        {
            try
            {
                CaseReportForm caseReportForm;
                List <CaseReportFormFormSection> sections;
                BuildFormWithSections(caseReportFormViewModel, out caseReportForm, out sections);

                if (caseReportFormViewModel.SectionsIds == null && caseReportFormViewModel.Fields == null)
                {
                    ModelState.AddModelError("Base",
                                             "Add least one section or field needs to be present on any form");
                }

                if (ModelState.IsValid)
                {
                    _context.CaseReportFormFormSections.AddRange(sections);
                    _context.CaseReportForms.Add(caseReportForm);
                    await _context.SaveChangesAsync();

                    return(Json(new { result = "ok" }));
                }
                else
                {
                    Hashtable errors = ModelStateHelper.Errors(ModelState);
                    return(Json(new { success = false, errors }));
                }
            }
            catch (DbUpdateException)
            {
                return(null);
            }
        }
        public IActionResult Show(int?id)
        {
            var caseReportForm = _caseReportFormResolver.FindByIdWithAllRelations(id.Value);

            if (caseReportForm == null)
            {
                return(NotFound());
            }
            var viewModel = CaseReportFormViewModel.BuildViewModel(caseReportForm);

            return(PartialView(@"/Views/CaseReportForms/_Show.cshtml", viewModel));
        }
        public IActionResult Edit(int?id)
        {
            var caseReportForm = _caseReportFormResolver.FindByIdWithAllRelations(id.Value);

            if (caseReportForm == null)
            {
                return(NotFound());
            }
            var viewModel  = CaseReportFormViewModel.BuildViewModel(caseReportForm);
            var sectionIds = caseReportForm.Sections.Select(s => s.CaseReportFormSectionId).ToList();

            ViewBag.CategoriesIds = _dropdownResolver.PopuplateCRFCategoriesDropdownList(caseReportForm.CaseReportFormCategoryId);
            ViewBag.SectionIds    = _dropdownResolver.PopulateCRFSectionsDropdownList(sectionIds);
            _caseReportFormResolver.BuildFormFor(ViewBag, caseReportForm.Fields.ToList(), _dropdownResolver);

            return(PartialView(@"/Views/CaseReportForms/_Edit.cshtml", viewModel));
        }
 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;
 }
        public void QueryCaseReportBuiltForms()
        {
            var results = _aspergillosisContext.CaseReportForms
                          .Include(crf => crf.Sections)
                          .ThenInclude(s => s.Section)
                          .Include(crf => crf.CaseReportFormCategory)
                          .Include(crf => crf.Fields)
                          .ToList <dynamic>();

            foreach (CaseReportForm result in results)
            {
                var viewModel = new CaseReportFormViewModel()
                {
                    ItemId        = result.ID.ToString(),
                    IsLocked      = result.IsLocked ? "<label class='label label-danger'>YES</label>" : "<label class='label label-success'>NO</label>",
                    Name          = result.Name,
                    CategoryName  = result.CaseReportFormCategory.Name,
                    SectionsNames = result.Sections.Select(s => s.Section.Name).ToList(),
                    FieldsNames   = result.Fields.Select(f => f.Label).ToList()
                };
                _list.Add(viewModel);
            }
        }