Exemplo n.º 1
0
        // GET: List/Item/Delete/5
        public ActionResult DeleteItem(int id, int listId)  //id = productId
        {
            //try
            //{
            //1. If UserProduct -> Delete
            ProductService productService = new ProductService();
            var            prodType       = productService.GetProductTypeId(id);

            if (prodType == 4)     //one-time UserProduct
            {
                var userProdId = productService.GetUserProductId(id);
                productService.DeleteUserProduct(userProdId);
            }

            //2. Update Sorting without this item
            LanguageService        languageService    = new LanguageService();
            var                    listEntries        = productService.GetEntriesAsProducts(listId, languageService.GetByCode(Session["LanguageCode"].ToString()).Id);
            UserListSortingService listSortingService = new UserListSortingService();
            ShoppingListService    listService        = new ShoppingListService();
            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;

            ShoppingListEntryService shoppingListEntryService = new ShoppingListEntryService();
            var entryId = shoppingListEntryService.GetEntryId(id, listId);

            listSortingService.SaveSorting(sorting, listEntries, entryId);

            //3. Delete ShoppingListEntry (if default or reusable only this)
            shoppingListEntryService.Delete(entryId);

            TempData["SuccessMessage"] = "Successfully deleted the entry.";
            return(RedirectToAction("SingleList", new { @id = listId }));
            //}
            //catch
            //{
            //    TempData["ErrorMessage"] = "There was an error while trying to delete this entry.";
            //    return Redirect(Request.UrlReferrer.ToString());
            //}
        }
Exemplo n.º 2
0
        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()));
            }
        }