public static List<Category> GetAll() { List<Category> allCategories = new List<Category>{}; SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM categories;", conn); rdr = cmd.ExecuteReader(); while(rdr.Read()) { int categoryId = rdr.GetInt32(0); string categoryName = rdr.GetString(1); Category newCategory = new Category(categoryName, categoryId); allCategories.Add(newCategory); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return allCategories; }
public void Test_Delete_DeletesCategoryFromDatabase() { //Arrange string name1 = "Home stuff"; Category testCategory1 = new Category(name1); testCategory1.Save(); string name2 = "Work stuff"; Category testCategory2 = new Category(name2); testCategory2.Save(); DateTime fakeTime = new DateTime(2016, 1, 1); Task testTask1 = new Task("Mow the lawn", testCategory1.GetId(),fakeTime); testTask1.Save(); DateTime fakeTime1 = new DateTime(2016, 1, 1); Task testTask2 = new Task("Send emails", testCategory2.GetId(),fakeTime1); testTask2.Save(); //Act testCategory1.Delete(); List<Category> resultCategories = Category.GetAll(); List<Category> testCategoryList = new List<Category> {testCategory2}; List<Task> resultTasks = Task.GetAll(); List<Task> testTaskList = new List<Task> {testTask2}; //Assert Assert.Equal(testCategoryList, resultCategories); Assert.Equal(testTaskList, resultTasks); }
public static Category Find(int id) { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM categories WHERE id = @CategoryId;", conn); SqlParameter categoryIdParameter = new SqlParameter(); categoryIdParameter.ParameterName = "@CategoryId"; categoryIdParameter.Value = id.ToString(); cmd.Parameters.Add(categoryIdParameter); SqlDataReader rdr = cmd.ExecuteReader(); int foundCategoryId = 0; string foundCategoryDescription = null; while(rdr.Read()) { foundCategoryId = rdr.GetInt32(0); foundCategoryDescription = rdr.GetString(1); } Category foundCategory = new Category(foundCategoryDescription, foundCategoryId); if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return foundCategory; }
public HomeModule() { Get["/"] = _ => { return View["index.cshtml"]; }; Get["/tasks"] = _ => { var AllTasks = Task.All(); return View["tasks.cshtml", AllTasks]; }; Get["/tasks/new"] = _ => { return View["task_form.cshtml"]; }; Get["/tasks/{id}"] = parameters => { var SelectedTask = Task.Find(parameters.id); return View["/task.cshtml", SelectedTask]; }; Get["/categories"] = _ => { var AllCategories = Category.All(); return View["categories.cshtml", AllCategories]; }; Get["/categories/new"] = _ => { return View["category_form.cshtml"]; }; Post["/categories"] = _ => { var NewCategory = new Category(Request.Form["category-name"]); var AllCategories = Category.All(); return View["categories.cshtml", AllCategories]; }; Get["/categories/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); var SelectedCategory = Category.Find(parameters.id); var CategoryTasks = SelectedCategory.GetTasks(); model.Add("category", SelectedCategory); model.Add("tasks", CategoryTasks); return View["/category.cshtml", model]; }; Get["/categories/{id}/tasks/new"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); Category SelectedCategory = Category.Find(parameters.id); List<Task> AllTasks = SelectedCategory.GetTasks(); model.Add("category", SelectedCategory); model.Add("tasks", AllTasks); return View["/category_tasks_form.cshtml", model]; }; Post["/tasks"] = _ => { Dictionary<string, object> model = new Dictionary<string, object>(); Category SelectedCategory = Category.Find(Request.Form["category-id"]); List<Task> CategoryTasks = SelectedCategory.GetTasks(); string TaskDescription = Request.Form["task-description"]; Task NewTask = new Task(TaskDescription); CategoryTasks.Add(NewTask); model.Add("tasks", CategoryTasks); model.Add("category", SelectedCategory); return View["/category.cshtml", model]; }; }
public void Test_CategoryEqualOverrideTrueForSameName() { //Arrange, Act Category firstCategory = new Category("Household chores"); Category secondCategory = new Category("Household chores"); //Assert Assert.Equal(firstCategory, secondCategory); }
public void T2_Equal_ReturnsTrueForSameName() { // Arrange, act Category firstCategory = new Category("Household chores"); Category secondCategory = new Category("Household chores"); // Assert Assert.Equal(firstCategory, secondCategory); }
protected void LinkButtonSaveCategory_Click(object sender, EventArgs e) { if (Page.IsValid) { ToDoListEntities context = new ToDoListEntities(); Category Category = new Category(); Category.Name = this.TextBoxCategoryName.Text; context.Categories.Add(Category); context.SaveChanges(); Response.Redirect("~/ManageCategories.aspx"); } }
public void Test_FindFindsCategoryInDatabase() { //Arrange Category testCategory = new Category("Household chores"); testCategory.Save(); //Act Category foundCategory = Category.Find(testCategory.GetId()); //Assert Assert.Equal(testCategory, foundCategory); }
public void T3_Save_SavesCategoryToDatabase() { // Arrange Category testCategory = new Category("Household chores"); testCategory.Save(); // Act List<Category> result = Category.GetAll(); List<Category> testList = new List<Category>{testCategory}; // Assert Assert.Equal(testList, result); }
public HomeModule() { //Index Get["/"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["index.cshtml", AllCategories]; }; //Get: Lists Get["/tasks"] = _ => { List<Task> AllTasks = Task.GetAll(); return View["tasks.cshtml", AllTasks]; }; Get["/categories"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["categories.cshtml", AllCategories]; }; Get["/categories/new"] = _ => { return View["categories_form.cshtml"]; }; Post["/categories/new"] = _ => { Category newCategory = new Category(Request.Form["category-name"]); newCategory.Save(); return View["success.cshtml"]; }; Get["/tasks/new"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["tasks_form.cshtml", AllCategories]; }; Post["/tasks/new"] = _ => { Task newTask = new Task(Request.Form["task-description"], Request.Form["category-id"]); newTask.Save(); return View["success.cshtml"]; }; Post["/tasks/delete"] = _ => { Task.DeleteAll(); return View["cleared.cshtml"]; }; Get["categories/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); var SelectedCategory = Category.Find(parameters.id); var CategoryTasks = SelectedCategory.GetTasks(); model.Add("category", SelectedCategory); model.Add("tasks", CategoryTasks); return View["/category.cshtml", model]; }; }
public void Test_SaveAssignsIdToCategoryObject() { //Arrange Category testCategory = new Category("Household chores"); testCategory.Save(); //Act Category savedCategory = Category.GetAll()[0]; int result = savedCategory.GetId(); int testId = testCategory.GetId(); //Assert Assert.Equal(testId, result); }
public void Test_GetTasks_RetrievesAllTasksWithCategory() { Category testCategory = new Category("Household chores"); testCategory.Save(); Task firstTask = new Task("Mow the lawn", testCategory.GetId()); firstTask.Save(); Task secondTask = new Task("Do the dishes", testCategory.GetId()); secondTask.Save(); List<Task> testTaskList = new List<Task> {firstTask, secondTask}; List<Task> resultTaskList = testCategory.GetTasks(); Assert.Equal(testTaskList, resultTaskList); }
public void Test_AddCategory_AddsCategoryToTask() { //Arrange Task testTask = new Task("Mow the lawn", new DateTime(2016, 2, 2), false); 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 Test18_GetCategories_ReturnsAllTaskCategories() { DateTime testTime = new DateTime(2016, 1, 1); Task testTask = new Task("Mow the lawn",testTime); testTask.Save(); Category testCategory1 = new Category("Home Stuff"); testCategory1.Save(); Category testCategory2 = new Category("Work Stuff"); testCategory2.Save(); testTask.AddCategory(testCategory1); List<Category> result = testTask.GetCategories(); List<Category> testList= new List<Category>{testCategory1}; Assert.Equal(testList,result); }
public void Test_Delete_DeletesCategoryAssociationsFromDatabase() { //Arrange Task testTask = new Task("Mow the lawn", false); testTask.Save(); string testName = "Home stuff"; Category testCategory = new Category(testName); testCategory.Save(); //Act testCategory.AddTask(testTask); testCategory.Delete(); List<Category> resultTaskCategories = testTask.GetCategories(); List<Category> testTaskCategories = new List<Category> {}; //Assert Assert.Equal(testTaskCategories, resultTaskCategories); }
public void Test_Delete_DeletesTaskAssociationsFromDatabase() { //Arrange Category testCategory = new Category("Home stuff"); testCategory.Save(); string testDescription = "Mow the lawn"; Task testTask = new Task(testDescription, new DateTime(2016, 2, 2)); testTask.Save(); //Act testTask.AddCategory(testCategory); testTask.Delete(); List<Task> resultCategoryTasks = testCategory.GetTasks(); List<Task> testCategoryTasks = new List<Task> {}; //Assert Assert.Equal(testCategoryTasks, resultCategoryTasks); }
public void Test_AddTask_AddsTaskToCategory() { //Arrange Category testCategory = new Category("Household chores"); testCategory.Save(); Task testTask = new Task("Mow the lawn", false); testTask.Save(); Task testTask2 = new Task("Water the garden", false); testTask2.Save(); //Act testCategory.AddTask(testTask); testCategory.AddTask(testTask2); List<Task> result = testCategory.GetTasks(); List<Task> testList = new List<Task>{testTask, testTask2}; //Assert Assert.Equal(testList, result); }
public void T7_Update_UpdatesCategoryInDatabase() { //Arrange string name = "Home Chores"; Category testCategory = new Category(name); testCategory.Save(); string newName = "Work Chores"; //Act testCategory.Update(newName); string result = testCategory.GetName(); //Assert Assert.Equal(newName, result); }
public HomeModule() { Get["/"] = _ => { return(View["index.cshtml"]); }; Get["/tasks/new"] = _ => { return(View["task_form.cshtml"]); }; Post["/tasks/new"] = _ => { Task newTask = new Task(Request.Form["task-description"]); newTask.Save(); return(View["success.cshtml"]); }; Get["/categories"] = _ => { var allCategories = Category.GetAll(); return(View["categories.cshtml", allCategories]); }; Get["/tasks"] = _ => { List <Task> allTasks = Task.GetAll(); return(View["tasks.cshtml", allTasks]); }; Get["/categories/new"] = _ => { return(View["category_form.cshtml"]); }; Post["/categories/new"] = _ => { Category newCategory = new Category(Request.Form["category-name"]); newCategory.Save(); return(View["success.cshtml"]); }; Get["tasks/{id}"] = parameters => { Dictionary <string, object> model = new Dictionary <string, object>(); Task SelectedTask = Task.Find(parameters.id); List <Category> TaskCategories = SelectedTask.GetCategories(); List <Category> AllCategories = Category.GetAll(); model.Add("task", SelectedTask); model.Add("taskCategories", TaskCategories); model.Add("allCategories", AllCategories); return(View["task.cshtml", model]); }; Get["categories/{id}"] = parameters => { Dictionary <string, object> model = new Dictionary <string, object>(); Category SelectedCategory = Category.Find(parameters.id); List <Task> CategoryTasks = SelectedCategory.GetTasks(); List <Task> AllTasks = Task.GetAll(); model.Add("category", SelectedCategory); model.Add("categoryTasks", CategoryTasks); model.Add("allTasks", AllTasks); return(View["category.cshtml", model]); }; Post["/categories"] = _ => { var newCategory = new Category(Request.Form["category-name"]); newCategory.Save(); var allCategories = Category.GetAll(); return(View["categories.cshtml", allCategories]); }; Post["/tasks"] = _ => { Category selectedCategory = Category.Find(Request.Form["category-id"]); int selectedCategoryId = selectedCategory.GetId(); string taskDescription = Request.Form["task-description"]; Task newTask = new Task(taskDescription); newTask.Save(); List <Task> categoryTasks = selectedCategory.GetTasks(); return(View["category.cshtml", selectedCategory]); }; Post["task/add_category"] = _ => { Category category = Category.Find(Request.Form["category-id"]); Task task = Task.Find(Request.Form["task-id"]); task.AddCategory(category); return(View["success.cshtml"]); }; Post["category/add_task"] = _ => { Category category = Category.Find(Request.Form["category-id"]); Task task = Task.Find(Request.Form["task-id"]); category.AddTask(task); return(View["success.cshtml"]); }; }
public void Dispose() { Task.DeleteAll(); Category.DeleteAll(); }
public void Test_GetTasks_ReturnAllCategoryTasks() { Category testCategory = new Category("Househould chores"); testCategory.Save(); DateTime testTime = new DateTime(2016, 1, 1); Task testTask1= new Task("Mow the lawn",testTime); testTask1.Save(); Task testTask2 = new Task("Buy plane ticket",testTime); testTask2.Save(); testCategory.AddTask(testTask1); List<Task> savedTasks = testCategory.GetTasks(); List<Task> testList= new List<Task>{testTask1}; Assert.Equal(testList,savedTasks); }
public void Test_Delete_DeletesCategoryAssociationsFromDatabase() { //Arrange DateTime testTime = new DateTime(2016, 1, 1); Task testTask = new Task("Mow the lawn",testTime); testTask.Save(); string testName = "Home stuff"; Category testCategory = new Category(testName); testCategory.Save(); //Act testCategory.AddTask(testTask); testCategory.Delete(); List<Category> resultTaskCategories = testTask.GetCategories(); List<Category> testTaskCategories = new List<Category> {}; List<Task> resultsTasks= Task.GetAll(); List<Task> testTasks= new List<Task>{testTask}; //Assert Assert.Equal(testTaskCategories, resultTaskCategories); Assert.Equal(testTasks, resultsTasks); }
public void T8_Delete_DeletesCategoryFromDatabase() { //Arrange string name1 = "Home stuff"; Category testCategory1 = new Category(name1); testCategory1.Save(); string name2 = "Work stuff"; Category testCategory2 = new Category(name2); testCategory2.Save(); //Act testCategory1.Delete(); List<Category> resultCategories = Category.GetAll(); List<Category> testCategoryList = new List<Category> {testCategory2}; //Assert Assert.Equal(testCategoryList, resultCategories); }
public static List<Category> GetAll() { List<Category> allCategories = new List<Category>{}; //DB.Connection() points to Database.cs which in turn uses the address from Startup.cs; opens a connection //write our connections opening/closing as a separate method SqlConnection conn = DB.Connection(); conn.Open(); //saves our SQL query and executes SqlCommand cmd = new SqlCommand("SELECT * FROM categories;", conn); SqlDataReader rdr = cmd.ExecuteReader(); //Read() returns true if there are more rows to read, false if not while(rdr.Read()) { //GetInt32 and GetString from SQLDataReader class; int categoryId = rdr.GetInt32(0); string categoryName = rdr.GetString(1); Category newCategory = new Category(categoryName, categoryId); allCategories.Add(newCategory); } //closes connection if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return allCategories; }
public HomeModule() { Get["/"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["index.cshtml", AllCategories]; }; Get["/tasks"] = _ => { List<Task> AllTasks = Task.GetAll(); return View["tasks.cshtml", AllTasks]; }; Get["/categories"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["categories.cshtml", AllCategories]; }; Get["/categories/new"] = _ => { return View["categories_form.cshtml"]; }; Post["/categories/new"] = _ => { Category newCategory = new Category(Request.Form["category-name"]); newCategory.Save(); return View["success.cshtml"]; }; Get["/tasks/new"] = _ => { return View["tasks_form.cshtml"]; }; Post["/tasks/new"] = _ => { Task newTask = new Task(Request.Form["task-description"],Request.Form["task-dueDate"]); newTask.Save(); return View["success.cshtml"]; }; Post["/tasks/delete"] = _ => { Task.DeleteAll(); return View["cleared.cshtml"]; }; Get["/categories/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); var SelectedCategory = Category.Find(parameters.id); var CategoryTasks = SelectedCategory.GetTasks(); model.Add("category", SelectedCategory); model.Add("tasks", CategoryTasks); return View["category.cshtml", model]; }; Get["category/edit/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); return View["category_edit.cshtml", SelectedCategory]; }; Patch["category/edit/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); SelectedCategory.Update(Request.Form["category-name"]); return View["success.cshtml"]; }; Get["category/delete/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); return View["category_delete.cshtml", SelectedCategory]; }; Delete["category/delete/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); SelectedCategory.Delete(); return View["success.cshtml"]; }; Get["tasks/{id}"]=parameters=>{ Dictionary<string, object> model = new Dictionary<string, object>(); Task SelectedTask = Task.Find(parameters.id); List<Category> TaskCategories = SelectedTask.GetCategories(); List<Category> AllCategories = Category.GetAll(); model.Add("task",SelectedTask); model.Add("taskCategories",TaskCategories); model.Add("allCategories",AllCategories); return View["task.cshtml",model]; }; Get["categories/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); Category selectedCategory = Category.Find(parameters.id); List<Task> categoryTasks = selectedCategory.GetTasks(); List<Task> allTasks = Task.GetAll(); model.Add("category", selectedCategory); model.Add("categoryTasks", categoryTasks); model.Add("allTasks", allTasks); return View["category.cshtml", model]; }; Post["task/add_category"] = _ =>{ Category category= Category.Find(Request.Form["category-id"]); Task task = Task.Find(Request.Form["task-id"]); task.AddCategory(category); return View["success.cshtml"]; }; Post["category/add_task"] = _ => { Category category = Category.Find(Request.Form["category-id"]); Task task = Task.Find(Request.Form["task-id"]); category.AddTask(task); return View["success.cshtml"]; }; }
public List<Category> GetCategories() { SqlConnection conn = DB.Connection(); SqlDataReader rdr = null; conn.Open(); SqlCommand cmd = new SqlCommand("SELECT category_id FROM categories_tasks WHERE task_id = @TaskId;", conn); SqlParameter taskIdParameter = new SqlParameter(); taskIdParameter.ParameterName = "@TaskId"; taskIdParameter.Value = this.GetId(); cmd.Parameters.Add(taskIdParameter); rdr = cmd.ExecuteReader(); List<int> categoryIds = new List<int> {}; while (rdr.Read()) { int categoryId = rdr.GetInt32(0); categoryIds.Add(categoryId); } if (rdr != null) { rdr.Close(); } List<Category> categories = new List<Category> {}; foreach (int categoryId in categoryIds) { SqlDataReader queryReader = null; SqlCommand categoryQuery = new SqlCommand("SELECT * FROM categories WHERE id = @CategoryId;", conn); SqlParameter categoryIdParameter = new SqlParameter(); categoryIdParameter.ParameterName = "@CategoryId"; categoryIdParameter.Value = categoryId; categoryQuery.Parameters.Add(categoryIdParameter); queryReader = categoryQuery.ExecuteReader(); while (queryReader.Read()) { int thisCategoryId = queryReader.GetInt32(0); string categoryName = queryReader.GetString(1); Category foundCategory = new Category(categoryName, thisCategoryId); categories.Add(foundCategory); } if (queryReader != null) { queryReader.Close(); } } if (conn != null) { conn.Close(); } return categories; }
public void Test_GetTasks_ReturnsAllCategoryTasks() { //Arrange Category testCategory = new Category("Household chores"); testCategory.Save(); Task testTask1 = new Task("Mow the lawn", false); testTask1.Save(); Task testTask2 = new Task("Buy plane ticket", false); testTask2.Save(); //Act testCategory.AddTask(testTask1); List<Task> savedTasks = testCategory.GetTasks(); List<Task> testList = new List<Task> {testTask1}; //Assert Assert.Equal(testList, savedTasks); }
public void Test_Delete_DeletesTaskFromDatabase() { //Arrange Category testCategory = new Category("Home stuff"); testCategory.Save(); string description1 = "Mow the lawn"; Task testTask1 = new Task(description1); testTask1.Save(); string description2 = "Do homework"; Task testTask2 = new Task(description2); testTask2.Save(); //Act testTask1.AddCategory(testCategory); testTask1.Delete(); List<Task> resultTasks = Task.GetAll(); List<Task> testTaskList = new List<Task> {testTask2}; List<Task> resultCategoryTasks = testCategory.GetTasks(); List<Task> testCategoryTasks = new List<Task> {}; //Assert Assert.Equal(testTaskList, resultTasks); Assert.Equal(testCategoryTasks, resultCategoryTasks); }
public void Test_GetCategories_ReturnsAllTaskCategories() { //Arrange Task testTask = new Task("Mow the lawn"); 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 HomeModule() { Get["/"] = _ => { return View["index.cshtml"]; }; Get["/due-date"] = _ => { return View["dueDate_form.cshtml"]; }; Get["/duedate"] = _ => { List<Task> AllTasks = Task.FindDueDate(Request.Query["latest-date"]); return View["tasks.cshtml",AllTasks]; }; Get["/tasks"] = _ => { List<Task> AllTasks = Task.GetAll(); return View["tasks.cshtml", AllTasks]; }; Get["/categories"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["categories.cshtml", AllCategories]; }; Get["/categories/new"] = _ => { return View["categories_form.cshtml"]; }; Post["/categories/new"] = _ => { Category newCategory = new Category(Request.Form["category-name"]); newCategory.Save(); return View["success.cshtml"]; }; Get["/tasks/new"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["tasks_form.cshtml", AllCategories]; }; Post["/tasks/new"] = _ => { Task newTask = new Task(Request.Form["task-description"], Request.Form["category-id"], Request.Form["new-date"]); newTask.Save(); return View["success.cshtml"]; }; Post["/tasks/delete"] = _ => { Task.DeleteAll(); return View["cleared.cshtml"]; }; Get["/categories/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); var SelectedCategory = Category.Find(parameters.id); var CategoryTasks = SelectedCategory.GetTasks(); model.Add("category", SelectedCategory); model.Add("tasks", CategoryTasks); return View["category.cshtml", model]; }; Get["category/edit/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); return View["category_edit.cshtml", SelectedCategory]; }; Patch["category/edit/{id}"] = parameters=>{ Category SelectedCategory = Category.Find(parameters.id); SelectedCategory.Update(Request.Form["category-name"]); return View["success.cshtml"]; }; Get["category/delete/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); return View["category_delete.cshtml", SelectedCategory]; }; Delete["category/delete/{id}"] = parameters => { Category SelectCategory = Category.Find(parameters.id); SelectCategory.Delete(); return View["success.cshtml"]; }; }
public static List<Category> GetAll() { List<Category> allCategories = new List<Category>{}; //Open connection SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM categories;", conn); SqlDataReader rdr = cmd.ExecuteReader(); //SqlDataReader.Read() method returns boolean - true if more rows, false otherwise while(rdr.Read()) { //Specific methods to get types from DB int categoryId = rdr.GetInt32(0); string categoryName = rdr.GetString(1); Category newCategory = new Category(categoryName, categoryId); allCategories.Add(newCategory); } //More explanation needed... if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return allCategories; }
public HomeModule() { //Index Get["/"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["index.cshtml", AllCategories]; }; //Get: Lists Get["/tasks"] = _ => { List<Task> AllTasks = Task.GetAll(); return View["tasks.cshtml", AllTasks]; }; Get["/categories"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["categories.cshtml", AllCategories]; }; //Create new category Get["/categories/new"] = _ => { return View["categories_form.cshtml"]; }; Post["/categories/new"] = _ => { Category newCategory = new Category(Request.Form["category-name"]); newCategory.Save(); return View["success.cshtml"]; }; //Create new task Get["/tasks/new"] = _ => { List<Category> AllCategories = Category.GetAll(); return View["tasks_form.cshtml", AllCategories]; }; Post["/tasks/new"] = _ => { Task newTask = new Task(Request.Form["task-description"], Request.Form["category-id"]); newTask.Save(); return View["success.cshtml"]; }; //"Clear All" routes Post["/tasks/clear"] = _ => { Task.DeleteAll(); return View["cleared.cshtml"]; }; Post["/categories/clear"] = _ => { Category.DeleteAll(); return View["cleared.cshtml"]; }; //View specific category Get["categories/{id}"] = parameters => { Dictionary<string, object> model = new Dictionary<string, object>(); var SelectedCategory = Category.Find(parameters.id); var CategoryTasks = SelectedCategory.GetTasks(); model.Add("category", SelectedCategory); model.Add("tasks", CategoryTasks); return View["category.cshtml", model]; }; //Edit specific category Get["category/edit/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); return View["category_edit.cshtml", SelectedCategory]; }; Patch["category/edit/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); SelectedCategory.Update(Request.Form["category-name"]); return View["success.cshtml"]; }; //Delete specific category Get["category/delete/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); return View["category_delete.cshtml", SelectedCategory]; }; Delete["category/delete/{id}"] = parameters => { Category SelectedCategory = Category.Find(parameters.id); SelectedCategory.Delete(); return View["success.cshtml"]; }; }