예제 #1
0
        public async Task <IActionResult> PutQuestion(int id, QuestionsDTO questionDto)
        {
            if (id != questionDto.Id)
            {
                return(BadRequest());
            }

            var question = MapFromDataModel(questionDto);

            _context.Entry(question).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QuestionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <QuestionsDTO> RetrieveQuestions(int totalQuestions)
        {
            var uriBuilder = new UriBuilder("https://opentdb.com/api.php");

            uriBuilder.Query = "amount=" + totalQuestions.ToString();

            var connectionString = uriBuilder.ToString();

            var client = new HttpClient();

            client.BaseAddress = new Uri(connectionString);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            var response = await client.GetAsync(client.BaseAddress);

            client.Dispose();

            if (response.IsSuccessStatusCode)
            {
                QuestionsDTO questions = await response.Content.ReadAsAsync <QuestionsDTO>();

                return(questions);
            }
            else
            {
                return(null);
            }
        }
        public static List <QuestionsModel> CreateAndShuffle(QuestionsDTO questions)
        {
            List <QuestionsModel> questionList = new List <QuestionsModel>();

            foreach (var question in questions.Results)
            {
                QuestionsModel appQuestion = new QuestionsModel();

                appQuestion.Id = question.Id;

                appQuestion.Question = question.Question;

                appQuestion.Answers.Add(question.CorrectAnswer);

                foreach (var answer in question.IncorrectAnswers)
                {
                    string appAnswer = answer;

                    appQuestion.Answers.Add(appAnswer);
                }

                appQuestion.Answers = appQuestion.Answers.OrderBy(x => Guid.NewGuid()).ToList();

                questionList.Add(appQuestion);
            }
            return(questionList);
        }
예제 #4
0
        public async Task <ActionResult <QuestionsDTO> > PostQuestion(QuestionsDTO questionDto)
        {
            _context.Questions.Add(MapFromDataModel(questionDto));
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetQuestion", new { id = questionDto.Id }, questionDto));
        }
예제 #5
0
 private Question MapFromDataModel(QuestionsDTO questionDto)
 {
     return(new Question
     {
         Id = questionDto.Id,
         CategoryId = questionDto.CategoryId,
         Statement = questionDto.Statement
     });
 }
예제 #6
0
        public static Questions ConvertToQustion(QuestionsDTO questionDTO)
        {
            Questions newQuestion = new Questions();

            newQuestion.OuestionTitle   = questionDTO.OuestionTitle;
            newQuestion.ProffestionId   = questionDTO.ProffestionId;
            newQuestion.QuestionContent = questionDTO.QuestionContent;
            newQuestion.QuestionId      = questionDTO.QuestionId;
            newQuestion.QuestionDate    = questionDTO.QuestionDate;
            newQuestion.QuestionView    = questionDTO.QuestionView;
            newQuestion.UserId          = questionDTO.UserId;
            return(newQuestion);
        }
예제 #7
0
        public static QuestionsDTO ConvertToDTO(Questions question)
        {
            QuestionsDTO newQuestion = new QuestionsDTO();

            newQuestion.OuestionTitle   = question.OuestionTitle;
            newQuestion.ProffestionId   = question.ProffestionId;
            newQuestion.QuestionContent = question.QuestionContent;
            newQuestion.QuestionId      = question.QuestionId;
            newQuestion.UserId          = question.UserId;
            newQuestion.QuestionDate    = question.QuestionDate;
            newQuestion.QuestionView    = question.QuestionView;
            return(newQuestion);
        }
예제 #8
0
        public void AddQuestion(QuestionsDTO questDTO)
        {
            Question quest = new Question
            {
                //     Id = questDTO.DTO_Id,
                // Id_Test = questDTO.Id_Test,
                //     QuestionText = questDTO.DTO_QuestionText,
                //  Answer_Id = questDTO.Answer_Id
            };

            Database.Questions.Create(quest);
            Database.Save();
        }
        public async Task <ActionResult> AddQustion(int?testId, string question, List <string> Answers, List <int?> checkBoxNum)
        {
            TestDTO                  TestInstance = new TestDTO(); // get that test
            QuestionsDTO             QU           = new QuestionsDTO();
            List <OptionalAnswerDTO> AnswerList   = new List <OptionalAnswerDTO>();

            int  count  = 0;
            bool isTrue = false;

            foreach (var answ in Answers)
            {
                count++;
                isTrue = false;

                foreach (var item in checkBoxNum)
                {
                    if (count == item.Value)
                    {
                        isTrue = true;
                    }
                }
                AnswerList.Add(new OptionalAnswerDTO()
                {
                    TrueAnswer   = isTrue,
                    OptionAnswer = answ,
                    QuestionObj  = QU
                });      //fill list of answers with the list from user
            }

            //foreach (var answ in Answers)
            //{
            //    AnswerList.Add(new OptionalAnswerDTO()
            //    {
            //        OptionAnswer = answ,
            //        QuestionObj = QU
            //    });      //fill list of answers with the list from user
            //}

            QU.QuestionText    = question;
            QU.OptionalAnswers = AnswerList;
            QU.TestObj         = TestInstance;
            TestInstance.Questions.Add(QU);
            TestInstance.Id = testId.Value;


            testService.UpdateTest(TestInstance);

            // return View(PassTheTest(testId.Value));
            return(AddQustion(testId));
        }
예제 #10
0
        public async Task Update(QuestionsDTO item)
        {
            var question = await db.Questions.FindAsync(item.Id);

            if (question != null)
            {
                question.Question        = item.Question;
                question.AnswerType      = item.AnswerType;
                question.AddWhyName      = item.AddWhyName;
                question.AddWhy          = item.AddWhy;
                db.Entry(question).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                await db.SaveChangesAsync();
            }
        }
예제 #11
0
        public async Task Add(QuestionsDTO item)
        {
            var question = new Questions
            {
                Id         = item.Id,
                Question   = item.Question,
                AnswerType = item.AnswerType,
                AddWhyName = item.AddWhyName,
                AddWhy     = item.AddWhy
            };

            db.Questions.Add(question);
            await db.SaveChangesAsync();
        }
예제 #12
0
 //public static AnswerDTO GetPostAnswer(AnswerDTO answerdto)
 //{
 //    try
 //    {
 //        using (TeacherForStudentEntities db = new TeacherForStudentEntities())
 //        {
 //            db.Answers.Add(Convertion.AnswerConvertion.ConvertToAnswer(answerdto));
 //            db.SaveChanges();
 //            return Convertion.AnswerConvertion.ConvertToDTO(Convertion.AnswerConvertion.ConvertToAnswer(answerdto));
 //        }
 //    }
 //    catch (Exception)
 //    {
 //        return null;
 //    }
 //}
 public static QuestionsDTO GetPostQuestion(QuestionsDTO questiondto)
 {
     try
     {
         using (TeacherForStudentEntities db = new TeacherForStudentEntities())
         {
             db.Questions.Add(Convertion.QuestionConvertion.ConvertToQustion(questiondto));
             db.SaveChanges();
             return(Convertion.QuestionConvertion.ConvertToDTO(Convertion.QuestionConvertion.ConvertToQustion(questiondto)));
         }
     }
     catch (Exception)
     {
         return(null);
     }
 }
예제 #13
0
        public async Task <IActionResult> AddOrUpdate(QuestionsDTO item)
        {
            setViewData();
            if (ModelState.IsValid)
            {
                if (item.Id > 0)
                {
                    await _repo.Update(item);
                }
                else
                {
                    await _repo.Add(item);
                }

                return(PartialView("_Questions", await _repo.GetList()));
            }
            return(Content("Wypełnij wszystkie wymagane pola"));
        }
        public async Task <QuestionsDTO> AddQuestions(QuestionsDTO questions)
        {
            foreach (var question in questions.Results)
            {
                Question dbQuestion = new Question()
                {
                    Category      = question.Category,
                    Difficulty    = question.Difficulty,
                    Question1     = question.Question,
                    CorrectAnswer = question.CorrectAnswer
                };

                if (question.Type.Equals("boolean"))
                {
                    dbQuestion.Type = false;
                }
                else
                {
                    dbQuestion.Type = true;
                }

                if (question.IncorrectAnswers.Count > 1)
                {
                    dbQuestion.IncorrectAnswer1 = question.IncorrectAnswers[0];
                    dbQuestion.IncorrectAnswer2 = question.IncorrectAnswers[1];
                    dbQuestion.IncorrectAnswer3 = question.IncorrectAnswers[2];
                }
                else
                {
                    dbQuestion.IncorrectAnswer1 = question.IncorrectAnswers[0];
                }
                await _context.AddAsync(dbQuestion);
            }

            _context.SaveChanges();

            foreach (var question in questions.Results)
            {
                question.Id = _context.Questions.Where(x => x.Question1.Equals(question.Question)).Select(x => x.Id).First();
            }

            return(questions);
        }
        [HttpPost]      //add test
        public ActionResult Add(string TestName, string question, List <string> Answers, List <int?> checkBoxNum)
        {
            TestDTO                  TD         = new TestDTO();
            QuestionsDTO             QU         = new QuestionsDTO();
            List <OptionalAnswerDTO> AnswerList = new List <OptionalAnswerDTO>();

            int  count  = 0;
            bool isTrue = false;

            foreach (var answ in Answers)
            {
                count++;
                isTrue = false;

                foreach (var item in checkBoxNum)
                {
                    if (count == item.Value)
                    {
                        isTrue = true;
                    }
                }
                AnswerList.Add(new OptionalAnswerDTO()
                {
                    TrueAnswer   = isTrue,
                    OptionAnswer = answ,
                    QuestionObj  = QU
                });      //fill list of answers with the list from user
            }

            QU.QuestionText    = question;
            QU.OptionalAnswers = AnswerList;
            QU.TestObj         = TD;

            TD.Questions.Add(QU);
            TD.Description = TestName;

            testService.CreateTest(TD);


            //return PassTheTest(TD.Id);
            return(View());
        }
        public void AddQuestion(QuestionsDTO question)
        {
            var mappedQuestion = _mapper.Map <QuestionsDTO, Questions>(question);

            _repository.Add(mappedQuestion);
        }
예제 #17
0
 public void EditQUestion(QuestionsDTO questDTO)
 {
     throw new NotImplementedException();
 }
예제 #18
0
 public void RemoveQuestion(QuestionsDTO questDTO)
 {
     Database.Questions.Delete(questDTO.Id);
     Database.Save();
 }