/// <summary> /// Insert a listening topic without any question /// </summary> /// <param name="listeningTopic"></param> /// <returns>int</returns> public int Insert(LOT_ListeningTopic listeningTopic) { int result = 0; try { if (listeningTopic != null) { listeningTopic.CreateDate = DateTime.Now; listeningTopic.UpdateDate = DateTime.Now; dbContext.LOT_ListeningTopics.InsertOnSubmit(listeningTopic); dbContext.SubmitChanges(); result = listeningTopic.ID; } } catch { result = 0; } return result; }
/// <summary> /// Insert a listening topic with its questions /// </summary> /// <param name="listeningTopic"></param> /// <param name="questionIDs"></param> /// <returns>Message</returns> public Message Insert(LOT_ListeningTopic listeningTopic, string[] questionIDs) { Message msg = null; DbTransaction trans = null; try { dbContext.Connection.Open(); trans = dbContext.Connection.BeginTransaction(); dbContext.Transaction = trans; //Insert new listening topic and get its ID int topicIDInserted = Insert(listeningTopic); string serverPath = System.Web.HttpContext.Current.Server.MapPath("~"); //move sound file from temp folder to LOT sound folder File.Move(serverPath + Constants.UPLOAD_TEMP_PATH + listeningTopic.FileName, serverPath + Constants.SOUND_FOLDER + listeningTopic.FileName); if (questionIDs != null) { foreach (string questionID in questionIDs) { //Get question from db LOT_Question questionDB = dbContext.LOT_Questions.Where(p => p.DeleteFlag == false && p.ID == int.Parse(questionID)).FirstOrDefault<LOT_Question>(); questionDB.ListeningTopicID = topicIDInserted; //Submit changes to db dbContext.SubmitChanges(); } } msg = new Message(MessageConstants.I0001, MessageType.Info, "Listening topic \"" + HttpUtility.HtmlDecode(CommonFunc.SubStringRoundWord(listeningTopic.TopicName, 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 listening topic with its questions /// </summary> /// <param name="topicUI"></param> /// <param name="questionIDs"></param> /// <returns>Message</returns> public Message Update(LOT_ListeningTopic topicUI, string[] questionIDs) { Message msg = null; DbTransaction trans = null; try { dbContext.Connection.Open(); trans = dbContext.Connection.BeginTransaction(); dbContext.Transaction = trans; string serverPath = System.Web.HttpContext.Current.Server.MapPath("~"); LOT_ListeningTopic topicDB = GetByID(topicUI.ID); //Another sound file was uploaded if (topicUI.FileName != topicDB.FileName) { //Remove old sound file File.Delete(serverPath + Constants.SOUND_FOLDER + topicDB.FileName); //move sound file from temp folder to LOT sound folder File.Move(serverPath + Constants.UPLOAD_TEMP_PATH + topicUI.FileName, serverPath + Constants.SOUND_FOLDER + topicUI.FileName); } //Set the updated information topicDB.FileName = topicUI.FileName; topicDB.RepeatTimes = topicUI.RepeatTimes; topicDB.TopicName = topicUI.TopicName; topicDB.UpdatedBy = topicUI.UpdatedBy; topicDB.UpdateDate = DateTime.Now; //Submit changes to db dbContext.SubmitChanges(); List<LOT_Question> currentQuestionsInDB = qDao.GetListByListeningTopicID(topicDB.ID); //Free all questions of the listening topic: set their listeing topic id to null foreach (LOT_Question question in currentQuestionsInDB) { if (questionIDs == null || !questionIDs.Contains(question.ID.ToString())) { //Get question from db LOT_Question questionDB = dbContext.LOT_Questions .Where(c => c.ID == question.ID).FirstOrDefault<LOT_Question>(); //Set updated information questionDB.UpdateDate = DateTime.Now; questionDB.UpdatedBy = topicUI.UpdatedBy; questionDB.ListeningTopicID = null; //Submit changes to db dbContext.SubmitChanges(); } } //Update question list of the listening topic if (questionIDs != null) { foreach (string questionID in questionIDs) { //Get question from db LOT_Question questionDB = dbContext.LOT_Questions .Where(c => c.ID == int.Parse(questionID)).FirstOrDefault<LOT_Question>(); //Set the undated information questionDB.UpdateDate = DateTime.Now; questionDB.UpdatedBy = topicUI.UpdatedBy; questionDB.ListeningTopicID = topicDB.ID; //Submit changes to db dbContext.SubmitChanges(); } } msg = new Message(MessageConstants.I0001, MessageType.Info, "Listening topic \"" + HttpUtility.HtmlDecode(CommonFunc.SubStringRoundWord(topicDB.TopicName, 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> /// Delete a listening topic /// </summary> /// <param name="listeningTopicUI"></param> /// <returns>bool</returns> private bool DeleteTopic(LOT_ListeningTopic listeningTopicUI) { bool result = false; if (listeningTopicUI != null) { // Get current info in dbContext LOT_ListeningTopic listeningTopicDb = dbContext.LOT_ListeningTopics .Single(p => p.DeleteFlag == false && p.ID == listeningTopicUI.ID); if (listeningTopicDb != null) { bool isUsed = IsTopicUsed(listeningTopicDb.ID); if (!isUsed) { // Set delete info listeningTopicDb.DeleteFlag = true; listeningTopicDb.UpdateDate = DateTime.Now; listeningTopicDb.UpdatedBy = listeningTopicUI.UpdatedBy; // Submit changes to dbContext dbContext.SubmitChanges(); //Free the all question of topic, set their listening topic id to null List<LOT_Question> arrQuestion = dbContext.LOT_Questions .Where(p => p.ListeningTopicID == listeningTopicUI.ID) .ToList<LOT_Question>(); foreach (LOT_Question question in arrQuestion) { question.ListeningTopicID = null; question.UpdatedBy = listeningTopicDb.UpdatedBy; question.UpdateDate = DateTime.Now; dbContext.SubmitChanges(); } result = true; } } } return result; }