public IActionResult DeleteUserQuestionByUQAS([FromBody] UserQuestionAnswerSet q) { foreach (AnsweredSubQuestion asq in q.AnsweredSubQuestions) { foreach (Answer a in asq.Answers) { _context.Answers.Remove(a); } _context.SubQuestions.Remove(asq.SubQuestion); } _context.RootQuestions.Remove(q.RootQuestion); _context.SaveChanges(); return(NoContent()); }
private void write_csv(int rootId) { UserQuestionAnswerSet uqas = GetUserQuestionsById(rootId).Value; string csvHeader = ""; List <List <string> > answers = new List <List <string> >(); int counter = 0; foreach (AnsweredSubQuestion asq in uqas.AnsweredSubQuestions) { csvHeader += asq.SubQuestion.Name + ","; answers.Add(new List <string>()); foreach (Answer ans in asq.Answers) { answers[counter].Add(ans.Value ? "Yes" : "No"); } counter++; } csvHeader = csvHeader.Remove(csvHeader.LastIndexOf(',')); List <string> answer_rows = new List <string>(); int minindex = 0; int minval = answers[0].Count; for (int i = 0; i < answers.Count; i++) { if (answers[i].Count < minval) { minindex = i; } } for (int i = 0; i < answers[minindex].Count; i++) { string line = ""; foreach (List <string> answerSet in answers) { line += answerSet[i] + ","; } line = line.Remove(line.LastIndexOf(",")); answer_rows.Add(line); } string path = @"wwwroot/lib/dtl_algorithm/data_" + rootId + ".csv"; // This text is added only once to the file. //if (System.IO.File.Exists(path)) //{ // using (StreamWriter sw = System.IO.File.CreateText(path)) // { // sw.WriteLine(csvHeader); // } //} // Create a file to write to. while (locked) { } locked = true; using (StreamWriter sw = System.IO.File.CreateText(path)) { sw.WriteLine(csvHeader); } using (StreamWriter sw = System.IO.File.AppendText(path)) { foreach (string row in answer_rows) { sw.WriteLine(row); } } locked = false; }