public Question PostQuestionFromDraft(QuestionCreateFromDraftRequestDTO q, int accountId) { DraftQuestion question = _questionServices.GetDraftQuestion(q.QuestionDraftId); //Validations if (accountId != question.AccountId) { throw new InvalidAccountException("User cannot edit another user's question"); } if (q.Text == null || q.Text.Length < _questionCharMin || q.Text.Length > _questionCharMax) { throw new InvalidQuestionLengthException("Question must be between " + _questionCharMin + " and " + _questionCharMax + " characters."); } //Create question after validations based on type //Then post question and return posted question Question questionToPost = question; switch (q.QuestionType) { case _schoolQuestion: //Validate Ids? questionToPost = new SchoolQuestion() { AccountId = accountId, SchoolId = q.SchoolId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; _questionServices.DeleteQuestion(q.QuestionDraftId); return(_questionServices.UpdateQuestion(questionToPost)); case _departmentQuestion: //Validate Ids? questionToPost = new DepartmentQuestion() { AccountId = accountId, SchoolDepartmentId = q.DepartmentId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; // Delete question then return posted question _questionServices.DeleteQuestion(q.QuestionDraftId); return(_questionServices.PostQuestion(questionToPost)); case _courseQuestion: questionToPost = new CourseQuestion() { AccountId = accountId, CourseId = q.CourseId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; _questionServices.DeleteQuestion(q.QuestionDraftId); return(_questionServices.PostQuestion(questionToPost)); default: throw new ArgumentException("Invalid Question type"); } }
//private string _schoolQuestionTypeName = typeof(SchoolQuestion).Name; //private string _departmentQuestionTypeName = typeof(DepartmentQuestion).Name; //private string _courseQuestionTypeName = typeof(CourseQuestion).Name; //private string _draftQuestionTypeName = typeof(CourseQuestion).Name; public Question PostQuestion(QuestionCreateRequestDTO q, int accountId) { //Validations if (q.Text == null || q.Text.Length < _questionCharMin || q.Text.Length > _questionCharMax) { throw new InvalidQuestionLengthException("Question must be between " + _questionCharMin + " and " + _questionCharMax + " characters."); } //Create question after validations based on type //Then post question and return posted question Question question; switch (q.QuestionType) { case _schoolQuestion: //Validate Ids? question = new SchoolQuestion() { AccountId = accountId, SchoolId = q.SchoolId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; return(_questionServices.PostQuestion(question)); case _departmentQuestion: //Validate Ids? question = new DepartmentQuestion() { AccountId = accountId, SchoolDepartmentId = q.DepartmentId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; return(_questionServices.PostQuestion(question)); case _courseQuestion: question = new CourseQuestion() { AccountId = accountId, CourseId = q.CourseId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; return(_questionServices.PostQuestion(question)); case _draftQuestion: question = new DraftQuestion() { AccountId = accountId, Text = q.Text, ExpNeededToAnswer = q.ExpNeededToAnswer }; return(_questionServices.PostQuestion(question)); default: throw new ArgumentException("Invalid Question type"); } }