Esempio n. 1
0
        public JsonResult SaveProduct(List <HttpPostedFileBase> Files)
        {
            var count = 1;

            foreach (var item in Files)
            {
                Product product = new Product()
                {
                    ProductName       = "San pham " + count++,
                    CreatedDate       = DateTime.Now,
                    ProductCategoryId = 2
                };

                _productService.CreateProduct(product);

                var productId = product.ProductId;

                ProductExtend model = new ProductExtend()
                {
                    ProductId = productId,
                    FileName  = item.FileName,
                    FileSize  = item.ContentLength,
                    FilePath  = Util.CreateProductImage(item, "KhiNenThuyLuc")
                };
                _productService.CreateProductExtend(model);
            }

            _productService.Save();

            return(Json(new { success = false }));
        }
        public JsonResult DeleteCategoryById(int?categoryId)
        {
            if (categoryId != null)
            {
                //ProductCategory prodCate = _prodCateService.GetProductCategoryById((int)categoryId);
                //if (prodCate != null)
                //{
                //    foreach (var item in prodCate.Products)
                //    {
                //        var pathProd = item.ProductExtend.FilePath;
                //        ProductExtend prodEx = _prodService.GetProductExtendByProductId(item.ProductId);
                //        _prodService.DeleteProductExtend(prodEx);
                //        Util.DeleteFileLocal(pathProd);
                //    }
                //    List<Product> listProd = new List<Product>();
                //    listProd = _prodService.GetListProductByCategoryId((int)categoryId).ToList();
                //    _prodService.DeleteListProduct(listProd);
                //    _prodService.Save();
                //    _prodCateService.Save();
                //    var pathCate = prodCate.ProductCategoryExtend.FilePath;
                //    ProductCategoryExtend prodCateEx = _prodCateService.GetProductCategoryExtendByCategoryId((int)categoryId);
                //    _prodCateService.DeleteCategoryExtend(prodCateEx);
                //    ProductCategory prodCa = _prodCateService.GetProductCategoryById(categoryId);
                //    _prodCateService.DeleteProductCategory(prodCa);
                //    //Util.DeleteFileLocal(pathCate);
                //    _prodCateService.Save();

                //    return Json(new { success = true });
                //}
                foreach (var prod in _prodService.GetListProductByCategoryId((int)categoryId))
                {
                    Util.DeleteFileLocal(prod.ProductExtend.FilePath);
                    ProductExtend prodEx = _prodService.GetProductExtendByProductId(prod.ProductId);
                    _prodService.DeleteProductExtend(prodEx);
                }
                List <Product> listProd = new List <Product>();
                listProd = _prodService.GetListProductByCategoryId((int)categoryId).ToList();
                _prodService.DeleteListProduct(listProd);
                _prodService.Save();

                var prodCateEx = _prodCateService.GetProductCategoryExtendByCategoryId((int)categoryId);
                Util.DeleteFileLocal(prodCateEx.FilePath);
                _prodCateService.DeleteCategoryExtend(prodCateEx);
                var prodCate = _prodCateService.GetProductCategoryById((int)categoryId);
                _prodCateService.DeleteProductCategory(prodCate);
                _prodCateService.Save();
                return(Json(new { success = true }));
            }
            return(Json(new { success = false }));
        }
Esempio n. 3
0
 public void DeleteProductExtend(ProductExtend prodExtend)
 {
     _db.ProductExtends.Remove(prodExtend);
 }
 public JsonResult AddOrEditProduct(ProductViewModel model)
 {
     try
     {
         if (model.IsAdd)
         {
             Product prod = new Product
             {
                 Price             = model.Price,
                 ProductContent    = model.ProductContent,
                 ProductCategoryId = model.ProductCategoryId,
                 ProductName       = model.ProductName,
                 View        = 0,
                 CreatedDate = DateTime.Now
             };
             _prodService.CreateProduct(prod);
             var           cate   = _prodCateService.GetProductCategoryById(model.ProductCategoryId);
             ProductExtend prodEx = new ProductExtend
             {
                 ProductId = prod.ProductId,
                 FileName  = model.FileUpload.FileName,
                 FileSize  = model.FileUpload.ContentLength,
                 FilePath  = Util.CreateProductImage(model.FileUpload, User.Identity.GetUserId(), cate.FolderName)
             };
             _prodService.CreateProductExtend(prodEx);
             var product = new
             {
                 FilePath       = prod.ProductExtend.FilePath,
                 ProductName    = prod.ProductName,
                 Price          = prod.Price,
                 View           = prod.View,
                 ProductContent = prod.ProductContent,
                 ProductId      = prod.ProductId
             };
             return(Json(new { success = true, product = product, IsAdd = true }));
         }
         else
         {
             var prod = _prodService.GetProductById(model.ProductId);
             if (model.FileUpload == null)
             {
                 prod.ProductName    = model.ProductName;
                 prod.ProductContent = model.ProductContent;
                 prod.Price          = model.Price;
                 _prodService.Save();
             }
             else
             {
                 Util.DeleteFileLocal(prod.ProductExtend.FilePath);
                 prod.ProductExtend.FilePath = Util.CreateProductImage(model.FileUpload, User.Identity.GetUserId(), prod.ProductCategory.FolderName);
                 prod.ProductExtend.FileName = model.FileUpload.FileName;
                 prod.ProductExtend.FileSize = model.FileUpload.ContentLength;
                 prod.ProductName            = model.ProductName;
                 prod.ProductContent         = model.ProductContent;
                 prod.Price = model.Price;
                 _prodService.Save();
             }
             var product = new
             {
                 FilePath       = prod.ProductExtend.FilePath,
                 ProductName    = prod.ProductName,
                 Price          = prod.Price,
                 View           = prod.View,
                 ProductContent = prod.ProductContent,
                 ProductId      = prod.ProductId
             };
             return(Json(new { success = true, product = product, IsAdd = false }));
         }
     }
     catch
     {
         return(Json(new { success = false }));
     }
 }
Esempio n. 5
0
 public void CreateProductExtend(ProductExtend productExtend)
 {
     _db.ProductExtends.Add(productExtend);
     Save();
 }