public string FillNewForm( string formModel, string userId ) { formFieldJSONModel formFieldData = JsonConvert.DeserializeObject<formFieldJSONModel>( formModel ); List<FieldData> fieldsAndOptions = formFieldData.fieldsData.Select( fieldData => new FieldData() { FormFieldId = fieldData.fieldId, IsOption = false, Value = GetStringDataFromObject( fieldData.fieldType, fieldData.fieldData ) } ).ToList(); List<optionData> fieldsWithOptions = new List<optionData>(); formFieldData.fieldsData .Where( fieldData => fieldData.options != null ) .ToList() .ForEach( fieldData => fieldsWithOptions.AddRange( fieldData.options.ToList() ) ); fieldsAndOptions.AddRange( fieldsWithOptions.Select( option => new FieldData() { FormFieldId = option.optionId, IsOption = true, Value = ( (bool)option.value ).ToString() } ).ToList() ); UserFormFieldData userFormFieldData = new UserFormFieldData() { DateCreating = DateTime.Now, ResearchFormId = formFieldData.formId, UserId = userId, ResearchNumber = _dbContext.ResearchForms.Find( formFieldData.formId ).UserFormsFieldData.Count + 1, Options = fieldsAndOptions }; _dbContext.ResearchForms.Find( formFieldData.formId ).UserFormsFieldData.Add( userFormFieldData ); _dbContext.SaveChanges(); var shortFormFieldData = new { formFieldDate = userFormFieldData.DateCreating, formFieldId = userFormFieldData.Id, formFieldNumber = userFormFieldData.ResearchNumber }; return JsonConvert.SerializeObject( shortFormFieldData ); }
private List<dynamic> GetFieldsDataInCorrectOrder( UserFormFieldData formFieldData ) { var formFieldsDataOrder = formFieldData.ResearchForm.Fields.OrderBy( x => x.PositionOnForm ).ToList(); List<Tuple<long, bool, bool>> fieldIdInCorrectOrder = new List<Tuple<long, bool, bool>>(); formFieldsDataOrder.ForEach( field => { if( field.Options == null || field.Options.Count == 0 ) { fieldIdInCorrectOrder.Add( new Tuple<long, bool, bool>( field.Id, false, false ) ); } if( field.FieldType != StaticHelper.ControlTypes["checkbox-group"] && ( field.Options != null && field.Options.Count > 0 ) ) { fieldIdInCorrectOrder.Add( new Tuple<long, bool, bool>( field.Id, false, true ) ); } if( field.FieldType == StaticHelper.ControlTypes["checkbox-group"] && ( field.Options != null && field.Options.Count > 0 ) ) { field.Options.ToList().ForEach( option => fieldIdInCorrectOrder.Add( new Tuple<long, bool, bool>( option.Id, true, false ) ) ); } } ); List<dynamic> fields = new List<dynamic>(); fieldIdInCorrectOrder.ForEach( fieldInForm => { if( !fieldInForm.Item3 ) { FieldData fieldData = formFieldData .Options .FirstOrDefault( field => field.IsOption == fieldInForm.Item2 && field.FormFieldId == fieldInForm.Item1 ); fields.Add( new { value = fieldData == null ? null : fieldData.Value, fieldId = fieldInForm.Item1 } ); } else { FieldData selectedOption = formFieldData .Options .FirstOrDefault( field => !field.IsOption && field.FormFieldId == fieldInForm.Item1 ); if( selectedOption != null ) { long selectedOptionId; bool isNotEmpty = long.TryParse( selectedOption.Value, out selectedOptionId ); fields.Add( new { value = isNotEmpty ? _dbContext.Options.Find( selectedOptionId ).Name:null, fieldId = fieldInForm.Item1 } ); } else { fields.Add( new { value = "", fieldId = fieldInForm.Item1 } ); } } } ); return fields; }