public static DepartmentQuestion GetQuestion(int id) { DepartmentQuestion question = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM [Questions] WHERE Id = @id"; command.Parameters.Add(new SqlParameter("@id", id)); try { SqlDataReader reader = command.ExecuteReader(); reader.Read(); if (reader.HasRows) { question = new DepartmentQuestion((int)reader["Id"], reader["Title"].ToString(), reader["Question"].ToString(), (int)reader["ServerQuestion_Id"], (int)reader["Ticket_Id"]); } reader.Close(); return(question); } catch (SqlException e) { Console.WriteLine("ERROR: GetQuestion - Reading from database"); return(null); } } }
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"); } }
//######################## UI events ######################## void SelectQuestionsListItem(object sender, MouseEventArgs e) { if (questionsListBox.SelectedItems.Count == 0) { return; } DepartmentQuestion question; question = (DepartmentQuestion)questionsListBox.SelectedItems[0]; if (question == null) { activeQuestion = null; UI_UpdateList(); return; } question = DepartmentDatabaseHandler.GetQuestion(question.id); if (question == null) { activeQuestion = null; UI_UpdateList(); return; } UI_UpdateQuestionInfo(question); }
//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"); } }
//Updates questions info void UI_UpdateQuestionInfo(DepartmentQuestion question) { Invoke((MethodInvoker) delegate { activeQuestion = question; questionTitleTxt.Text = question.title; questionDetailsTxt.Text = question.question; answerTxt.Text = ""; }); }
void AnswerBtnClick(object sender, EventArgs e) { if (activeQuestion == null || answerTxt.Text == "") { return; } if (ticketService.AnswerQuestion(activeQuestion.serverQuestionId, answerTxt.Text)) { DepartmentDatabaseHandler.RemoveQuestion(activeQuestion.id); activeQuestion = null; answerBtn.Enabled = false; questionTitleTxt.Text = ""; questionDetailsTxt.Text = ""; notificationServiceClient.SendMessage(EventOperation.QUESTION_ANSWERED, userId, ""); UI_UpdateList(); } }