public ActionResult RenameList(FormCollection collection) { try { var newName = collection["ListName"]; int listId = int.Parse(collection["ShoppingList_Id"]); ShoppingListService listService = new ShoppingListService(); var dbList = listService.Get(listId); if (newName == dbList.Name) { throw new Exception("The name of the list is unchanged."); } ShoppingListDto list = new ShoppingListDto { Id = listId, Name = newName }; listService.Update(list); TempData["SuccessMessage"] = "The list's name was successfully updated!"; return(Redirect(Request.UrlReferrer.ToString())); } catch { TempData["ErrorMessage"] = "Updating the list's name wasn't successful."; return(Redirect(Request.UrlReferrer.ToString())); } }
public ActionResult EditItem(FormCollection collection) { try { var name = collection["Name"]; var reusable = collection["UserProduct"]; var price = decimal.Parse(collection["Price"]); var listId = int.Parse(collection["ListId"]); var qty = uint.Parse(collection["Quantity"]); var unitTypeId = int.Parse(collection["UnitTypesListId"]); int catId = 0; int userCatId = 0; if (collection["CategoryListId"] != "") { catId = int.Parse(collection["CategoryListId"]); } var userCat = collection["UserCategory"]; var currencyId = int.Parse(collection["CurrencyListId"]); var prodTypeId = int.Parse(collection["ProductTypeId"]); var prodId = int.Parse(collection["ProductId"]); /* UPDATING ShoppingListEntry */ ShoppingListEntryService entryService = new ShoppingListEntryService(); ShoppingListEntryDto entry = new ShoppingListEntryDto { Id = entryService.GetEntryId(prodId, listId), Quantity = (int)qty, ProductTypeId = prodTypeId, ShoppingList_Id = listId, Product_Id = prodId, State_Id = 2 //Default is unchecked }; entryService.Update(entry); //updates ShoppingListEntry /* CREATE NEW UserCatgory */ if (userCat != "") { LanguageService languageService = new LanguageService(); CategoryDto category = new CategoryDto { Name = userCat, LanguageId = languageService.GetByCode(Session["LanguageCode"].ToString()).Id, UserId = int.Parse(Session["UserId"].ToString()) }; CategoryService categoryService = new CategoryService(); userCatId = categoryService.Create(category); } ProductService productService = new ProductService(); if (prodTypeId == 4 || prodTypeId == 3) { if (reusable == "false") { prodTypeId = 4; //4 = UserListProduct = non reusable } else { prodTypeId = 3; } /* UPDATING UserProduct */ UserProductDto userProduct = new UserProductDto { Id = productService.GetUserProductId(prodId), ProductId = prodId, Name = name }; if (userCat != "") { userProduct.Category_Id = userCatId; } else { userProduct.Category_Id = catId; } userProduct.User_Id = int.Parse(Session["UserId"].ToString()); userProduct.Unit_Id = unitTypeId; userProduct.Price = price; userProduct.Currency_Id = currencyId; productService.Update(userProduct); } else if (prodTypeId == 1) //if default Product { if (catId != 0) { //create LinkDefaultProductToCategory entry } else if (userCatId != 0) { //create LinkDefaultProductToCategory entry } } /* UPDATING Product -> for new Timestamp*/ ProductDto productDto = new ProductDto { Id = prodId, ProductTypeId = prodTypeId }; productService.Update(productDto); /* UPDATING ShoppingList -> for new Timestamp: */ ShoppingListDto shoppingList = new ShoppingListDto { Id = listId }; ShoppingListService listService = new ShoppingListService(); listService.Update(shoppingList); TempData["SuccessMessage"] = "Successfully edited this item"; return(RedirectToAction("SingleList", new { @id = listId })); } catch { TempData["ErrorMessage"] = "There was an error while editing the item"; return(Redirect(Request.UrlReferrer.ToString())); } }
public ActionResult CreateItem(FormCollection collection) { try { var name = collection["Name"]; var reusable = collection["UserProduct"]; var price = decimal.Parse(collection["Price"]); var listId = int.Parse(collection["ShoppingList_Id"]); var qty = int.Parse(collection["quantity"]); var unitTypeId = int.Parse(collection["UnitTypesListId"]); //TODO: create lists in view and get id (like for countries/languages) var catId = int.Parse(collection["CategoryListId"]); var userCat = collection["UserCategory"]; int userCatId = 0; var currencyId = int.Parse(collection["CurrencyListId"]); int prodType = 0; var prodId = 0; var chosenProductId = collection["ChosenProductId"]; //gets defaultProductId from dropdown if (name != "" && chosenProductId != "") //Name filled in and defaultProduct chosen { throw new Exception("You can either create a new item or choose from the default products, but not both!"); } LanguageService languageService = new LanguageService(); if (userCat != "") { CategoryDto category = new CategoryDto { Name = userCat, LanguageId = languageService.GetByCode(Session["LanguageCode"].ToString()).Id, UserId = int.Parse(Session["UserId"].ToString()) }; CategoryService categoryService = new CategoryService(); userCatId = categoryService.Create(category); } ShoppingListEntryService entryService = new ShoppingListEntryService(); ProductService productService = new ProductService(); if (name != "") //IF UserProduct -> create Product - ShoppingListEntry - UserProduct { if (reusable == "false") { prodType = 4; //4 = UserListProduct = non reusable } else { prodType = 3; //reusable UserProduct } //create a new Product ProductDto productDto = new ProductDto { ProductTypeId = prodType }; prodId = productService.Create(productDto); //create new UserProduct UserProductDto userProduct = new UserProductDto { ProductId = prodId, Name = name }; if (userCat != "") { userProduct.Category_Id = userCatId; } else { userProduct.Category_Id = catId; } userProduct.User_Id = int.Parse(Session["UserId"].ToString()); userProduct.Unit_Id = unitTypeId; userProduct.Price = price; userProduct.Currency_Id = currencyId; entryService.Create(userProduct); } else if (chosenProductId != "") //IF DefaultProduct -> create ShoppingListEntry & LinkDefaultProductToUser { //check if chosen defaultProduct or reusable UserProduct prodId = int.Parse(chosenProductId); prodType = productService.GetProductTypeId(prodId); if (prodType == 1) //if DefaultProduct: create Link entry { DefaultProductDto defaultProductDto = new DefaultProductDto { Id = productService.GetDefaultProductId(prodId) }; productService.CreateLink(defaultProductDto, int.Parse(Session["UserId"].ToString())); } //if reusable UserProduct: only create ShoppingListEntry } //create Entry if not existent right now! var existentEntries = entryService.GetEntriesByListId(listId); foreach (ShoppingListEntryDto shoppingListEntry in existentEntries) { if (shoppingListEntry.Product_Id == prodId) { throw new Exception("You can't add the same product to your list twice."); } } ShoppingListEntryDto entry = new ShoppingListEntryDto { Quantity = qty, Product_Id = prodId, ShoppingList_Id = listId, State_Id = 2 //Default is unchecked }; entryService.Create(entry); //Update ShoppingList to update Timestamp: ShoppingListDto shoppingList = new ShoppingListDto { Id = listId }; ShoppingListService listService = new ShoppingListService(); listService.Update(shoppingList); // Update Sorting var listEntries = productService.GetEntriesAsProducts(listId, languageService.GetByCode(Session["LanguageCode"].ToString()).Id); UserListSortingService listSortingService = new UserListSortingService(); var sortingId = listService.Get(listId, int.Parse(Session["UserId"].ToString())).ChosenSortingId; UserListSortingDto sorting = new UserListSortingDto(); if (sortingId != null) { sorting.Id = (int)sortingId; } sorting.ShoppingList_Id = listId; listSortingService.SaveSorting(sorting, listEntries); TempData["SuccessMessage"] = "Successfully created a new item"; return(RedirectToAction("SingleList", new { @id = listId })); } catch { TempData["ErrorMessage"] = "There was an error while creating a new item. Be aware that you can only create a new item or choose from the dropdown list of default products and your own reusable items, but you can't do both."; return(Redirect(Request.UrlReferrer.ToString())); } }