public DomainModelResponse Add(QuestionnaireAddRequest request)
        {
            Questionnaire Questionnaire = new Questionnaire();

            Questionnaire.QuestionnaireCode = request.QuestionnaireCode;
            Questionnaire.IsATemplate       = request.IsATemplate;
            Questionnaire.LastChangedTime   = DateTime.UtcNow;
            StringBuilder qIds = new StringBuilder();

            if (request.QuestionIds != null)
            {
                foreach (int i in request.QuestionIds)
                {
                    qIds.Append(i.ToString());
                    qIds.Append(",");
                }
                qIds.Remove(qIds.Length - 1, 1);
            }
            if (request.Questions != null)
            {
                List <QuestionType> questionTypes = _repQuestionType.Get().ToList();
                foreach (var q in request.Questions)
                {
                    EntityModel.Question eQ = new EntityModel.Question()
                    {
                        QuestionText    = q.questionText,
                        LastChangedTime = DateTime.UtcNow,
                        QuestionType    = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(),
                        QuestionTypeId  = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().QuestionTypeId,
                        Priority        = q.Priority.ToString()
                    };
                    _repQuestion.Add(eQ);
                    _uow.Commit();
                    eQ = _repQuestion.Get(filter: f => f.QuestionText == q.questionText).FirstOrDefault();
                    qIds.Append(eQ.QuestionId.ToString());
                    qIds.Append(",");
                    foreach (var ans in q.answerChoices)
                    {
                        AnswerChoice ac = new AnswerChoice()
                        {
                            Answer     = ans,
                            Question   = eQ,
                            QuestionId = eQ.QuestionId
                        };
                        _repAnswerChoice.Add(ac);
                    }
                }
                qIds.Remove(qIds.Length - 1, 1);
            }
            Questionnaire.QuestionSet = qIds.ToString();
            _repQuestionnaire.Add(Questionnaire);
            _uow.Commit();
            _questionnaireResponse.addResponse("Add", MessageCodes.InfoCreatedSuccessfully, "questionnaire : " + request.QuestionnaireCode);
            return(_questionnaireResponse);
        }
        public DomainModelResponse Add(QuestionnaireAddRequest request)
        {
            Questionnaire Questionnaire = new Questionnaire();
            Questionnaire.QuestionnaireCode = request.QuestionnaireCode;
            Questionnaire.IsATemplate = request.IsATemplate;
            Questionnaire.LastChangedTime = DateTime.UtcNow;
            StringBuilder qIds = new StringBuilder();
            if(request.QuestionIds != null)
            {
                foreach(int i in request.QuestionIds)
                {
                    qIds.Append(i.ToString());
                    qIds.Append(",");
                }
                qIds.Remove(qIds.Length - 1, 1);
            }
            if(request.Questions != null)
            {
                List<QuestionType> questionTypes = _repQuestionType.Get().ToList();
                foreach(var q in request.Questions)
                {
                    EntityModel.Question eQ = new EntityModel.Question()
                    {
                        QuestionText = q.questionText,
                        LastChangedTime = DateTime.UtcNow,
                        QuestionType = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(),
                        QuestionTypeId = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().QuestionTypeId,

                    };
                    _repQuestion.Add(eQ);
                    _uow.Commit();
                    eQ = _repQuestion.Get(filter: f => f.QuestionText == q.questionText).FirstOrDefault();
                    qIds.Append(eQ.QuestionId.ToString());
                    qIds.Append(",");
                    foreach(var ans in q.answerChoices)
                    {
                        AnswerChoice ac = new AnswerChoice()
                        {
                            Answer = ans,
                            Question = eQ,
                            QuestionId = eQ.QuestionId
                        };
                        _repAnswerChoice.Add(ac);
                    }
                }
                qIds.Remove(qIds.Length - 1, 1);
            }
            Questionnaire.QuestionSet = qIds.ToString();
            _repQuestionnaire.Add(Questionnaire);
            _uow.Commit();
            _questionnaireResponse.addResponse("Add", MessageCodes.InfoCreatedSuccessfully, "questionnaire : " + request.QuestionnaireCode);
            return _questionnaireResponse;
        }
        public DomainModelResponse Update(QuestionnaireUpdateRequest request)
        {
            Questionnaire Questionnaire = _repQuestionnaire.Get(filter: f => f.QuestionnaireCode == request.QuestionnaireCode).FirstOrDefault();

            if (request.IsATemplate.HasValue)
            {
                Questionnaire.IsATemplate = (bool)request.IsATemplate;
            }
            List <string> qIds = Questionnaire.QuestionSet.Split(',').ToList();

            if (request.QuestionIds != null) //List of QuestionIds to be deleted
            {
                foreach (int id in request.QuestionIds)
                {
                    qIds.Remove(id.ToString());
                }
            }

            if (request.Questions != null)
            {
                List <QuestionType> questionTypes = _repQuestionType.Get().ToList();
                foreach (var q in request.Questions)
                {
                    EntityModel.Question eQ = new EntityModel.Question()
                    {
                        QuestionText    = q.questionText,
                        LastChangedTime = DateTime.UtcNow,
                        QuestionType    = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(),
                        QuestionTypeId  = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().QuestionTypeId,
                        Priority        = q.Priority.ToString()
                    };
                    _repQuestion.Add(eQ);
                    _uow.Commit();
                    eQ = _repQuestion.Get(filter: f => f.QuestionText == q.questionText).FirstOrDefault();
                    qIds.Add(eQ.QuestionId.ToString());
                    foreach (var ans in q.answerChoices)
                    {
                        AnswerChoice ac = new AnswerChoice()
                        {
                            Answer     = ans,
                            Question   = eQ,
                            QuestionId = eQ.QuestionId
                        };
                        _repAnswerChoice.Add(ac);
                    }
                }
            }

            StringBuilder fQIds = new StringBuilder();

            foreach (string id in qIds)
            {
                fQIds.Append(id);
                fQIds.Append(",");
            }
            fQIds.Remove(fQIds.Length - 1, 1);
            Questionnaire.QuestionSet = fQIds.ToString();
            _repQuestionnaire.Update(Questionnaire);
            _uow.Commit();
            _questionnaireResponse.addResponse("Update", MessageCodes.InfoSavedSuccessfully, "questionnaire : " + request.QuestionnaireCode);
            return(_questionnaireResponse);
        }
        public DomainModelResponse Update(QuestionnaireUpdateRequest request)
        {
            Questionnaire Questionnaire = _repQuestionnaire.Get(filter: f => f.QuestionnaireCode == request.QuestionnaireCode).FirstOrDefault();
            if (request.IsATemplate.HasValue)
                Questionnaire.IsATemplate = (bool)request.IsATemplate;
            List<string> qIds = Questionnaire.QuestionSet.Split(',').ToList();
            if(request.QuestionIds != null) //List of QuestionIds to be deleted
            {
                foreach(int id in request.QuestionIds)
                {
                    qIds.Remove(id.ToString());
                }
            }

            if (request.Questions != null)
            {
                List<QuestionType> questionTypes = _repQuestionType.Get().ToList();
                foreach (var q in request.Questions)
                {
                    EntityModel.Question eQ = new EntityModel.Question()
                    {
                        QuestionText = q.questionText,
                        LastChangedTime = DateTime.UtcNow,
                        QuestionType = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(),
                        QuestionTypeId = questionTypes.Where(x => x.Type.Equals(q.questionType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault().QuestionTypeId,

                    };
                    _repQuestion.Add(eQ);
                    _uow.Commit();
                    eQ = _repQuestion.Get(filter: f => f.QuestionText == q.questionText).FirstOrDefault();
                    qIds.Add(eQ.QuestionId.ToString());
                    foreach (var ans in q.answerChoices)
                    {
                        AnswerChoice ac = new AnswerChoice()
                        {
                            Answer = ans,
                            Question = eQ,
                            QuestionId = eQ.QuestionId
                        };
                        _repAnswerChoice.Add(ac);
                    }
                }
            }

            StringBuilder fQIds = new StringBuilder();
            foreach(string id in qIds)
            {
                fQIds.Append(id);
                fQIds.Append(",");
            }
            fQIds.Remove(fQIds.Length - 1, 1);
            Questionnaire.QuestionSet = fQIds.ToString();
            _repQuestionnaire.Update(Questionnaire);
            _uow.Commit();
            _questionnaireResponse.addResponse("Update", MessageCodes.InfoSavedSuccessfully, "questionnaire : " + request.QuestionnaireCode);
            return _questionnaireResponse;
        }