public HttpResponseMessage SubmitFormAPI2(SubmittedFormAPIViewModel viewModel) { Logger.Current.Verbose("In SubmitFormAPI2 method. Request received to submit form."); SubmittedFormViewModel submittedFormViewModel = new SubmittedFormViewModel(); submittedFormViewModel.SubmittedFormFields = new List <SubmittedFormFieldViewModel>(); var getFormData = viewModel.SubmittedFormFields.ToList(); var frm = getFormData.GroupBy(x => x.Key).Select(x => new { Key = x.Select(g => g.Key).FirstOrDefault(), Value = string.Join("|", x.Select(g => g.Value).ToList()) }); foreach (var field in frm) { submittedFormViewModel.SubmittedFormFields.Add(new SubmittedFormFieldViewModel() { Key = field.Key, Value = !string.IsNullOrEmpty(field.Value) ? field.Value.Trim() : field.Value }); } SubmitFormRequest request = new SubmitFormRequest() { SubmittedFormViewModel = submittedFormViewModel, RequestedBy = this.UserId, AccountId = viewModel.AccountId }; request.SubmittedFormViewModel.SubmittedOn = DateTime.Now.ToUniversalTime(); request.SubmittedFormViewModel.FormId = viewModel.FormId; request.SubmittedFormViewModel.AccountId = viewModel.AccountId; SubmitFormResponse response = formService.SubmitForm(request); if (response.Exception == null && response.FormSubmissionEntryViewModel != null) { return(Request.BuildResponse(new FormSubmissionAPIResponse() { Acknowledgement = "Form submitted successfully", FormSubmissionId = response.FormSubmissionEntryViewModel.FormSubmissionId })); } else { return(Request.BuildResponse(new FormSubmissionAPIResponse() { Acknowledgement = "Form submission failed", Exception = response.Exception })); } }
public static SubmittedFormViewModel GenerateSubmittedFormViewModel() { SubmittedFormViewModel newForm = new SubmittedFormViewModel(); newForm.IPAddress = "192.168.1.1"; newForm.AccountId = 1; newForm.SubmittedOn = new DateTime(); newForm.SubmittedFormFields = new List <SubmittedFormFieldViewModel>(); for (byte i = 1; i <= 2; i++) { newForm.SubmittedFormFields.Add(new SubmittedFormFieldViewModel() { Key = i.ToString(), Value = i.ToString() }); } return(newForm); }
public HttpResponseMessage SubmitForm(FormDataCollection formData) { Logger.Current.Verbose("In SubmitForm method. Request received to submit form."); HttpResponseMessage formSubmissionResponse; SubmittedFormViewModel submittedFormViewModel = new SubmittedFormViewModel(); var context = System.Web.HttpContext.Current.Request; submittedFormViewModel.SubmittedFormFields = new List <SubmittedFormFieldViewModel>(); try { Logger.Current.Informational("Submitted form from Context: " + context.Form); if (formData != null) { var getFormData = formData.ToList(); var submittedToForm = formData.Where(c => c.Key == "formid").FirstOrDefault(); var formAccountId = formData.Where(c => c.Key == "accountid").FirstOrDefault(); var requestDomain = formData.Where(c => c.Key == "domainname").FirstOrDefault(); var submittedBy = formData.Where(c => c.Key == "userid").FirstOrDefault(); var stiTrackingId = formData.Where(c => c.Key == "STITrackingID").FirstOrDefault(); var link = formData.Where(c => c.Key == "redirect-override").FirstOrDefault(); getFormData.Remove(submittedToForm); getFormData.Remove(formAccountId); getFormData.Remove(requestDomain); getFormData.Remove(submittedBy); getFormData.Remove(stiTrackingId); getFormData.Remove(link); int parsedFormId = 0; int parsedAccountId = 0; int.TryParse(submittedToForm.Value, out parsedFormId); int.TryParse(formAccountId.Value, out parsedAccountId); if (parsedFormId > 0 && parsedAccountId > 0) { Logger.Current.Verbose("Request received to submit form with ID: " + parsedFormId); try { var frm = getFormData.GroupBy(x => x.Key).Select(x => new { Key = x.Select(g => g.Key).FirstOrDefault(), value = string.Join("|", x.Select(g => g.Value).ToList()) }); foreach (var field in frm) { submittedFormViewModel.SubmittedFormFields.Add(new SubmittedFormFieldViewModel() { Key = field.Key, Value = !string.IsNullOrEmpty(field.value) ? field.value.Trim() : field.value }); } SubmitFormRequest request = new SubmitFormRequest() { SubmittedFormViewModel = submittedFormViewModel, RequestedBy = this.UserId, AccountId = parsedAccountId }; request.SubmittedFormViewModel.SubmittedOn = DateTime.Now.ToUniversalTime(); request.SubmittedFormViewModel.FormId = parsedFormId; request.SubmittedFormViewModel.AccountId = parsedAccountId; request.SubmittedFormViewModel.IPAddress = requestDomain.Value; request.SubmittedFormViewModel.STITrackingID = stiTrackingId.Value; var domain = accountService.GetAccountDomainUrl(new GetAccountDomainUrlRequest() { AccountId = parsedAccountId }).DomainUrl; var userId = 0; int.TryParse(submittedBy.Value, out userId); request.SubmittedFormViewModel.OwnerId = domain == requestDomain.Value ? userId : 0; // If this is a test form submission domain and requestDomain would be same. Hence, the submitting user will be the owner. formService.SubmitForm(request); } catch (Exception ex) { Logger.Current.Error("Error in form submission: ", ex); } finally { SubmitFormResponse getFormResponse = formService.GetFormAcknowdegement(parsedFormId); Logger.Current.Informational("Response: " + getFormResponse.Acknowledgement.Acknowledgement); formSubmissionResponse = Request.BuildResponse(new FormResponse() { Success = true, Acknowledgement = !string.IsNullOrEmpty(link.Value) ? link.Value : getFormResponse.Acknowledgement.Acknowledgement, AcknowledgementType = !string.IsNullOrEmpty(link.Value) ? AcknowledgementType.Url : getFormResponse.Acknowledgement.AcknowledgementType }); } } else { formSubmissionResponse = new HttpRequestMessage().CreateResponse(HttpStatusCode.NotAcceptable); } } else { throw new Exception("Received empty form data"); } } catch (Exception ex) { ex.Data.Add("submitteddata", formData); Logger.Current.Error("Error in form submission: ", ex); formSubmissionResponse = new HttpRequestMessage().CreateResponse(HttpStatusCode.NotAcceptable); } return(formSubmissionResponse); }