public void TestAddSubTask() { YouCompleteMe.Models.Subtask subtask = new YouCompleteMe.Models.Subtask(); subtask.taskID = 33; subtask.st_CreatedDate = DateTime.Now; subtask.st_Deadline = DateTime.Now; subtask.st_CompleteDate = DateTime.Now; subtask.st_Priority = 2; subtask.st_Description = "use for test unit test"; subtask.note = "this use only for unit test"; int subtaskID = SubtaskController.AddSubTask(subtask); YouCompleteMe.Models.Subtask result = SubtaskController.getASubTask(subtaskID); Assert.AreEqual(subtaskID, result.subtaskID); }
/* * This method use for add subtask into database. * It return the subtask id of new subtask. */ public static int AddSubTask(YouCompleteMe.Models.Subtask subtask) { SqlConnection connection = DBConnection.GetConnection(); connection.Open(); SqlTransaction sqlTransaction = connection.BeginTransaction(); string insertStatement = "INSERT subtask " + "(taskID, st_Description, st_CreatedDate, st_CompleteDate, st_Deadline, st_Priority) " + "VALUES (@taskID, @st_Description, @st_CreatedDate, @st_CompleteDate, @st_Deadline, @st_Priority)"; SqlCommand insertCommand = new SqlCommand(insertStatement, connection, sqlTransaction); insertCommand.Parameters.AddWithValue("@taskID", subtask.taskID); insertCommand.Parameters.AddWithValue("@st_Description", subtask.st_Description); insertCommand.Parameters.AddWithValue("@st_CreatedDate", subtask.st_CreatedDate); if (subtask.st_CompleteDate == DateTime.MaxValue) { insertCommand.Parameters.AddWithValue("@st_CompleteDate", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@st_CompleteDate", subtask.st_CompleteDate); } if (subtask.st_Deadline == DateTime.MaxValue) { insertCommand.Parameters.AddWithValue("@st_Deadline", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@st_Deadline", subtask.st_Deadline); } if (subtask.st_Priority == -1) { insertCommand.Parameters.AddWithValue("@st_Priority", DBNull.Value); } else { insertCommand.Parameters.AddWithValue("@st_Priority", subtask.st_Priority); } try { insertCommand.ExecuteNonQuery(); string selectStatement = "SELECT IDENT_CURRENT('subtask') FROM subtask"; SqlCommand selectCommand = new SqlCommand(selectStatement, connection, sqlTransaction); int subtaskID = Convert.ToInt32(selectCommand.ExecuteScalar()); //add notes to notes table if (subtask.note != "") { string insertNoteStatement = "INSERT note " + "(taskID, subtaskID, note_message) " + "VALUES (@taskID, @subtaskID, @note_message)"; SqlCommand insertNoteCommand = new SqlCommand(insertNoteStatement, connection, sqlTransaction); insertNoteCommand.Parameters.AddWithValue("@taskID", subtask.taskID); insertNoteCommand.Parameters.AddWithValue("@note_message", subtask.note); insertNoteCommand.Parameters.AddWithValue("@subtaskID", subtaskID); insertNoteCommand.ExecuteNonQuery(); } sqlTransaction.Commit(); return(subtaskID); } catch (SqlException ex) { sqlTransaction.Rollback(); throw ex; } finally { connection.Close(); } }
/* * This method use for add subtask into database. * It return the subtask id of new subtask. */ public static int AddSubTask(YouCompleteMe.Models.Subtask subtask) { return(SubtaskDAL.AddSubTask(subtask)); }