/* +-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- | Subroutine: removeList +-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- | Purpose: to remove a given list from the dase. +-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */ public void removeList(WList removing_list) { //The lambda method for removing the wlist from the table is declared. wlistsTable.DeleteOnSubmit(removing_list); //The changes to the table and respected database are made. wlistsTable.Context.SubmitChanges(); }
public void changes_to_other_users_items_dont_save() { //Arrange: A repository with one item and a controller are created. WList list = new WList { wlistID = 1, name = "list", userName = "******", sundayDate = DateTime.Now.Date }; Item item = new Item { wlistID = 1, name = "item", cost = 1, quantity = 1 }; var itemRepository = UnitTestHelpers.MockItemRepository(item); var listRepository = UnitTestHelpers.MockWListRepository(list); var controller = UnitTestHelpers.HomeControllerForUser(itemRepository, listRepository, "differentUser"); //Act: The item is edited by the different user. //item.cost //controller.saveItem(new Item); }
public void valid_change_is_recorded() { //Arrange: A repository with one item and a controller are created. WList list = new WList { wlistID = 1, name = "list", userName = "******", sundayDate = DateTime.Now.Date }; Item item = new Item { wlistID = 1, name = "item", cost = 1, quantity = 1}; var itemRepository = UnitTestHelpers.MockItemRepository(item); var listRepository = UnitTestHelpers.MockWListRepository(list); HomeController controller = UnitTestHelpers.HomeControllerForUser(itemRepository,listRepository,"user"); //Act: the item is edited Item edited = new Item{wlistID=1,name="item",cost=2,quantity=1,itemID=item.itemID}; controller.saveItem(edited); controller.User.Identity.Name.ShouldEqual("user"); //Assert: there is only one item in the repository, and it has a cost of 2. itemRepository.items.Count().ShouldEqual(1); itemRepository.items.First().cost.ShouldEqual(2); }
/* +-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- | Subroutine: saveList +-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- | Purpose: to update changes, or insert lists into the database. +-- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */ public void saveList(WList saving_wlist) { //The lists name is checked to be of valid length, and truncated if not. saving_wlist.name=(saving_wlist.name.Length<20)? saving_wlist.name: saving_wlist.name.Substring(0,20); if(saving_wlist.wlistID==0){ //The given list is not in table and to be added. wlistsTable.InsertOnSubmit(saving_wlist); } else if(wlistsTable.GetOriginalEntityState(saving_wlist)==null){ //The given object changed if it is already in the table. WList updating = wlistsTable.FirstOrDefault(x=>x.wlistID==saving_wlist.wlistID); updating.name = saving_wlist.name; updating.sundayDate = saving_wlist.sundayDate; } //Any changes are saved to the table and the respected database table. wlistsTable.Context.SubmitChanges(); }
public ActionResult editListName(WList editing) { //Validility check to assure current user is authentic if(editing.userName!=User.Identity.Name){ //Name is being changed for a different user, security breach detected, user //is returned to the main screen. TempData["message"]="Failed to change name, security breach detected."; return RedirectToAction("Index", new { index = Edit }); } string originalName = listRepository.wlists.First(x=>x.wlistID==editing.wlistID).name; listRepository.saveList(editing); TempData["message"] = originalName + "'s name has been changed to \"" + editing.name +"\"."; //The user is directed to the index of the list being edited. return RedirectToAction("Index",new { focus=editing.sundayDate, wlistID=editing.wlistID, index=Edit }); }