PracowniaTestowania/WebApplication5/Templates/TemplateGenerator.cs

73 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication5.Models;
using WebApplication5.Writer;
namespace WebApplication5.Templates
{
public class TemplateGenerator
{
private TestsDbContext _db;
private TemplatesOrder _templatesOrder;
private string fileName = Guid.NewGuid().ToString();
private WriterPDF writer= new WriterPDF();
public TemplateGenerator(TestsDbContext db, TemplatesOrder templatesOrder)
{
_db = db;
_templatesOrder = templatesOrder;
}
public void ProcessOrder()
{
writer.CreateFile(fileName);
for (int i = 0; i < _templatesOrder.NumberOfExamples; i++)
{
var questionNumber = 1;
writer.WriteTitle(_templatesOrder.TestName);
var questions = new List<Question>();
foreach (var cat in _templatesOrder.Categories)
{
writer.WriteCategory(_db.Categories.First(c => c.Id == cat.CategoryId).Name);
var catQuestions = GetQuestionsForCategory(cat);
writer.WriteQuestions(catQuestions, questionNumber);
questionNumber += catQuestions.Count;
questions = questions.Concat(catQuestions)
.ToList();
}
writer.EndPage();
writer.WriteAnswerTemplate(questions, string.Join("_", questions.Select(q => q.Id)));
//TODO
//wrtie to DB
//QRcode + questions
}
writer.CloseFile();
}
private List<Question> GetQuestionsForCategory(TemplateViewModel templatesToGenerate)
{
var questions = _db.Questions.Include("Answers").Where(q => q.CategoryId == templatesToGenerate.CategoryId).ToList();
if (templatesToGenerate.QuestionsIds.Any())
{
questions = questions
.Where(q => templatesToGenerate.QuestionsIds.Contains(q.Id))
.ToList();
}
else
{
questions = questions
.OrderBy(arg => Guid.NewGuid()).Take(templatesToGenerate.NumberOfQuestions)
.ToList();
}
return questions;
}
}
}