Ejemplo n.º 1
0
        public IActionResult Index(NewQuestionModel model)
        {
            var storyaddDto = new StoryAddDto()
            {
                Description = DateTime.Now.ToString(),
                Link        = model.StoryLink,
                Title       = model.StoryTitle,
            };

            _storyService.Add(storyaddDto);

            ViewData["message"] = "Sınav Başarıyla Oluşturuldu !";
            var lastindex = _storyService.LastIndex(storyaddDto);
            var question1 = new QuestionAddDto()
            {
                StoryId       = lastindex,
                CorrectOption = model.Question1.CorrectOption,
                OptionA       = model.Question1.OptionA,
                OptionB       = model.Question1.OptionB,
                OptionC       = model.Question1.OptionC,
                OptionD       = model.Question1.OptionD,
                Title         = model.Question1.Title
            };
            var question2 = new QuestionAddDto()
            {
                StoryId       = lastindex,
                CorrectOption = model.Question2.CorrectOption,
                OptionA       = model.Question2.OptionA,
                OptionB       = model.Question2.OptionB,
                OptionC       = model.Question2.OptionC,
                OptionD       = model.Question2.OptionD,
                Title         = model.Question2.Title
            };
            var question3 = new QuestionAddDto()
            {
                StoryId       = lastindex,
                CorrectOption = model.Question3.CorrectOption,
                OptionA       = model.Question3.OptionA,
                OptionB       = model.Question3.OptionB,
                OptionC       = model.Question3.OptionC,
                OptionD       = model.Question3.OptionD,
                Title         = model.Question3.Title
            };
            var question4 = new QuestionAddDto()
            {
                StoryId       = lastindex,
                CorrectOption = model.Question4.CorrectOption,
                OptionA       = model.Question4.OptionA,
                OptionB       = model.Question4.OptionB,
                OptionC       = model.Question4.OptionC,
                OptionD       = model.Question4.OptionD,
                Title         = model.Question4.Title
            };

            _questionService.Add(question1);
            _questionService.Add(question2);
            _questionService.Add(question3);
            _questionService.Add(question4);
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public IActionResult Index()
        {
            var model = new NewQuestionModel()
            {
                StoryTitles = LoadStories()
            };

            return(View(model));
        }
Ejemplo n.º 3
0
        public IActionResult New(NewQuestionModel newQuestionModel)
        {
            if (this.ModelState.IsValid)
            {
                int newQuestionId = this.questionsService.CreateNew(newQuestionModel.Title.Trim(), newQuestionModel.Content, newQuestionModel.Tags, DateTime.UtcNow, this.User.GetUserId());

                return(RedirectToAction(nameof(Details), new { id = newQuestionId }));
            }

            return(View(newQuestionModel));
        }
Ejemplo n.º 4
0
        public IActionResult CreateQuestion([FromBody] NewQuestionModel newQuestion)
        {
            if (HttpContext.Session.GetString("Username") == null ||
                HttpContext.Session.GetString("IsEditor") == "False" ||
                !ModelState.IsValid)
            {
                return(Json("401: Unauthorized"));
            }

            var model = _quizModelBuilder.CreateQuestion(newQuestion);

            return(Json(model));
        }
Ejemplo n.º 5
0
        public IActionResult Create(int id)
        {
            var category = _categoryImplementation.GetById(id);
            var model    = new NewQuestionModel
            {
                CategoryId       = category.CategoryId,
                CategoryTitle    = category.CategoryTitle,
                CategoryImageUrl = category.CategoryImageUrl,
                AuthorName       = User.Identity.Name
            };

            return(View(model));
        }
Ejemplo n.º 6
0
        private Question BuildPost(NewQuestionModel model, ApplicationUser user)
        {
            var category = _categoryImplementation.GetById(model.CategoryId);

            return(new Question
            {
                QuestionTitle = model.QuestionTitle,
                QuestionContent = model.QuestionContent,
                QuestionCreated = DateTime.Now,
                User = user,
                Category = category
            });
        }
Ejemplo n.º 7
0
        public async Task <bool> CreateQuestion(NewQuestionModel question)
        {
            var questionToInsert = new Data.Question
            {
                QuestionString = question.Question,
                CorrectAnswer  = new Answer
                {
                    AnswerText = question.CorrectAnswer
                },
                WrongAnswers = new WrongAnswers
                {
                    Answers = new List <Answer>()
                }
            };

            foreach (var wrongAnswer in question.WrongAnswers)
            {
                var wrongAnswerToAdd = new Answer
                {
                    AnswerText = wrongAnswer
                };

                questionToInsert.WrongAnswers.Answers.Add(wrongAnswerToAdd);
            }
            ;

            try
            {
                var quiz = await _quizManagerContext.Quizzes
                           .Include(a => a.Questions)
                           .ThenInclude(b => b.CorrectAnswer)
                           .Include(c => c.Questions)
                           .ThenInclude(d => d.WrongAnswers)
                           .ThenInclude(e => e.Answers)
                           .FirstOrDefaultAsync(x => x.Id == Convert.ToInt32(question.QuizId));

                quiz.Questions.Add(questionToInsert);
                await _quizManagerContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> AddQuestion(NewQuestionModel model)
        {
            var userId   = _userManager.GetUserId(User);
            var user     = _userManager.FindByIdAsync(userId).Result;
            var question = BuildPost(model, user);

            string[] censoredWords = System.IO.File.ReadAllLines(@"CensoredWords.txt");
            Censor   censor        = new Censor(censoredWords);

            question.QuestionTitle   = censor.CensorText(question.QuestionTitle);
            question.QuestionContent = censor.CensorText(question.QuestionContent);

            await _questionImplementation.AddQuestion(question);

            await _applicationUserImplementation.UpdateUserRating(userId, typeof(Question));

            return(RedirectToAction("Index", "Question", new { id = question.QuestionId }));
        }
Ejemplo n.º 9
0
 public bool CreateQuestion(NewQuestionModel newQuestion)
 {
     return(_quizService.CreateQuestion(newQuestion).GetAwaiter().GetResult());
 }
Ejemplo n.º 10
0
        public IActionResult AddQuestion([FromForm] NewQuestionModel question, [FromForm] string options)
        {
            var questionOptions = JsonConvert.DeserializeObject <List <NewOptionModel> >(options);

            if (question.QuestionImage == null || question.QuestionImage.Length == 0)
            {
                return(BadRequest(new { message = "Please select a valid question image" }));
            }

            // SETUP FOLDER
            var questionImagesFolderPath  = Path.Combine("Resources", "QuestionImages");
            var questionOptionsFolderPath = Path.Combine("Resources", "QuestionOptionImages");

            var questionImagesFolderFullPath  = Path.Combine(Directory.GetCurrentDirectory(), questionImagesFolderPath);
            var questionOptionsFolderFullPath = Path.Combine(Directory.GetCurrentDirectory(), questionOptionsFolderPath);

            if (!Directory.Exists(questionImagesFolderFullPath))
            {
                Directory.CreateDirectory(questionImagesFolderFullPath);
            }
            if (!Directory.Exists(questionOptionsFolderFullPath))
            {
                Directory.CreateDirectory(questionOptionsFolderFullPath);
            }

            // UPLOAD QUESTION IMAGE
            var uniqueQuestionImageFileName         = $"{Guid.NewGuid()}_question{Path.GetExtension(question.QuestionImage.FileName)}";
            var uniqueQuestionImageFillNameFullpath = Path.Combine(questionImagesFolderFullPath, uniqueQuestionImageFileName);

            try
            {
                using var fs = new FileStream(uniqueQuestionImageFillNameFullpath, FileMode.Create);
                question.QuestionImage.CopyTo(fs);
            }
            catch (Exception)
            {
                // Console.WriteLine(ex.Message);
                return(BadRequest(new { message = "Error while saving question image." }));
            }

            var questionOptionEntities = new List <Option>();
            var questionEntity         = new Question
            {
                Name       = question.Name,
                ImagePath  = Path.Combine(questionImagesFolderPath, uniqueQuestionImageFileName),
                Difficulty = question.Difficulty,
                Options    = questionOptionEntities
            };

            // UPLOAD QUESTION OPTION IMAGES
            foreach (var questionOption in questionOptions)
            {
                var questionOptionImageExtension = GetExtensionFromBase64File(questionOption.Image);

                var uniqueQuestionOptionImageFileName         = $"{Guid.NewGuid()}_option.{questionOptionImageExtension}";
                var uniqueQuestionOptionImageFileNameFullPath = Path.Combine(questionOptionsFolderFullPath, uniqueQuestionOptionImageFileName);

                try
                {
                    using var fs = new FileStream(uniqueQuestionOptionImageFileNameFullPath, FileMode.Create);
                    string imageAsBase64String = "";

                    if (questionOption.Image.Contains(","))
                    {
                        imageAsBase64String = questionOption.Image
                                              .Substring(questionOption.Image.IndexOf(",") + 1);
                    }

                    var imageAsBase64ByteArray = Convert.FromBase64String(imageAsBase64String);

                    fs.Write(imageAsBase64ByteArray, 0, imageAsBase64ByteArray.Length);
                }
                catch (Exception)
                {
                    // Console.WriteLine(ex.Message);
                    return(BadRequest(new { message = "Error while saving question option images." }));
                }

                questionOptionEntities.Add(new Option
                {
                    ImagePath = Path.Combine(questionOptionsFolderPath, uniqueQuestionOptionImageFileName),
                    IsAnswer  = !string.IsNullOrEmpty(questionOption.IsAnswer) && !questionOption.IsAnswer.Contains("false")
                });
            }

            _unitOfWork.Questions.Add(questionEntity);
            _unitOfWork.Complete();

            return(Ok(questionEntity));
        }