/// <summary> /// Delete a question /// </summary> /// <param name="questionUI"></param> /// <returns>bool</returns> private bool Delete(LOT_Question questionUI) { bool result = false; if (questionUI != null) { // Get current info in dbContext LOT_Question questionDb = GetById(questionUI.ID); if (questionDb != null && !IsUsed(questionDb.ID)) { // Set delete info questionDb.DeleteFlag = true; questionDb.UpdateDate = DateTime.Now; questionDb.UpdatedBy = questionUI.UpdatedBy; // Submit changes to dbContext dbContext.SubmitChanges(); result = true; } } return result; }
/// <summary> /// Insert a question with its answers /// </summary> /// <param name="question"></param> /// <param name="arrAnswer"></param> /// <returns>Message</returns> public Message Insert(LOT_Question question, List<LOT_Answer> arrAnswer) { Message msg = null; DbTransaction trans = null; try { dbContext.Connection.Open(); trans = dbContext.Connection.BeginTransaction(); dbContext.Transaction = trans; //Remove white space question.QuestionContent = Regex.Replace(question.QuestionContent, @"[ ]{2,}", " "); //Insert a question and get its ID int questionIDInserted = Insert(question); //Insert answer if question is multiplechoice if (arrAnswer != null) { foreach (LOT_Answer answer in arrAnswer) { answer.QuestionID = questionIDInserted; dbContext.LOT_Answers.InsertOnSubmit(answer); dbContext.SubmitChanges(); } } msg = new Message(MessageConstants.I0001, MessageType.Info, "Question \"" + CommonFunc.SubStringRoundWord(CommonFunc.RemoveAllHtmlWithNoTagsAllowed(question.QuestionContent), Constants.QUESTION_CONTENT_LENGTH_SHOWED_IN_MESSAGE) + "\"", "added"); trans.Commit(); } catch { if (trans != null) { trans.Rollback(); } // Show system error msg = new Message(MessageConstants.E0007, MessageType.Error); } return msg; }
/// <summary> /// Update a question with its answers /// </summary> /// <param name="questionUI"></param> /// <param name="arrAnswer"></param> /// <returns>Message</returns> public Message Update(LOT_Question questionUI, List<LOT_Answer> arrAnswer) { Message msg = null; DbTransaction trans = null; try { dbContext.Connection.Open(); trans = dbContext.Connection.BeginTransaction(); dbContext.Transaction = trans; //Get question from database LOT_Question questionDB = GetById(questionUI.ID); //Set updated information questionDB.QuestionContent = Regex.Replace(questionUI.QuestionContent, @"[ ]{2,}", " "); questionDB.UpdatedBy = questionUI.UpdatedBy; questionDB.UpdateDate = DateTime.Now; questionDB.SectionID = questionUI.SectionID; dbContext.SubmitChanges(); //Update answers of the question if (arrAnswer != null) { //Get list of answer before updating List<LOT_Answer> currentAnswersInDb = answerDao.GetListByQuestionID(questionDB.ID); foreach (LOT_Answer a in currentAnswersInDb) { //Delete the answer if it was delete on page if (IsDeletedOnUI(a, arrAnswer)) { answerDao.Delete(a); } } foreach (LOT_Answer answer in arrAnswer) { //Update the answer if it exists if (answer.ID != 0) { LOT_Answer answerDB = dbContext.LOT_Answers .Where(c => c.ID == answer.ID).SingleOrDefault<LOT_Answer>(); //Set the updated information answerDB.AnswerContent = answer.AnswerContent; answerDB.AnswerOrder = answer.AnswerOrder; answerDB.IsCorrect = answer.IsCorrect; //Submit changes to database dbContext.SubmitChanges(); } //Insert new answer if it does not exist else { answer.QuestionID = questionDB.ID; answerDao.Insert(answer); } } } msg = new Message(MessageConstants.I0001, MessageType.Info, "Question \"" + CommonFunc.SubStringRoundWord(CommonFunc.RemoveAllHtmlWithNoTagsAllowed(questionDB.QuestionContent), Constants.QUESTION_CONTENT_LENGTH_SHOWED_IN_MESSAGE) + "\"", "updated"); trans.Commit(); } catch { if (trans != null) { trans.Rollback(); } // Show system error msg = new Message(MessageConstants.E0007, MessageType.Error); } return msg; }
/// <summary> /// Insert a question without answer /// </summary> /// <param name="question"></param> /// <returns>int</returns> public int Insert(LOT_Question question) { int result = 0; try { if (question != null) { //Set the updated information question.CreatedDate = DateTime.Now; question.UpdateDate = DateTime.Now; dbContext.LOT_Questions.InsertOnSubmit(question); //Submit changes to database dbContext.SubmitChanges(); result = question.ID; } } catch { result = 0; } return result; }