public async Task <ResponseViewModel> CreateResponse(NewResponseViewModel response, ObjectId formId) { FormObjectViewModel form = await GetForm(formId); List <FieldViewModel> fields = form.fields; if (fields.Count != response.responseValues.Count) { throw new Exception("Responses do not match fields"); } bool fieldsValid = ResponseUtils.ResponseValidator(new List <FieldViewModel>(fields), new List <NewResponseValuesViewModel>(response.responseValues)); if (!fieldsValid) { throw new Exception("Responses do not match fields"); } List <ResponseValueViewModel> responseValues = new List <ResponseValueViewModel>(); foreach (var responseValue in response.responseValues) { if (!TypeConstants.IsValidFieldType(responseValue.responseType)) { throw new Exception("Invalid Response Type"); } ResponseValueViewModel responseValueViewModel = new ResponseValueViewModel { Id = ObjectId.GenerateNewId(), responseType = responseValue.responseType, value = responseValue.value, index = responseValue.index, fieldId = ObjectId.Parse(responseValue.fieldId) }; string responseType = responseValue.responseType; if (responseType == TypeConstants.CHECKBOX_INPUT) { var values = Constants.ConvertJsonObject(responseValue.value); responseValueViewModel.value = values; } responseValues.Add(responseValueViewModel); } ResponseViewModel responseViewModel = new ResponseViewModel { Id = ObjectId.GenerateNewId(), createdBy = response.createdBy, createdAt = DateTime.UtcNow, formId = formId, responseValues = responseValues }; await responseCollection.InsertOneAsync(responseViewModel); return(responseViewModel); }
public async Task <ResponseViewModel> UpdateResponse(NewResponseViewModel response, ObjectId formId, ObjectId responseId) { DeleteResult responseDeleteResult = await responseCollection.DeleteOneAsync(_ => _.Id == responseId); if (!responseDeleteResult.IsAcknowledged) { throw new Exception("Unable to update previous response"); } FormObjectViewModel form = await GetForm(formId); List <FieldViewModel> fields = form.fields; if (fields.Count != response.responseValues.Count) { throw new Exception("Responses do not match fields"); } bool fieldsValid = ResponseUtils.ResponseValidator(new List <FieldViewModel>(fields), new List <NewResponseValuesViewModel>(response.responseValues)); if (!fieldsValid) { throw new Exception("Responses do not match fields"); } return(await CreateResponse(response, formId)); }
public IActionResult Note(NewNoteViewModel note) { if (note != null) { var dataNote = new Note { Expiry = note.Expiry, Content = note.Content, MaxReads = note.MaxReads, PIN = note.PIN }; var createResponse = manager.Create(dataNote); if (createResponse?.Success ?? false && !string.IsNullOrWhiteSpace(createResponse?.Id)) { string message = string.Format("Your FadeNote has been created. It will expire at {0} or when it is read {1} times. Which ever happens first.", createResponse.Expiry, note.MaxReads); var response = new NewResponseViewModel() { Id = createResponse.Id, Message = message, URL = Url.Link("Note", new { Id = createResponse.Id }), Expiry = createResponse.Expiry, MaxReads = note.MaxReads, }; return(View("Success", response)); } } return(View("Index")); }
public async Task <object> CreateResponseBenchmark([FromQuery] string formId, [FromQuery] string incorrectRatio) { bool formIdParseSuccess = ObjectId.TryParse(formId, out ObjectId formIdObject); if (!formIdParseSuccess) { return(BadRequest(new { success = false, message = "Invalid Form Id" })); } bool incorrectRatioParseSuccess = float.TryParse(incorrectRatio, out float incorrectRatioValue); if (!incorrectRatioParseSuccess) { incorrectRatioValue = 0; } try { FormResponseModel formResponse = await $"{Constants.BASE_URL}" .AppendPathSegment("form") .AppendPathSegment($"{formId}") .GetJsonAsync <FormResponseModel>(); NewResponseViewModel newResponse = responseGenerator.GenerateRandomResponse( formResponse.form, incorrectRatioValue); var watch = Stopwatch.StartNew(); ResponseModel response = await $"{Constants.BASE_URL}" .AppendPathSegment("response") .SetQueryParams(new { formId }) .PostJsonAsync(newResponse) .ReceiveJson <ResponseModel>(); watch.Stop(); return(Ok(new { success = true, timeElapsed = watch.ElapsedMilliseconds, response })); } catch (Exception e) { Console.WriteLine(e.Message); return(new JsonResult(new { success = false, message = e.Message })); } }