예제 #1
0
        public ActionResult DeleteList(FormCollection collection)
        {
            try
            {
                int listId = int.Parse(collection["ShoppingList_Id"].ToString());

                ShoppingListService listService     = new ShoppingListService();
                LanguageService     languageService = new LanguageService();
                var langId = languageService.GetByCode(Session["LanguageCode"].ToString()).Id;

                //TO DO: delete all related entries (ShoppingListEntry)
                ProductService productService = new ProductService();
                var            listEntries    = productService.GetEntriesAsProducts(listId, langId);

                foreach (ProductDto product in listEntries)
                {
                    DeleteItem(product.Id, listId);
                }

                //delete ListSorting
                UserListSortingService listSortingService = new UserListSortingService();
                listSortingService.Delete((int)listService.Get(listId).ChosenSortingId);

                //delete list itself & all links in LinkUserToList
                listService.Delete(listId);

                TempData["SuccessMessage"] = "The list was successfully deleted.";
                return(RedirectToAction("Lists"));
            }
            catch
            {
                TempData["ErrorMessage"] = "Deleting the list wasn't successful. Try again.";
                return(RedirectToAction("Lists"));
            }
        }