public async Task <bool> PostFormAnswerAsync(Models.FormAnswer formAnswer) { var route = "coordinators/" + formAnswer.CoordinatorId + "/answer/" + formAnswer.FormId; var uri = new Uri(string.Format(Constants.RestUrl, route)); var body = FormParser.PostFormAnswerBuilder(formAnswer); var content = new StringContent(body.ToString(), Encoding.UTF8, "application/json"); var contentString = await content.ReadAsStringAsync(); Debug.WriteLine("[Form API] - Answer content built: " + contentString); try { var response = await _client.PostAsync(uri, content); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); Debug.WriteLine("[Form API] - Post Answer result: " + responseContent); return(true); } else { var failedContent = await response.Content.ReadAsStringAsync(); Debug.WriteLine("[Form API] - Post Answer response unsuccessful " + failedContent); return(false); } } catch (Exception ex) { Debug.WriteLine("[Form API exception]:" + ex.Message); return(false); } }
public static JObject PostFormAnswerBuilder(Models.FormAnswer formAnswer) { JObject body = new JObject(); JObject formAnswerContent = new JObject(); JArray multipleChoiceAnswersJSON = new JArray(); JArray discursiveAnswersJSON = new JArray(); var multipleChoiceAnswers = formAnswer.MultipleChoiceAnswers; var discursiveAnswers = formAnswer.DiscursiveAnswers; formAnswerContent.Add("formId", formAnswer.FormId); formAnswerContent.Add("coordinatorId", formAnswer.CoordinatorId); foreach (Models.MultipleChoiceAnswer answer in multipleChoiceAnswers) { JObject multipleChoiceQuestion = new JObject(); JArray optionsAnswered = new JArray(); multipleChoiceQuestion.Add("question", answer.Question); foreach (string option in answer.Answers) { optionsAnswered.Add(option); } multipleChoiceQuestion.Add("answers", optionsAnswered); multipleChoiceAnswersJSON.Add(multipleChoiceQuestion); } foreach (Models.DiscursiveQuestion discursiveQuestion in discursiveAnswers) { JObject discursiveQuestionAnswer = new JObject { { "question", discursiveQuestion.Question }, { "answer", discursiveQuestion.Answer } }; discursiveAnswersJSON.Add(discursiveQuestionAnswer); } formAnswerContent.Add("discursiveAnswers", discursiveAnswersJSON); formAnswerContent.Add("multipleChoiceAnswers", multipleChoiceAnswersJSON); body.Add("formAnswer", formAnswerContent); return(body); }