public ActionResult AddProduct(ProductAddModel model)
        {
            string role = CurrentUser.Role;

            if (role != Convert.ToString((byte)UserType.Admin))
            {
                return(RedirectToAction("index", "home"));
            }
            #region treeinsert
            var    category                  = _categoryService.GetCategoryByCategoryId(model.CategoryId);//tree insert product
            string productGroupName          = _categoryService.GetCategoryByCategoryId(model.ProductGroupId).CategoryName;
            PrepareCategoryHash categoryHash = new PrepareCategoryHash();
            categoryHash.CreateHashTableAndProductTree();
            var categoryTable = categoryHash.GetCategoryTable;
            var productGroupTreeForCategory  = (ProductGroupTree)categoryTable[category.CategoryName];
            ProductGroupTreeNode node        = productGroupTreeForCategory.GetProductTreeNodeByProductGroupName(productGroupTreeForCategory.GetRoot(), productGroupName);
            ProductItemModel     productItem = new ProductItemModel();
            productItem.BrandName          = model.BrandName;
            productItem.ModelName          = model.ModelName;
            productItem.ProductCost        = model.Cost;
            productItem.ProductDescription = model.ProductDescription;
            productItem.ProductPrice       = model.Price;
            productItem.Status             = true;
            productItem.ProductNumber      = model.ProductNumber;
            productItem.ProductName        = model.ProductName;
            node.ProductBase.Products.Add(productItem);

            #endregion
            #region databaseinsert
            string fileNameDt = "";
            if (Request.Files.Count > 0)//resim upload
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    fileNameDt = fileName;
                    var path = Path.Combine(Server.MapPath("~/ProductImages/"), fileName);
                    file.SaveAs(path);
                }
            }
            Product product = new Product();//database adding
            product.BrandName          = model.BrandName;
            product.CategoryId         = model.ProductGroupId;
            product.ModelName          = model.ModelName;
            product.ProductCost        = model.Cost;
            product.ProductDescription = model.ProductDescription;
            product.ProductName        = model.ProductName;
            product.ProductNumber      = model.ProductNumber;
            product.ProductPrice       = model.Price;
            product.ProductImagePath   = fileNameDt;
            product.Status             = true;
            _productservice.AddProduct(product);
            #endregion
            return(RedirectToAction("AddProduct", new { message = "success" }));
        }
        public ActionResult Edit(ProductAddModel model, int hdnCategoryId)
        {
            var product      = _productservice.GetProductByProductId(model.ProductId);
            var categoryLast = _categoryService.GetCategoryByCategoryId(product.CategoryId);
            var categoryTop  = _categoryService.GetCategoryByCategoryId(categoryLast.CategoryParentId);

            if (hdnCategoryId != model.ProductGroupId)//kategori ve ürün grubu aynı değilse
            {
                //önce ağaçdaki ürün bulunur ve silinir daha sonra yeni yerine eklenir
                var newProductGroupName = _categoryService.GetCategoryByCategoryId(model.ProductGroupId).CategoryName;
                #region tree_delete_product
                PrepareCategoryHash categoryHash = new PrepareCategoryHash();
                categoryHash.CreateHashTableAndProductTree();
                Hashtable categoryTable             = categoryHash.GetCategoryTable;
                var       categories                = _categoryService.GetAllCategory().Where(x => x.CategoryType == (byte)CategoryType.Category);
                List <ProductItemModel> productList = new List <ProductItemModel>();

                var tree = (ProductGroupTree)categoryTable[categoryTop.CategoryName];

                var treeNode = tree.GetProductTreeNodeByProductGroupName(tree.GetRoot(), categoryLast.CategoryName
                                                                         );
                if (treeNode.ProductBase != null)
                {
                    var anyProduct = treeNode.ProductBase.Products.Where(x => x.ProductId == model.ProductId).FirstOrDefault();
                    if (anyProduct != null)
                    {
                        treeNode.ProductBase.Products.Remove(anyProduct);
                    }
                }


                #endregion

                #region treeinsert
                var    category         = _categoryService.GetCategoryByCategoryId(model.CategoryId);//tree insert product
                string productGroupName = _categoryService.GetCategoryByCategoryId(model.ProductGroupId).CategoryName;

                var productGroupTreeForCategory  = (ProductGroupTree)categoryTable[categoryTop.CategoryName];
                ProductGroupTreeNode node        = productGroupTreeForCategory.GetProductTreeNodeByProductGroupName(productGroupTreeForCategory.GetRoot(), newProductGroupName);
                ProductItemModel     productItem = new ProductItemModel();
                productItem.BrandName          = model.BrandName;
                productItem.ModelName          = model.ModelName;
                productItem.ProductCost        = model.Cost;
                productItem.ProductDescription = model.ProductDescription;
                productItem.ProductPrice       = model.Price;
                productItem.Status             = true;
                productItem.ProductNumber      = model.ProductNumber;
                productItem.ProductName        = model.ProductName;
                node.ProductBase.Products.Add(productItem);

                #endregion
            }
            else
            {
                //ürün bilgileri değiştirilir

                PrepareCategoryHash categoryHash = new PrepareCategoryHash();
                categoryHash.CreateHashTableAndProductTree();
                Hashtable categoryTable             = categoryHash.GetCategoryTable;
                var       categories                = _categoryService.GetAllCategory().Where(x => x.CategoryType == (byte)CategoryType.Category);
                List <ProductItemModel> productList = new List <ProductItemModel>();

                var tree = (ProductGroupTree)categoryTable[categoryTop.CategoryName];

                var treeNode = tree.GetProductTreeNodeByProductGroupName(tree.GetRoot(), categoryLast.CategoryName
                                                                         );
                if (treeNode.ProductBase != null)
                {
                    var anyProduct = treeNode.ProductBase.Products.Where(x => x.ProductId == model.ProductId).FirstOrDefault();
                    if (anyProduct != null)
                    {
                        anyProduct.ModelName          = model.ModelName;
                        anyProduct.ProductCost        = model.Cost;
                        anyProduct.ProductDescription = model.ProductDescription;
                        anyProduct.ProductName        = model.ProductName;
                        anyProduct.ProductNumber      = model.ProductNumber;
                        anyProduct.ProductPrice       = model.Price;
                        anyProduct.BrandName          = model.BrandName;
                    }
                }
            }
            #region databaseEdit
            if (model.CategoryId != hdnCategoryId)
            {
                product.CategoryId = model.ProductGroupId;
            }
            product.BrandName          = model.BrandName;
            product.ModelName          = model.ModelName;
            product.ProductCost        = model.Cost;
            product.ProductDescription = model.ProductDescription;
            product.ProductName        = model.ProductName;
            product.ProductNumber      = model.ProductNumber;
            product.ProductPrice       = model.Price;
            _productservice.UpdateProduct(product);
            #endregion

            return(RedirectToAction("Edit", new { id = model.ProductId, message = "success" }));
        }