// GET: Quizzes/Details/5 public async Task <IActionResult> Details(int?id) { if (!id.HasValue) { return(BadRequest(Constants.ErrorMessages.BadRequest)); } var quiz = await _quizRepository.GetById(id); if (quiz == null) { return(NotFound(Constants.ErrorMessages.NotFound)); } var questions = await _questionRepository.GetByQuizId(id); if (questions == null) { return(NotFound(Constants.ErrorMessages.NotFoundQuestion)); } var model = new DetailsQuizViewModel { Quiz = quiz, Questions = questions, }; return(View(model)); }
public IActionResult Details(int id) { if (id <= 0) { return(this.NotFound()); } DetailsQuizViewModel model = this.quizzesService.GetQuizWithQuestionsAndAnswersById(id); return(this.View(model)); }
public IActionResult ExportQuizQuestions(int id) { if (id <= 0) { return(this.NotFound()); } DetailsQuizViewModel model = this.quizzesService.GetQuizWithQuestionsAndAnswersById(id); MemoryStream stream = this.exportService.ExportQuizQuestions(model); //Download Word document in the browser return(this.File(stream, "application/msword", $"{model.Title}.docx")); }
public DetailsQuizViewModel GetQuizWithQuestionsAndAnswersById(int id) { DetailsQuizViewModel model = this.quizisRepository .AllAsNoTracking() .Where(x => x.Id == id) .To <DetailsQuizViewModel>() .FirstOrDefault(x => x.Id == id); model.Questions = this.questionsService.GetQuestionsByQuizId <QuestionQuizViewModel>(id); foreach (var question in model.Questions) { question.Answers = this.answersService.GetQuestionAnswersById <AnswerViewModel>(question.Id); } return(model); }
public MemoryStream ExportQuizQuestions(DetailsQuizViewModel model) { WordDocument document = new WordDocument(); //Adds new section to the document IWSection section = document.AddSection(); IWParagraph paragraph = section.AddParagraph(); paragraph.ApplyStyle(BuiltinStyle.Heading1); paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; paragraph.AppendText($"{model.CategoryName} - {model.Title}"); foreach (var question in model.Questions) { paragraph = section.AddParagraph(); //Applies default numbered list style paragraph.ListFormat.ApplyDefNumberedStyle(); //Adds text to the paragraph paragraph.AppendText($"{question.Value}"); //Continues the list defined paragraph.ListFormat.ContinueListNumbering(); paragraph = section.AddParagraph(); paragraph.ListFormat.IncreaseIndentLevel(); foreach (var answer in question.Answers) { paragraph.AppendText($"{answer.Value}"); //Continues the list defined paragraph.ListFormat.ContinueListNumbering(); paragraph = section.AddParagraph(); } paragraph.ListFormat.DecreaseIndentLevel(); } //Saves the Word document to MemoryStream MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); stream.Position = 0; //Download Word document in the browser return(stream); }