public async Task <IActionResult> UpdateQuestionPartViewData(int surveyId, [FromBody] SBQuestionPartViewViewModel updatedQPartView)
        {
            if (ModelState.IsValid)
            {
                var survey = await this._unitOfWork.Surveys.GetAsync(surveyId);

                if (survey.Owner == this.User.Identity.Name || await HasModifySurveyPermissions(surveyId))
                {
                    try {
                        var questionPartView = await this._unitOfWork.QuestionPartViews.GetQuestionPartViewWithStructureAsync(updatedQPartView.Id);

                        this._surveyBuilderService.UpdateQuestionPartName(surveyId, questionPartView.QuestionPart, updatedQPartView.QuestionPart?.Name);
                        this._surveyBuilderService.UpdateQuestionPartViewOptions(questionPartView, updatedQPartView.isOptional, updatedQPartView.isHousehold, updatedQPartView.repeatSourceQuestionName, updatedQPartView.Icon);
                        this._surveyBuilderService.SetQuestionPartViewLabel(questionPartView, updatedQPartView.Label.Value, updatedQPartView.Label.Language);
                        questionPartView.IsMultiView = updatedQPartView.IsMultiView;

                        if (updatedQPartView.CATIDependent != null)
                        {
                            var catiConnectedQPartView = await this._unitOfWork.QuestionPartViews.GetQuestionPartViewWithStructureAsync(updatedQPartView.CATIDependent.Id);

                            this._surveyBuilderService.UpdateQuestionPartViewOptions(catiConnectedQPartView, updatedQPartView.isOptional, updatedQPartView.isHousehold, updatedQPartView.repeatSourceQuestionName, updatedQPartView.Icon);
                            this._surveyBuilderService.SetQuestionPartViewLabel(catiConnectedQPartView, updatedQPartView.CATIDependent.Label.Value, updatedQPartView.Label.Language);
                        }

                        await this._unitOfWork.SaveChangesAsync();

                        return(new OkResult());
                    } catch (Exception ex) {
                        return(BadRequest(ex.Message));
                    }
                }
                else
                {
                    return(BadRequest("Insufficient permissions."));
                }
            }
            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> AddPage(int surveyId, string surveyViewName, string initialLanguage, [FromBody] SBQuestionPartViewViewModel pageInfo)
        {
            //test
            if (ModelState.IsValid)
            {
                var survey = await this._unitOfWork.Surveys.GetAsync(surveyId);

                if (survey.Owner == this.User.Identity.Name || await HasModifySurveyPermissions(surveyId))
                {
                    var surveyView = await this._unitOfWork.SurveyViews.GetSurveyViewWithPagesStructureAsync(surveyId, surveyViewName);

                    QuestionPartView newPage = Mapper.Map <QuestionPartView> (pageInfo);
                    newPage.Labels = new LabelCollection <QuestionPartViewLabel> ()
                    {
                        new QuestionPartViewLabel()
                        {
                            Language = initialLanguage,
                            Value    = pageInfo.Label.Value
                        }
                    };
                    this._surveyBuilderService.AddSurveyPage(surveyView, newPage);

                    if (surveyViewName != "CATI")
                    {
                        var catiView = await this._unitOfWork.SurveyViews.GetSurveyViewWithPagesStructureAsync(surveyId, "CATI");

                        if (catiView != null)
                        {
                            newPage.CATIDependent = new QuestionPartView {
                                Icon   = newPage.Icon,
                                Order  = newPage.Order,
                                Labels = new LabelCollection <QuestionPartViewLabel> ()
                                {
                                    new QuestionPartViewLabel()
                                    {
                                        Language = initialLanguage,
                                        Value    = pageInfo.Label.Value
                                    }
                                }
                            };

                            this._surveyBuilderService.AddSurveyPage(catiView, newPage.CATIDependent);
                        }
                    }

                    await this._unitOfWork.SaveChangesAsync();

                    return(new OkResult());
                }
                else
                {
                    return(BadRequest("Insufficient permissions."));
                }
            }
            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> AddQuestionPartView(int surveyId, string surveyViewName, int parentQuestionPartViewId, string initialLanguage, [FromBody] SBQuestionPartViewViewModel questionInfo)
        {
            if (ModelState.IsValid)
            {
                var survey = await this._unitOfWork.Surveys.GetAsync(surveyId);

                if (survey.Owner == this.User.Identity.Name || await HasModifySurveyPermissions(surveyId))
                {
                    var parentPartView = await this._unitOfWork.QuestionPartViews.GetQuestionPartViewWithStructureAsync(parentQuestionPartViewId);

                    QuestionPartView question;
                    question = await this._unitOfWork.QuestionPartViews.GetAsync(questionInfo.Id);

                    bool fixConditionals = false;
                    if (question == null)
                    {
                        question        = Mapper.Map <QuestionPartView> (questionInfo);
                        question.Labels = new LabelCollection <QuestionPartViewLabel> ()
                        {
                            new QuestionPartViewLabel()
                            {
                                Language = initialLanguage,
                                Value    = questionInfo.Label.Value
                            }
                        };
                        //ensure question part name is unique
                        if (question.QuestionPart != null && !this._unitOfWork.Surveys.QuestionNameIsUnique(surveyId, question.QuestionPart.Name, null))
                        {
                            return(BadRequest("Question name must be unique"));
                        }
                        if (question.QuestionPart != null)
                        {
                            question.QuestionPart.Survey = survey;
                        }
                    }
                    else
                    {
                        //remove question from prior parent and fix order elements
                        fixConditionals = true;
                        var pastParentPartView = await this._unitOfWork.QuestionPartViews.GetQuestionPartViewWithStructureAsync(questionInfo.ParentViewId);

                        this._surveyBuilderService.RemoveQuestionPartView(pastParentPartView, question.Id, true);
                        question.Order = questionInfo.Order;
                    }
                    this._surveyBuilderService.AddQuestionPartView(parentPartView, question);
                    if (question.CATIDependent != null)
                    {
                        question.CATIDependent.Icon         = question.Icon;
                        question.CATIDependent.IsHousehold  = question.IsHousehold;
                        question.CATIDependent.IsOptional   = question.IsOptional;
                        question.CATIDependent.Order        = question.Order;
                        question.CATIDependent.ParentView   = parentPartView.CATIDependent;
                        question.CATIDependent.QuestionPart = question.QuestionPart;
                        question.CATIDependent.RepeatSource = question.RepeatSource;
                        question.CATIDependent.Labels       = new LabelCollection <QuestionPartViewLabel> ()
                        {
                            new QuestionPartViewLabel()
                            {
                                Language = initialLanguage,
                                Value    = questionInfo.CATIDependent.Label.Value
                            }
                        };
                        this._surveyBuilderService.AddQuestionPartView(question.CATIDependent.ParentView, question.CATIDependent);
                    }
                    await this._unitOfWork.SaveChangesAsync();

                    if (fixConditionals)
                    {
                        var structure = this._unitOfWork.SurveyViews.GetSurveyViewQuestionStructure(surveyId, surveyViewName);
                        if (question.QuestionPartViewChildren.Count > 0)
                        {
                            foreach (var child in question.QuestionPartViewChildren)
                            {
                                this._surveyBuilderService.ValidateConditionals(structure, child.Id);
                            }
                        }
                        else
                        {
                            this._surveyBuilderService.ValidateConditionals(structure, question.Id);
                        }
                        await this._unitOfWork.SaveChangesAsync();
                    }
                    return(Ok(question.ToLocalizedModel <SBQuestionPartViewViewModel> (initialLanguage)));
                }
                else
                {
                    return(BadRequest("Insufficient permissions."));
                }
            }
            return(BadRequest(ModelState));
        }