public static Task Find(int id) { SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM tasks WHERE id = @TaskId;", conn); SqlParameter taskIdParameter = new SqlParameter(); taskIdParameter.ParameterName = "@TaskId"; taskIdParameter.Value = id.ToString(); cmd.Parameters.Add(taskIdParameter); rdr = cmd.ExecuteReader(); int foundTaskId = 0; string foundTaskDescription = null; DateTime foundTaskDueDate = new DateTime(0); while(rdr.Read()) { foundTaskId = rdr.GetInt32(0); foundTaskDescription = rdr.GetString(1); foundTaskDueDate = rdr.GetDateTime(2); } Task foundTask = new Task(foundTaskDescription, foundTaskDueDate, foundTaskId); if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return foundTask; }
public void Test_EqualOverrideTrueForSameDescription() { //Arrange, Act Task firstTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); Task secondTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); //Assert Assert.Equal(firstTask, secondTask); }
public void Test_Equal_ReturnsTrueIfDescriptionsAreTheSame() { //Arrange, Act Task firstTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); Task secondTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); //Assert Assert.Equal(firstTask, secondTask); }
public void Test_Equal_ReturnsTrueIfDescriptionsAreTheSame() { //Arrange, Act DateTime sampleDate = new DateTime(1990,09,05); Task firstTask = new Task("Mow the lawn", sampleDate, 1); Task secondTask = new Task("Mow the lawn", sampleDate, 1); //Assert Assert.Equal(firstTask, secondTask); }
public void Test_FindFindsTaskInDatabase() { //Arrange Task testTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); testTask.Save(); //Act Task foundTask = Task.Find(testTask.GetId()); //Assert Assert.Equal(testTask, foundTask); }
public void Test_Find_FindsTaskInDatabase() { //Arrange DateTime sampleDate = new DateTime(1990,09,05); Task testTask = new Task("Mow the lawn", sampleDate, 1); testTask.Save(); //Act Task foundTask = Task.Find(testTask.GetId()); //Assert Assert.Equal(testTask, foundTask); }
public void Test_Save_AssignsIdToObject() { //Arrange DateTime sampleDate = new DateTime(1990,09,05); Task testTask = new Task("Mow the lawn", sampleDate, 1); //Act testTask.Save(); Task savedTask = Task.GetAll()[0]; int result = savedTask.GetId(); int testId = testTask.GetId(); //Assert Assert.Equal(testId, result); }
public void Test_AddCategory_AddsCategoryToTask() { //Arrange Task testTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); testTask.Save(); Category testCategory = new Category("Home stuff"); testCategory.Save(); //Act testTask.AddCategory(testCategory); List<Category> result = testTask.GetCategories(); List<Category> testList = new List<Category>{testCategory}; //Assert Assert.Equal(testList, result); }
public void Test_AddTask_AddsTaskToCategory() { Category testCategory = new Category("Household chores"); testCategory.Save(); Task testTask = new Task("Mow the Lawn", new DateTime(2014, 4, 21)); testTask.Save(); Task testTask2 = new Task("Water the garden", new DateTime(2014, 4, 21)); testTask2.Save(); testCategory.AddTask(testTask); testCategory.AddTask(testTask2); List<Task> result = testCategory.GetTasks(); List<Task> testList = new List<Task>{testTask, testTask2}; Assert.Equal(testList, result); }
public static List<Task> GetAll() { List<Task> AllTasks = new List<Task>{}; SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM tasks ORDER BY duedate;", conn); rdr = cmd.ExecuteReader(); while(rdr.Read()) { int taskId = rdr.GetInt32(0); string taskDescription = rdr.GetString(1); DateTime taskDueDate = rdr.GetDateTime(2); Task newTask = new Task(taskDescription, taskDueDate, taskId); AllTasks.Add(newTask); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return AllTasks; }
public void AddTask(Task task) { _tasks.Add(task); }
public void Test_Save_SavesToDatabase() { //Arrange Task testTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); //Act testTask.Save(); List<Task> result = Task.GetAll(); List<Task> testList = new List<Task>{testTask}; //Assert Assert.Equal(testList, result); }
public void Test_Save_AssignsIdToObject() { //Arrange Task testTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); //Act testTask.Save(); Task savedTask = Task.GetAll()[0]; int result = savedTask.GetId(); int testId = testTask.GetId(); //Assert Assert.Equal(testId, result); }
public void Test_GetCategories_ReturnsAllTaskCategories() { //Arrange Task testTask = new Task("Mow the lawn", new DateTime(2015, 1, 18)); testTask.Save(); Category testCategory1 = new Category("Home stuff"); testCategory1.Save(); Category testCategory2 = new Category("Work stuff"); testCategory2.Save(); //Act testTask.AddCategory(testCategory1); List<Category> result = testTask.GetCategories(); List<Category> testList = new List<Category> {testCategory1}; //Assert Assert.Equal(testList, result); }
public void Test_GetTasks_RetrievesAllCategoryTasks() { Category testCategory = new Category("Household chores"); testCategory.Save(); Task testTask1 = new Task("Mow the lawn", new DateTime(2014, 4, 21)); testTask1.Save(); Task testTask2 = new Task("Buy plane ticket", new DateTime(2014, 4, 21)); testTask2.Save(); testCategory.AddTask(testTask1); List<Task> savedTasks = testCategory.GetTasks(); List<Task> testList = new List<Task> {testTask1}; Assert.Equal(testList, savedTasks); }
public List<Task> GetTasks() { SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT task_id FROM categories_tasks WHERE category_id = @CategoryId;", conn); SqlParameter categoryIdParameter = new SqlParameter(); categoryIdParameter.ParameterName = "@CategoryId"; categoryIdParameter.Value = this.GetId(); cmd.Parameters.Add(categoryIdParameter); rdr = cmd.ExecuteReader(); List<int> taskIds = new List<int> {}; while(rdr.Read()) { int taskId = rdr.GetInt32(0); taskIds.Add(taskId); } if (rdr != null) { rdr.Close(); } List<Task> tasks = new List<Task> {}; foreach (int taskId in taskIds) { SqlDataReader queryReader = null; SqlCommand taskQuery = new SqlCommand("SELECT * FROM tasks WHERE id = @TaskId;", conn); SqlParameter taskIdParameter = new SqlParameter(); taskIdParameter.ParameterName = "@TaskId"; taskIdParameter.Value = taskId; taskQuery.Parameters.Add(taskIdParameter); queryReader = taskQuery.ExecuteReader(); while(queryReader.Read()) { int thisTaskId = queryReader.GetInt32(0); string taskDescription = queryReader.GetString(1); DateTime taskDueDate = queryReader.GetDateTime(2); Task foundTask = new Task(taskDescription, taskDueDate, thisTaskId); tasks.Add(foundTask); } if (queryReader != null) { queryReader.Close(); } } if (conn != null) { conn.Close(); } return tasks; }
public void RemoveTask(Task task) { _tasks.Remove(task); }
public void AddTask(Task newTask) { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO categories_tasks (category_id, task_id) VALUES (@CategoryId, @TaskId)", conn); SqlParameter categoryIdParameter = new SqlParameter(); categoryIdParameter.ParameterName = "@CategoryId"; categoryIdParameter.Value = this.GetId(); cmd.Parameters.Add(categoryIdParameter); SqlParameter taskIdParameter = new SqlParameter(); taskIdParameter.ParameterName = "@TaskId"; taskIdParameter.Value = newTask.GetId(); cmd.Parameters.Add(taskIdParameter); cmd.ExecuteNonQuery(); if (conn != null) { conn.Close(); } }
public void Test_Save_SavesToDatabase() { //Arrange DateTime sampleDate = new DateTime(1990,09,05); Task testTask = new Task("Mow the lawn", sampleDate ,1); //Act testTask.Save(); List<Task> result = Task.GetAll(); List<Task> testList = new List<Task>{testTask}; //Assert Assert.Equal(testList, result); }