コード例 #1
0
        public async Task TestCopyQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            InitContext(context);
            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            context.AddPrincipalMock(question.User.UserId, Roles.User);

            IQuestionService questionService = context.GetService <QuestionService>();

            context.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = context.GetService <VersionService>();

            //Act
            int newId = await versionService.CopyQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);
            QuestionDto newQuestion = await questionService.GetQuestionAsync(newId);

            Assert.Equal(question.Options.Count, newQuestion.Options.Count);
            Assert.Equal(question.QuestionType, newQuestion.QuestionType);
            for (int i = 0; i < question.Options.Count; i++)
            {
                QuestionOption    optionSource      = question.Options[i];
                QuestionOptionDto optionDestination = newQuestion.Options[i];

                Assert.NotEqual(optionSource.Id, optionDestination.Id);
                Assert.True(optionDestination.Id > 0);
            }
        }
コード例 #2
0
        public async Task <QuestionOptionDto> Insert(QuestionOptionDto request)
        {
            var entity = AutoMapper.Mapper.Map <QuestionOption>(request);

            var result = await entityRepository.InsertAsync(entity);

            return(AutoMapper.Mapper.Map <QuestionOptionDto>(result));
        }
コード例 #3
0
        public async Task <QuestionOptionDto> Update(QuestionOptionDto request)
        {
            var entity = AutoMapper.Mapper.Map <QuestionOption>(request);

            request.IsDeleted = true;

            var result = await entityRepository.UpdateAsync(entity);

            return(AutoMapper.Mapper.Map <QuestionOptionDto>(result));
        }
コード例 #4
0
        public async Task <List <QuestionOptionDto> > GetQuestion(int categoryId)
        {
            if (categoryId <= 0)
            {
                throw new ArgumentNullException(nameof(categoryId));
            }

            var questions = await _questionRepository.GetQuestionsByCaregoryId(categoryId);

            if (questions == null)
            {
                throw new NullReferenceException(nameof(questions));
            }

            var questionOptions = new List <QuestionOptionDto>();

            foreach (var qtn in questions)
            {
                var questionOption = new QuestionOptionDto();

                questionOption.Question = qtn;

                questionOption.Options = new List <IOption>();

                var options = await _optionRepository.GetOptionsByQuestionId(qtn.QuestionId);

                if (options == null)
                {
                    throw new NullReferenceException(nameof(options));
                }

                foreach (var item in options)
                {
                    questionOption.Options.Add(item);
                }
                questionOptions.Add(questionOption);
            }

            return(questionOptions);
        }
コード例 #5
0
 protected virtual void CopyData(object dto, CranEntity entity)
 {
     if (dto is QuestionOptionDto && entity is QuestionOption)
     {
         QuestionOptionDto dtoSource         = (QuestionOptionDto)dto;
         QuestionOption    entityDestination = (QuestionOption)entity;
         entityDestination.IsTrue     = dtoSource.IsTrue;
         entityDestination.Text       = dtoSource.Text ?? string.Empty;
         entityDestination.IdQuestion = dtoSource.IdQuestion;
     }
     else if (dto is QuestionDto && entity is Question)
     {
         QuestionDto dtoSource         = (QuestionDto )dto;
         Question    entityDestination = (Question)entity;
         entityDestination.Title        = dtoSource.Title;
         entityDestination.Text         = dtoSource.Text ?? string.Empty;
         entityDestination.Explanation  = dtoSource.Explanation;
         entityDestination.QuestionType = dtoSource.QuestionType;
         entityDestination.Language     = Enum.Parse <Language>(dtoSource.Language);
     }
     else if (dto is RelQuestionTagDto && entity is RelQuestionTag)
     {
         RelQuestionTagDto dtoSource         = (RelQuestionTagDto)dto;
         RelQuestionTag    entityDestination = (RelQuestionTag)entity;
         entityDestination.IdQuestion = dtoSource.IdQuestion;
         entityDestination.IdTag      = dtoSource.IdTag;
     }
     else if (dto is RelQuestionImageDto && entity is RelQuestionImage)
     {
         RelQuestionImageDto dtoSource         = (RelQuestionImageDto)dto;
         RelQuestionImage    entityDestination = (RelQuestionImage)entity;
         entityDestination.IdQuestion = dtoSource.IdQuestion;
         entityDestination.IdImage    = dtoSource.IdImage;
     }
     else if (dto is ImageDto && entity is Image)
     {
         ImageDto dtoSource         = (ImageDto)dto;
         Image    entityDestination = (Image)entity;
         entityDestination.Width  = dtoSource.Width;
         entityDestination.Height = dtoSource.Height;
         entityDestination.Full   = dtoSource.Full;
     }
     else if (dto is CourseDto && entity is Course)
     {
         CourseDto dtoSource         = (CourseDto)dto;
         Course    entityDestination = (Course)entity;
         entityDestination.Title             = dtoSource.Title;
         entityDestination.Language          = Enum.Parse <Language>(dtoSource.Language);
         entityDestination.NumQuestionsToAsk = dtoSource.NumQuestionsToAsk;
         entityDestination.Description       = dtoSource.Description;
     }
     else if (dto is RelCourseTagDto && entity is RelCourseTag)
     {
         RelCourseTagDto dtoSource         = (RelCourseTagDto)dto;
         RelCourseTag    entityDestination = (RelCourseTag)entity;
         entityDestination.IdCourse = dtoSource.IdCourse;
         entityDestination.IdTag    = dtoSource.IdTag;
     }
     else
     {
         throw new NotImplementedException();
     }
 }
コード例 #6
0
 private void CopyDataQuestionOption(QuestionOptionDto dto, QuestionOption entity)
 {
     entity.IsTrue     = dto.IsTrue;
     entity.Text       = dto.Text ?? string.Empty;
     entity.IdQuestion = dto.IdQuestion;
 }