예제 #1
0
        public ActionResult Create(CreateProductPostRequest productRequest)
        {
            productRequest.CreateBy = User.Identity.GetUserName();
            if (User.IsInRole("Administrator"))
            {
                productRequest.Status = (int)Define.Status.Active;
            }
            else
            {
                productRequest.Status = (int)Define.Status.WaitingCreate;
            }
            if (ModelState.IsValid)
            {
                var file = Request.Files["coverImage"];
                //HttpPostedFileBase file = coverImage;
                if (file != null && file.ContentLength > 0)
                {
                    if (file.ContentLength > 0)
                    {
                        // width + height will force size, care for distortion
                        //Exmaple: ImageUpload imageUpload = new ImageUpload { Width = 800, Height = 700 };

                        // height will increase the width proportionally
                        //Example: ImageUpload imageUpload = new ImageUpload { Height= 600 };

                        // width will increase the height proportionally
                        ImageUpload imageUpload = new ImageUpload {
                            Width = 600
                        };

                        // rename, resize, and upload
                        //return object that contains {bool Success,string ErrorMessage,string ImageName}
                        ImageResult imageResult = imageUpload.RenameUploadFile(file);
                        if (imageResult.Success)
                        {
                            // Add new image to database
                            var photo = new share_Images
                            {
                                ImageName = imageResult.ImageName,
                                ImagePath = Path.Combine(ImageUpload.LoadPath, imageResult.ImageName)
                            };
                            var imageId = service.AddImage(photo);
                            // Add product
                            productRequest.CoverImageId = imageId;
                            productRequest.CreateBy     = User.Identity.GetUserName();
                            service.AddProduct(productRequest);
                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            // use imageResult.ErrorMessage to show the error
                            ViewBag.Error = imageResult.ErrorMessage;
                        }
                    }
                }
            }
            PopulateStatusDropDownList();
            ViewBag.BrandId = PopulateListBrand(productRequest.BrandId);
            return(View("Create1", productRequest));
        }
예제 #2
0
        /// <summary>
        /// Add new product to database
        /// </summary>
        /// <returns></returns>
        public bool AddProduct(CreateProductPostRequest newProduct)
        {
            try
            {
                ecom_Products product = new ecom_Products()
                {
                    ProductCode  = newProduct.ProductCode != null?newProduct.ProductCode:"",
                    Name         = newProduct.Name,
                    Price        = newProduct.Price,
                    Quantity     = newProduct.Quantity,
                    Unit         = newProduct.Unit,
                    BrandId      = newProduct.BrandId,
                    CoverImageId = newProduct.CoverImageId,
                    Description  = newProduct.Description,
                    Description2 = newProduct.Description2,
                    //Tags = newProduct.Tags,
                    Tags              = "",
                    IsNewProduct      = newProduct.IsNewProduct,
                    IsBestSellProduct = newProduct.IsBestSellProduct,
                    SortOrder         = newProduct.SortOrder,
                    Status            = newProduct.Status
                };

                share_Images coverImage = imageRepository.GetByID(newProduct.CoverImageId);
                product.share_Images.Add(coverImage);
                db.Insert(product);
                db.Save();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #3
0
        public ActionResult Create(CreateProductPostRequest productRequest)
        {
            if (ModelState.IsValid)
            {
                var file = Request.Files["coverImage"];
                if (file != null && file.ContentLength > 0)
                {
                    if (file.ContentLength > 0)
                    {
                        string largeFileName = null;
                        bool   isSuccess     = UploadProductImages(file, out largeFileName);

                        if (isSuccess)
                        {
                            share_Images largeImage = new share_Images
                            {
                                ImageName = largeFileName,
                                ImagePath = Path.Combine(ImageUpload.LargeImagePath, largeFileName)
                            };

                            var imageId = service.AddImage(largeImage);
                            // Add product
                            productRequest.CoverImageId = imageId;
                            bool isAddSuccess = service.AddProduct(productRequest);
                            if (isAddSuccess)
                            {
                                return(RedirectToAction("Index"));
                            }
                            else
                            {
                                ViewBag.Error = "Upload product image fail!";
                            }
                        }
                        else
                        {
                            // use imageResult.ErrorMessage to show the error
                            ViewBag.Error = "Create product fail!";
                        }
                    }
                }
            }
            PopulateStatusDropDownList();
            ViewBag.BrandId = PopulateListBrand(productRequest.BrandId);
            return(View(productRequest));
        }
예제 #4
0
 /// <summary>
 /// Add new product to database
 /// </summary>
 /// <returns></returns>
 public bool AddProduct(CreateProductPostRequest newProduct)
 {
     try
     {
         ecom_Products product = new ecom_Products()
         {
             ProductCode       = newProduct.ProductCode,
             Name              = newProduct.Name,
             Price             = newProduct.Price,
             Quantity          = newProduct.Quantity,
             Unit              = newProduct.Unit,
             BrandId           = newProduct.BrandId,
             CoverImageId      = newProduct.CoverImageId,
             Description       = newProduct.Description,
             Description2      = newProduct.Description2,
             Tags              = newProduct.Tags,
             IsNewProduct      = newProduct.IsNewProduct,
             IsBestSellProduct = newProduct.IsBestSellProduct,
             SortOrder         = newProduct.SortOrder,
             Status            = newProduct.Status,
             CreateTy          = newProduct.CreateBy,
             CreatedDate       = DateTime.Now,
             TotalBuy          = 0
         };
         if (newProduct.CategoryId == null)
         {
             product.ecom_Categories = new List <ecom_Categories>();
         }
         else
         {
             var selectedCategories = new HashSet <int>(newProduct.CategoryId);
             var categoriesProduct  = new HashSet <int>(product.ecom_Categories.Select(c => c.Id));
             foreach (var category in categoryRepository.GetAllCategories())
             {
                 if (selectedCategories.Contains(category.Id))
                 {
                     if (!categoriesProduct.Contains(category.Id))
                     {
                         product.ecom_Categories.Add(category);
                     }
                 }
                 else
                 {
                     if (categoriesProduct.Contains(category.Id))
                     {
                         product.ecom_Categories.Remove(category);
                     }
                 }
             }
         }
         share_Images coverImage = imageRepository.GetByID(newProduct.CoverImageId);
         product.share_Images.Add(coverImage);
         db.Insert(product);
         db.Save();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }