public ServiceResult<Question> CreateQuestion(Question Question, List<string> Answers)
 {
     try
     {
         var questionCreated=adminRepository.CreateQuestion(Question);
         if(questionCreated.Success==true)
         {
             var answerCreated = adminRepository.CreateQuestionAnswer(Answers);
             if(answerCreated.Success==true)
             {
                 return ServiceResult<Question>.SuccessFunc("Question created successfully", Question);
             }
             else
             {
                 return ServiceResult<Question>.ErrorFunc("Question can not be created", Question);
             }
         }
         else
         {
             return ServiceResult<Question>.ErrorFunc("Question already exists", Question);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
     }
     return null;
 }
        public RepositoryResult<Question> GetNextQuestion(Question question, string id)
        {
            try
            {
                if (id == "Skipped")
                {
                    TempClass.NextQuestionNumber++;
                    TempClass.TotallyAnsweredQuestions++;
                }

                var Question = NextQuetion(question.ID, question.TestName, question.DifficultyLevel, question.UserName);
                if (Question != null)
                {
                    return RepositoryResult<Question>.SuccessFunc("", Question);
                }
                else
                {
                    return RepositoryResult<Question>.ErrorFunc();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return null;
        }
 public ActionResult Index(Question question, string id)
 {
     var GetNextQuestion = questionService.GetNextQuestion(question, id);
     if(GetNextQuestion.Success)
     {
         return View(GetNextQuestion.Value);
     }
     else
     {
         var testResult = questionService.GetTestResult(question);
         Thread.Sleep(2000);
         return View("TestCompleted", testResult);
     }
 }
 public ServiceResult<bool> EditQuestion(Question question, List<string> InputAnswers)
 {
     try
     {
         var questionEdited = adminRepository.EditQuestion(question, InputAnswers);
         if(questionEdited.Success)
         {
             return ServiceResult<bool>.SuccessFunc();
         }
         else
         {
             return ServiceResult<bool>.ErrorFunc();
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
     }
     return null;
 }
 public ActionResult CreateQuestion(Question question, List<string> Answers)
 {
     try
     {
         var questionCreated = adminService.CreateQuestion(question, Answers);
         if (questionCreated.Success == true)
         {
             return RedirectToAction("Questions");
         }
         else
         {
             ModelState.AddModelError("", questionCreated.Message);
             return View("CreateQuestion");
         }
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex.Message);
         return View(question);
     }
 }
 public RepositoryResult<bool> CreateQuestion(Question question)
 {
     try
     {
         var questionExists = db.Questions.FirstOrDefault(i => i.TestQuestion == question.TestQuestion);
         if (questionExists == null)
         {
             db.Questions.Add(question);
             db.SaveChanges();
             return RepositoryResult<bool>.SuccessFunc();
         }
         else
         {
             return RepositoryResult<bool>.ErrorFunc();
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
     }
     return null;
 }
        public RepositoryResult<bool> CheckAnswer(Question question)
        {
            if (question.CorrectAnswer == question.SelectedAnswer)
            {
                TempClass.NextQuestionNumber++;
                TempClass.TotallyAnsweredQuestions++;
                TempClass.GradeForAnswer += question.Grade;

                var user = db.Users.FirstOrDefault(i => i.UserName == question.UserName);
                user.Points += question.Grade;
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                Thread.Sleep(2000);
                return RepositoryResult<bool>.SuccessFunc();
            }
            else
            {
                TempClass.NextQuestionNumber = question.ID;
                Thread.Sleep(2000);
                return RepositoryResult<bool>.ErrorFunc();
            }
        }
        public TestResult GetTestResult(Question question)
        {
            TestResult testResult = new TestResult();
            testResult.CategoryName = question.TestName;
            testResult.Date = DateTime.Now;
            testResult.CollectedPoints = TempClass.GradeForAnswer;
            testResult.TestMaxPoints = db.Questions.Where(i => i.TestName == question.TestName).Sum(i => i.Grade);
            testResult.AnsweredQuestions = TempClass.TotallyAnsweredQuestions;

            return testResult;
        }
 public ActionResult IndexApplied(Question question)
 {
     var checkAnswer = questionService.CheckAnswer(question);
     ModelState.Clear();
     return RedirectToAction("Index", "Test", question);
 }
        public RepositoryResult<bool> EditQuestion(Question question, List<string> InputAnswers)
        {
            try
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();

                var getQuestion = db.QuestionAnswers;
                var getCurrentQuestionAnswers = getQuestion.Where(i => i.QuestionID == question.ID);

                int Counter = 0;
                foreach (var realAnswer in getCurrentQuestionAnswers)
                {
                    Answer RealAnswer = realAnswer.Answers;
                    RealAnswer.QuestionAnswer = InputAnswers[Counter];
                    Counter++;

                    db.Entry(RealAnswer).State = EntityState.Modified;
                }
                db.SaveChanges();

                return RepositoryResult<bool>.SuccessFunc();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return RepositoryResult<bool>.ErrorFunc();
            }
        }
 public ActionResult EditQuestion(Question question, List<string> InputAnswers)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var questionEdited = adminService.EditQuestion(question, InputAnswers);
             if (questionEdited.Success == true)
             {
                 return RedirectToAction("Questions");
             }
             else
             {
                 ModelState.AddModelError("", "Question can not be edited");
                 return View(question);
             }
         }
         else
         {
             return View(question);
         }
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex.Message);
         return View(question);
     }
 }