示例#1
0
        public MappersTests()
        {
            var userService = new Mock <IUserService>();

            _questionMapper = new QuestionMapper(userService.Object);
            _answerMapper   = new AnswerMapper(userService.Object);
        }
示例#2
0
        public async Task CreateQuestionAsync(QuestionDto questionDto)
        {
            var question = QuestionMapper.ToEntity(questionDto);

            question.QuestionId = Guid.Empty;
            await _questionRepo.CreateQuestionAsync(question);
        }
示例#3
0
        public async Task <QuestionDto> CreateQuestionAsync(QuestionDto question)
        {
            var dbQuestion      = QuestionMapper.ToDb(question);
            var createdQuestion = await _questionRepo.CreateQuestionAsync(dbQuestion);

            return(QuestionMapper.ToDto(createdQuestion));
        }
        public IActionResult SaveQuestion(QuestionModel questionModel)
        {
            if (ModelState.IsValid == false)
            {
                StringBuilder sb = new StringBuilder();

                foreach (var value in ModelState.Values)
                {
                    foreach (var error in value.Errors)
                    {
                        sb.AppendLine(error.ErrorMessage);
                    }
                }

                return(Content(sb.ToString()));
            }

            QuestionMapper questionMapper = new QuestionMapper();
            Question       question       = questionMapper.Map(questionModel);

            question.Creator = CurrentUser;

            if (question.ID != 0)
            {
                DB.QuestionRepository.Update(question);
                TempData["Message"] = UiMessages.SuccesMessage;
            }
            else
            {
                DB.QuestionRepository.Add(question);
                TempData["Message"] = UiMessages.SuccesMessage;
            }
            return(RedirectToAction("Index"));
        }
示例#5
0
        /// <summary>
        /// Add new Question
        /// </summary>
        /// <param name="questionDTO"></param>
        /// <returns></returns>
        public QuestionDTO Add(QuestionDTO questionDTO)
        {
            try
            {
                Question question = QuestionMapper.ToDB(questionDTO);
                question.Tags = new List <Tag>();
                foreach (var tag in questionDTO.Tags)
                {
                    tag.Id = Guid.NewGuid();
                    var currentTag = db.Tags.Where(x => x.Body == tag.Body).FirstOrDefault();
                    if (currentTag == null)
                    {
                        var tagsaved = db.Tags.Add(TagMapper.ToDB(tag));
                        question.Tags.Add(tagsaved);
                    }
                    else
                    {
                        question.Tags.Add(currentTag);
                    }
                }

                var questionAdded = db.Questions.Add(question);
                db.SaveChanges();
                var questionFound = db.Questions
                                    .Include("Author")
                                    .Where(s => s.Id == questionAdded.Id)
                                    .FirstOrDefault();

                return(QuestionMapper.ToDTO(questionFound));
            }
            catch
            {
                return(null);
            }
        }
示例#6
0
 public HttpResponseMessage Edit([FromBody] Question questionToEdit, [FromUri] Guid Id)
 {
     try
     {
         if (questionLogic.Find(Id).Author.Id != CurrentUserId())
         {
             throw new Unauthorised();
         }
         questionToEdit.Id = Id;
         QuestionDTO questionEdit = QuestionMapper.ToDTO(questionToEdit);
         questionEdit.Tags = SetTags(questionToEdit.Tags);
         QuestionDTO question = questionLogic.Edit(questionEdit);
         if (question == null)
         {
             throw new NoSuchQuestionFound();
         }
         HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new { question });
         return(response);
     }
     catch (Unauthorised e)
     {
         HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Forbidden, new { error = e.Message });
         return(response);
     }
     catch (NoSuchQuestionFound e)
     {
         HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, new { error = e.Message });
         return(response);
     }
     catch (Exception e)
     {
         HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, e.Message);
         return(response);
     }
 }
示例#7
0
        /// <summary>
        /// Return all questions (page||count)
        /// </summary>
        /// <param name="page"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List <QuestionDTO> All(int?page, int?count, string searchString)
        {
            try
            {
                List <QuestionDTO> questions = new List <QuestionDTO>();
                var takePage    = page ?? 1;
                var takeCount   = count ?? PageConstants.DefaultPageRecordCount;
                var questionsDB = db.Questions
                                  .Include("Author")
                                  .Where(s => (searchString == null ? true : s.Title.Contains(searchString)))
                                  .OrderByDescending(x => x.UploadDate)
                                  .Skip((takePage - 1) * takeCount)
                                  .Take(takeCount)
                                  .ToList();

                foreach (var ques in questionsDB)
                {
                    QuestionDTO questionDTO = QuestionMapper.ToDTO(ques);
                    questionDTO.TotalAnswers = answerData.Count(ques.Id);
                    questions.Add(questionDTO);
                }

                return(questions);
            }
            catch
            {
                return(null);
            }
        }
示例#8
0
        public async Task <IEnumerable <QuestionDto> > GetQuestionsAsync()
        {
            var dbQuestions = await _questionRepo.GetQuestionsAsync();

            var questions = dbQuestions.Select(q => QuestionMapper.ToDto(q));

            return(questions);
        }
示例#9
0
 public ActionResult Ask(QuestionViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         Service.Ask(QuestionMapper.Map(viewModel));
     }
     return(GetUserQuestions());
 }
示例#10
0
        public async Task <IEnumerable <QuestionDto> > GetAllQuestionsAsync()
        {
            var questions = await _questionRepo.GetAllQuestionsAsync();

            var questionDtos = new List <QuestionDto>();

            foreach (var question in questions)
            {
                questionDtos.Add(QuestionMapper.ToDto(question));
            }

            return(questionDtos);
        }
示例#11
0
        public static List <QuestionModel> GetQuestions()
        {
            List <Question>      questions      = Kernel.DB.QuestionRepository.Get();
            List <QuestionModel> questionModels = new List <QuestionModel>();
            QuestionMapper       questionMapper = new QuestionMapper();

            foreach (var question in questions)
            {
                QuestionModel questionModel = questionMapper.Map(question);
                questionModels.Add(questionModel);
            }

            return(questionModels);
        }
示例#12
0
        public static IReadOnlyList <QuestionBase> GetQuestions(short poolId)
        {
            const string spName = "GetQuestions";

            using (var conn = new SqlConnection(cs))
            {
                conn.Open();
                using (var r = conn.ExecuteReader(spName, new { PoolID = poolId }, commandType: CommandType.StoredProcedure))
                {
                    var mapper = new QuestionMapper();
                    var result = mapper.Map(r);
                    return(result.ToList());
                }
            }
        }
示例#13
0
        public static TagDTO ToDTO(Tag tag)
        {
            List <QuestionDTO> question = new List <QuestionDTO>();

            foreach (var ques in tag.Questions)
            {
                question.Add(QuestionMapper.ToDTO(ques));
            }
            TagDTO tagDTO = new TagDTO()
            {
                Id        = tag.Id,
                Body      = tag.Body,
                Questions = question
            };

            return(tagDTO);
        }
示例#14
0
        public static Tag ToViewModel(TagDTO tagDTO)
        {
            List <Question> question = new List <Question>();

            foreach (var ques in tagDTO.Questions)
            {
                question.Add(QuestionMapper.ToViewModel(ques));
            }
            Tag tag = new Tag()
            {
                Id        = tagDTO.Id,
                Body      = tagDTO.Body,
                Questions = question
            };

            return(tag);
        }
示例#15
0
 /// <summary>
 /// Edit question
 /// </summary>
 /// <param name="questionDTO"></param>
 /// <returns></returns>
 public QuestionDTO Edit(QuestionDTO questionDTO)
 {
     try
     {
         QuestionDTO questionUpdated = new QuestionDTO();
         var         questionFound   = db.Questions.Where(x => x.Id == questionDTO.Id).FirstOrDefault();
         if (questionFound == null)
         {
             throw new NoSuchQuestionFound();
         }
         questionFound.Description = questionDTO.Description;
         questionFound.Title       = questionDTO.Title;
         List <Tag> tags = new List <Tag>();
         foreach (var tag in questionDTO.Tags)
         {
             tag.Id = Guid.NewGuid();
             var currentTag = db.Tags.Where(x => x.Body == tag.Body).FirstOrDefault();
             if (currentTag == null)
             {
                 var tagsaved = db.Tags.Add(TagMapper.ToDB(tag));
                 tags.Add(tagsaved);
             }
             else
             {
                 tags.Add(currentTag);
             }
         }
         questionFound.Tags     = tags;
         questionFound.Image    = questionDTO.Image;
         questionFound.EditDate = DateTime.Now;
         questionUpdated        = QuestionMapper.ToDTO(questionFound);
         db.SaveChanges();
         return(questionUpdated);
     }
     catch (NoSuchQuestionFound)
     {
         throw new NoSuchQuestionFound();
     }
     catch
     {
         return(null);
     }
 }
        public IActionResult Index()
        {
            ViewBag.Message = TempData["Message"];

            List <Question> questions = DB.QuestionRepository.Get();

            QuestionViewModel questionViewModel = new QuestionViewModel();

            QuestionMapper questionMapper = new QuestionMapper();

            foreach (var question in questions)
            {
                var questionModel = questionMapper.Map(question);
                questionViewModel.Questions.Add(questionModel);
            }

            EnumerationUtil.Enumerate(questionViewModel.Questions);

            return(View(questionViewModel));
        }
        public override void Execute(object parameter)
        {
            if (viewModel.CurrentSituation == (int)Situation.NORMAL)
            {
                viewModel.CurrentSituation = (int)Situation.ADD;
            }
            else if (viewModel.CurrentSituation == (int)Situation.SELECTED)
            {
                viewModel.CurrentSituation = (int)Situation.EDIT;
            }
            else
            {
                if (viewModel.CurrentSituation == (int)Situation.ADD || viewModel.CurrentSituation == (int)Situation.EDIT)
                {
                    QuestionMapper mapper   = new QuestionMapper();
                    Question       question = mapper.Map(viewModel.CurrentQuestion);

                    question.Creator = Kernel.CurrentUser;
                    if (viewModel.CurrentQuestion.No == 0)
                    {
                        viewModel.CurrentQuestion.ID = DB.QuestionRepository.Add(question);
                        viewModel.CurrentQuestion.No = viewModel.Questions.Count + 1;
                        viewModel.Questions.Add(viewModel.CurrentQuestion);
                    }
                    else
                    {
                        DB.QuestionRepository.Update(question);
                        viewModel.Questions[viewModel.CurrentQuestion.No - 1] = viewModel.CurrentQuestion;
                    }

                    viewModel.SelectedQuestion = null;
                    viewModel.CurrentQuestion  = new QuestionModel();

                    viewModel.CurrentSituation = (int)Situation.NORMAL;
                }
                else
                {
                    //MessageBox.Show(UIMessages.ValidationCommonMessage, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
示例#18
0
 /// <summary>
 /// Find Question by Id
 /// </summary>
 /// <param name="QuestionId"></param>
 /// <returns></returns>
 public QuestionDTO Find(Guid QuestionId)
 {
     try
     {
         Question question = db.Questions.Include("Author").Where(x => x.Id == QuestionId).FirstOrDefault();
         if (question == null)
         {
             throw new NoSuchQuestionFound();
         }
         QuestionDTO questionDTO = QuestionMapper.ToDTO(question);
         return(questionDTO);
     }
     catch (NoSuchQuestionFound)
     {
         throw new NoSuchQuestionFound();
     }
     catch
     {
         return(null);
     }
 }
示例#19
0
        public HttpResponseMessage Add(Question questionToAdd)
        {
            try
            {
                if (questionToAdd != null && ModelState.IsValid)
                {
                    QuestionDTO questionDTO = QuestionMapper.ToDTO(questionToAdd);
                    questionDTO.Tags = SetTags(questionToAdd.Tags);
                    var     email  = CurrentEmail();
                    UserDTO author = userLogic.Find(email);
                    questionDTO.Author = author;
                    var question = questionLogic.Add(questionDTO);
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, question);
                    return(response);
                }
                else
                {
                    var validationErrors = new List <string>();
                    foreach (var state in ModelState)
                    {
                        foreach (var error in state.Value.Errors)
                        {
                            validationErrors.Add((error.ErrorMessage));
                        }
                    }

                    var jsonerrors = JsonConvert.SerializeObject(new
                    {
                        errors = validationErrors
                    });
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, JsonConvert.DeserializeObject(jsonerrors)));
                }
            }
            catch (Exception e)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, e.Message);
                return(response);
            }
        }
示例#20
0
        public void Test_QuestionDto_To_Question()
        {
            var questionDto = new QuestionDto
            {
                Id          = 1,
                Title       = "Question Title",
                Text        = "Question Text",
                CreatedDate = DateTime.UtcNow,
                UpdatedDate = DateTime.UtcNow,
                UserId      = 1
            };

            var question = QuestionMapper.ToDb(questionDto);

            Assert.NotNull(question);
            Assert.Equal(questionDto.Id, question.Id);
            Assert.Equal(questionDto.Title, question.Title);
            Assert.Equal(questionDto.Text, question.Text);
            Assert.Equal(questionDto.CreatedDate, question.CreatedDate);
            Assert.Equal(questionDto.UpdatedDate, question.UpdatedDate);
            Assert.Equal(questionDto.UserId, question.UserId);
        }
示例#21
0
        public void MapQuestionToEntity()
        {
            //Arrange
            var questionDto = new QuestionDto()
            {
                QuestionId   = Guid.NewGuid(),
                QuestionText = "This is the best thing every",
                CreatedDate  = DateTime.UtcNow,
                ModifiedDate = DateTime.UtcNow,
                UserId       = Guid.NewGuid()
            };

            //Act
            var questionEntity = QuestionMapper.ToEntity(questionDto);

            //Assert
            Assert.Equal(questionDto.QuestionId, questionEntity.QuestionId);
            Assert.Equal(questionDto.QuestionText, questionEntity.QuestionText);
            Assert.Equal(questionDto.CreatedDate, questionEntity.CreatedDate);
            Assert.Equal(questionDto.ModifiedDate, questionEntity.ModifiedDate);
            Assert.Equal(questionDto.UserId, questionEntity.UserId);
        }
示例#22
0
        /// <summary>
        /// Find tag by Id
        /// </summary>
        /// <param name="TagId"></param>
        /// <returns></returns>
        public TagDTO Find(Guid TagId)
        {
            try
            {
                var dbTags = db.Tags.Include("Questions").Where(x => x.Id == TagId).FirstOrDefault();

                TagDTO tag = TagMapper.ToDTO(dbTags);
                foreach (var ques in dbTags.Questions)
                {
                    tag.Questions.Add(QuestionMapper.ToDTO(ques));
                }

                return(tag);
            }
            catch (NoSuchTagFound)
            {
                throw new NoSuchTagFound();
            }
            catch
            {
                return(null);
            }
        }
示例#23
0
 /// <summary>
 /// Delete question by Id
 /// </summary>
 /// <param name="QuestionId"></param>
 /// <returns></returns>
 public QuestionDTO Delete(Guid QuestionId)
 {
     try
     {
         Question question = db.Questions.Where(x => x.Id == QuestionId).FirstOrDefault();
         if (question == null)
         {
             throw new NoSuchQuestionFound();
         }
         QuestionDTO questionDTO = QuestionMapper.ToDTO(question);
         db.Questions.Remove(question);
         db.SaveChanges();
         return(questionDTO);
     }
     catch (NoSuchQuestionFound)
     {
         throw new NoSuchQuestionFound();
     }
     catch
     {
         return(null);
     }
 }
示例#24
0
        public override void Execute(object parameter)
        {
            SureDialogViewModel sureDialogViewModel = new SureDialogViewModel();

            sureDialogViewModel.DialogText = UIMessages.DeleteSureMessage;

            SureDialog sureDialog = new SureDialog();

            sureDialog.DataContext = sureDialogViewModel;
            sureDialog.ShowDialog();

            if (sureDialog.DialogResult == true)
            {
                QuestionMapper questionMapper = new QuestionMapper();

                Question question = questionMapper.Map(viewModel.CurrentQuestion);
                question.IsDeleted = true;
                question.Creator   = Kernel.CurrentUser;

                DB.QuestionRepository.Update(question);

                int no = viewModel.SelectedQuestion.No;

                viewModel.CurrentSituation = (int)Situation.NORMAL;
                viewModel.Questions.Remove(viewModel.SelectedQuestion);

                List <QuestionModel> modelList = viewModel.Questions.ToList();

                EnumerationUtil.Enumerate(modelList, no - 1);
                viewModel.AllQuestions = modelList;

                viewModel.UpdateDataFiltered();

                viewModel.SelectedQuestion = null;
                viewModel.CurrentQuestion  = new QuestionModel();
            }
        }
        public IActionResult SaveQuestion(int id)
        {
            QuestionModel questionModel = new QuestionModel();

            if (id != 0)
            {
                Question question = DB.QuestionRepository.FindByID(id);
                if (question == null)
                {
                    return(Content("Sual Tapılmadı"));
                }
                QuestionMapper questionMapper = new QuestionMapper();
                questionModel = questionMapper.Map(question);
            }

            List <Subject>        subjects      = DB.SubjectRepository.Get();
            List <SelectListItem> subjectModels = new List <SelectListItem>();

            foreach (var subject in subjects)
            {
                subjectModels.Add(new SelectListItem(subject.Name, subject.ID.ToString()));
            }

            List <Exam1>          exams      = DB.ExamRepository.Get();
            List <SelectListItem> examModels = new List <SelectListItem>();

            foreach (var exam in exams)
            {
                examModels.Add(new SelectListItem(exam.ExamType, exam.ID.ToString()));
            }

            questionModel.Subjects = subjectModels;
            questionModel.Exams    = examModels;

            return(View(questionModel));
        }
        public List <Question> Obtenha()
        {
            var map = new QuestionMapper();

            return(map.Obtenha());
        }
示例#27
0
        public async Task <QuestionDto> GetQuestionAsync(int id)
        {
            var dbQuestion = await _questionRepo.GetQuestionAsync(id);

            return(QuestionMapper.ToDto(dbQuestion));
        }
 public QuestionQueryHandler(ISession session, IUserService userService)
 {
     _session        = session;
     _questionMapper = new QuestionMapper(userService);
 }
示例#29
0
        public void MapQuestionToDtoWithNull()
        {
            var questionDto = QuestionMapper.ToDto(null);

            Assert.Null(questionDto);
        }
示例#30
0
        public ActionResult GetQuestion(int questionId)
        {
            var question = Service.GetQuestion(questionId);

            return(View("RenderTemplates/QuestionView", QuestionMapper.Map(question)));
        }