public TestModel GenerateTest(int applicantId, int internshipId) { var alreadyTested = db.C_ApplicantInternship.Any(ai => ai.N_ApplicantId == applicantId && ai.N_InternshipId == internshipId); if (!alreadyTested) { var applicantInternship = new C_ApplicantInternship { DateTestTaken = DateTime.Now, N_ApplicantId = applicantId, N_InternshipId = internshipId }; var insertedEntity = db.C_ApplicantInternship.Add(applicantInternship); db.SaveChanges(); Debug.WriteLine("Inserted CApplicantInternship"); var configuration = db.TestConfigurations.FirstOrDefault(); var result = new TestModel { ApplicantInternshipId = insertedEntity.Id, TimeLimitInMinutes = (short)configuration.TimeLimit.Minutes }; Debug.WriteLine($"Configuration: {configuration.QuestionNumber}"); var randomGenerator = new Random(DateTime.Now.Millisecond); var numberOfQuestionsInDB = db.TestQuestions.Count(); Debug.WriteLine($"Number of questions in db: {numberOfQuestionsInDB}"); if (numberOfQuestionsInDB > configuration.QuestionNumber) { result.Questions = new List <TestQuestionModel>(); while (result.Questions.Count < configuration.QuestionNumber) { var randomQuestionId = randomGenerator.Next(configuration.QuestionNumber) + 1; var question = db.TestQuestions.Find(randomQuestionId); var resultContainsGeneratedQuestionId = result.Questions.AsQueryable().Any(q => q.Id == randomQuestionId); if (!resultContainsGeneratedQuestionId && question != null) { result.Questions.Add(TestQuestionConverter.TestQuestionToTestQuestionModel(question)); } Debug.WriteLine($"Added question to test: {question.Statement}"); } } else if (numberOfQuestionsInDB == configuration.QuestionNumber) { result.Questions = db.TestQuestions.ToList().Select(q => TestQuestionConverter.TestQuestionToTestQuestionModel(q)).ToList(); } return(result); } else { return(new TestModel { ApplicantInternshipId = 0 }); } }
public TestQuestionModel Get(int id) { return(TestQuestionConverter.TestQuestionToTestQuestionModel(testQuestions.Find(id))); }
public void Add([FromBody] TestQuestionModel value) { testQuestions.Add(TestQuestionConverter.TestQuestionModelToTestQuestion(value)); db.SaveChanges(); }
public IEnumerable <TestQuestionModel> Get() { return(testQuestions.ToList().Select(a => TestQuestionConverter.TestQuestionToTestQuestionModel(a))); }