private static async Task <bool> DownloadDocument(object data) { bool result = false; AzureBlobDocuments document = jsonHelper.FromJson <AzureBlobDocuments>(data.ToString()); //AzureBlobDocuments document = JsonConvert.DeserializeObject<AzureBlobDocuments>(data.ToString()); CloudAppendBlob appendBlob = CONTAINER.GetAppendBlobReference(document.Filename); // Get a reference to a blob named. List <AzureBlobDocuments> azureCollection = ListDocuments().GetAwaiter().GetResult(); if (azureCollection != null) { if (azureCollection.Exists(o => o.Filename == document.Filename)) { try { new FileInfo(DOWNLOAD_DIRECTORY).Directory.Create(); //Create directory if not found using (var fileStream = System.IO.File.OpenWrite(DOWNLOAD_DIRECTORY + document.Filename)) { await appendBlob.DownloadToStreamAsync(fileStream); result = true; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception Caught - " + ex.Message); } } } return(result); }
private static async Task <List <CSVResponse> > GetSurveyResponses(object data) { AzureBlobDocuments document = jsonHelper.FromJson <AzureBlobDocuments>(data.ToString()); CSVResponse question = new CSVResponse(); List <CSVResponse> csvResponse = new List <CSVResponse>(); bool headerIgnore = true; string csvData = null; CloudAppendBlob appendBlob = CONTAINER.GetAppendBlobReference(document.Filename); try { csvData = await appendBlob.DownloadTextAsync(); if (csvData != null) { csvData = csvData.Replace("\r\n", ",-,"); //replace "\r\n" with end line character csvData = csvData.Remove(csvData.Length - 1); //remove trailing "," before splitting to stop erronous final record. string[] values = csvData.Split(','); for (int i = 0; i < values.Count(); i++) { Int32.TryParse(values[i++], out int surveyID); question.SurveyID = surveyID; question.Date = values[i++]; question.Time = values[i++]; int j = 1; do { if (values[i].Equals("-")) { break; } question.Responses.Add(new QuestionResponse() { QuestionNumber = j++, Answer = values[i++] }); } while (i <= values.Count()); if (!headerIgnore) { csvResponse.Add(question); } question = new CSVResponse(); headerIgnore = false; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception Caught - " + ex.Message); } return(csvResponse); }
public string Post([FromBody] object data, string option) { string result = "Error unable to process request. Please ensure all inputs are valid."; if (option != null && data != null) { switch (option.ToLower()) { case "download": result = DownloadDocument(data).GetAwaiter().GetResult() ? "Successfully downloaded file to " + DOWNLOAD_DIRECTORY : "Error - File not downloaded please try again"; break; case "report": //For data manipulation List <CSVResponse> reportData = GetSurveyResponses(data).GetAwaiter().GetResult(); AzureBlobDocuments document = jsonHelper.FromJson <AzureBlobDocuments>(data.ToString()); if (reportData != null) { ReportAnalysisModel report = CollateReportData(reportData); report.ReportTitle = document.Filename; if (report != null) { result = JsonConvert.SerializeObject(report); } } else { result = "Error, unable to run report."; } break; case "saveresponse": //To append a response to csv in azure. result = AppendResponse(data).GetAwaiter().GetResult() ? "Successfully added response to CSV" : "Error adding response to CSV"; break; default: break; } } return(result); }