public async Task <ActionResult> SaveStringAnswer(StringAnswer stringAnswer) { var questionId = int.Parse(Request.Form["QuestionId"].ToString()); if (stringAnswer.Id != 0) { var answer = await _questionnaireService.GetAnswer <StringAnswer>(stringAnswer.Id); if (answer != null) { answer.AnswerContents = stringAnswer.AnswerContents; } await _questionnaireService.SaveChangesAsync(); } else { stringAnswer.Question = await _questionnaireService.GetQuestion(questionId); stringAnswer.PlannedInspection = await _questionnaireService.GetPlannedInspection(stringAnswer.PlannedInspection.Id); await _questionnaireService.CreateAnswer(stringAnswer); } return(RedirectToAction("Details", new { id = stringAnswer.PlannedInspection.Id })); }
public async Task <ActionResult> Draw() { var questionId = int.Parse(Request.Form["QuestionId"]); var plannedInspectionId = int.Parse(Request.Form["plannedInspectionId"]); var plannedInspection = await _inspectionService.GetPlannedInspection(plannedInspectionId); var fileAnswer = plannedInspection.Answers .OfType <FileAnswer>() .FirstOrDefault(e => e.Question.Id == questionId); if (fileAnswer == null) { return(RedirectToAction("Details", "inspection", new { id = plannedInspection.Id })); } fileAnswer.Question = await _questionnaireService.GetQuestion(questionId); var uploadPath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Uploads"); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } var filePath = Path.Combine(uploadPath, $"{Guid.NewGuid()}.png"); var formFile = Request.Form.Files[0]; if (formFile.Length > 0) { await using var imageFile = new FileStream(filePath, FileMode.Create); await formFile.CopyToAsync(imageFile); } var answer = await _questionnaireService.GetAnswer <FileAnswer>(fileAnswer.Id); if (fileAnswer.Id != 0 && answer != null) { answer.UploadedFilePath = filePath.Replace(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\"), ""); } else { await _questionnaireService.CreateAnswer(fileAnswer); } await _questionnaireService.SaveChangesAsync(); return(RedirectToAction("Details", "inspection", new { id = fileAnswer.PlannedInspection.Id })); }