Exemplo n.º 1
0
        public async Task TestController_CreateQuestion() { 
            using(YuYanDBContext db = new YuYanDBContext())
            using (YuYanDBRepository repos = new YuYanDBRepository(db)) {
                YuYanService svc = new YuYanService(repos);
                var controller = new SurveyController(svc);

                dtoSurveyQuestion questionObj = new dtoSurveyQuestion();
                questionObj.Question = "Add from Test";
                questionObj.QuestionType = QuestionType.checkbox;
                //questionObj.SurveyId = 6;

                var result = await controller.CreateQuestion(6, questionObj);
                Assert.IsNotNull(result);
            }
        }
Exemplo n.º 2
0
        public async Task<tbSurveyQuestion> UpdateQuestion(dtoSurveyQuestion question)
        {
            tbSurveyQuestion theQuestion = null;

            try
            {
                theQuestion = await _db.tbSurveyQuestions.FirstOrDefaultAsync(x => x.QuestionId == question.QuestionId
                    && (x.IsActive ?? true) && !(x.IsDeleted ?? false));

                if (theQuestion != null)
                {
                    theQuestion.Question = question.Question;
                    theQuestion.QuestionType = question.QuestionType;
                    theQuestion.UpdatedDate = DateTime.UtcNow;

                    if (theQuestion.tbSurveyQuestionItems != null)
                    {
                        if (theQuestion.tbSurveyQuestionItems.Count() > 0)
                        {
                            IList<int> deleteItemIds = theQuestion.tbSurveyQuestionItems
                                .Where(x => (x.IsActive ?? true) && !(x.IsDeleted ?? false))
                                .Select(x => x.QuestionItemId)
                                .Except(question.dtoItems.Select(z => z.QuestionItemId)).ToList();
                            foreach (int dItemId in deleteItemIds)
                            {
                                await DeleteItem(dItemId);
                                await DeactiveItem(dItemId);
                            }
                        }
                    }

                    if (question.dtoItems != null)
                    {
                        if (question.dtoItems.Count() > 0)
                        {
                            foreach (dtoSurveyQuestionItem item in question.dtoItems)
                            {
                                if (item.QuestionItemId != 0)
                                {
                                    //update
                                    await UpdateItem(item);
                                }
                                else
                                {
                                    //add new item
                                    await CreateNewItem(item);
                                }
                            }
                        }
                    }

                    await _db.SaveChangesAsync();
                }
            }
            catch (DataException dex)
            {
                throw new ApplicationException("Data error!", dex);
            }

            return theQuestion;
        }
Exemplo n.º 3
0
        public async Task<tbSurveyQuestion> CreateNewQuestion(dtoSurveyQuestion question)
        {
            tbSurveyQuestion newQuestion = new tbSurveyQuestion();

            try
            {
                var questionCount = _db.tbSurveyQuestions.Where(x => x.SurveyId == question.SurveyId
                    && (x.IsActive ?? true) && !(x.IsDeleted ?? false)).Count();
                newQuestion.SurveyId = question.SurveyId;
                newQuestion.Question = question.Question;
                newQuestion.QuestionType = question.QuestionType;
                newQuestion.QuestionOrder = questionCount + 1;
                newQuestion.CreatedDate = DateTime.UtcNow;
                newQuestion.UpdatedDate = DateTime.UtcNow;
                newQuestion.IsActive = true;
                newQuestion.IsDeleted = false;

                _db.tbSurveyQuestions.Add(newQuestion);

                if (question.dtoItems != null)
                {
                    if (question.dtoItems.Count() > 0)
                    {
                        foreach (dtoSurveyQuestionItem item in question.dtoItems)
                        {
                            item.QuestionId = newQuestion.QuestionId;
                            await CreateNewItem(item);
                        }
                    }
                }

                await _db.SaveChangesAsync();
            }
            catch (DataException dex)
            {
                throw new ApplicationException("Data error!", dex);
            }

            return newQuestion;
        }
Exemplo n.º 4
0
        public async Task<IHttpActionResult> UpdateQuestion(int surveyId, int questionId, [FromBody] dtoSurveyQuestion question)
        {
            dtoSurveyQuestion dtoSurveyQuestion = new dtoSurveyQuestion();

            try
            {
                question.SurveyId = surveyId;
                dtoSurveyQuestion = await _yuyanSvc.UpdateSurveyQuestion(question);
            }
            catch (ApplicationException aex)
            {
                return BadRequest(aex.Message);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

            return Ok(dtoSurveyQuestion);
        }
Exemplo n.º 5
0
        public async Task<IHttpActionResult> GetQuestionByQuestionId(int surveyId, int questionId)
        {
            dtoSurveyQuestion question = new dtoSurveyQuestion();

            try
            {
                question = await _yuyanSvc.GetSurveyQuestionByQuestionId(questionId);
            }
            catch (ApplicationException aex)
            {
                return BadRequest(aex.Message);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

            return Ok(question);
        }
Exemplo n.º 6
0
        public static dtoSurveyQuestion ConvertToDtoSurveyQuestion(this tbSurveyQuestion source, dtoSurveyQuestion data = null)
        {

            if (data == null)
                data = new dtoSurveyQuestion();

            if (source == null)
                return null;

            data.QuestionId = source.QuestionId;
            data.SurveyId = source.SurveyId;
            data.Question = source.Question;
            data.QuestionOrder = source.QuestionOrder;
            data.QuestionType = source.QuestionType;
            data.IsActive = source.IsActive;
            data.IsDeleted = source.IsDeleted;

            IList<dtoSurveyQuestionItem> itemList = new List<dtoSurveyQuestionItem>();
            if (source.tbSurveyQuestionItems != null)
            {
                foreach (tbSurveyQuestionItem item in source.tbSurveyQuestionItems)
                {
                    if ((item.IsActive ?? true) && !(item.IsDeleted ?? false))
                        itemList.Add(item.ConvertToDtoSurveyQuestionItem());
                }
            }
            data.dtoItems = itemList;

            return data;
        }