Beispiel #1
0
        public ActionResult CreateProduct()
        {
            ProductPL product = new ProductPL();

            product.Categories = new SelectList(_categoryService.GetAll().ToList(), "Id", "Name");

            return(View(product));
        }
Beispiel #2
0
        public bool ImageValidation(HttpPostedFileBase imageUpload, ProductPL productPL)
        {
            bool result = false;

            if (imageUpload != null && imageUpload.ContentLength > 0)
            {
                string extention = imageUpload.ContentType.ToLower();

                if (extention != "image/jpg" &&
                    extention != "image/jpeg" &&
                    extention != "image/gif" &&
                    extention != "image/gif")
                {
                    productPL.Categories = new SelectList(_categoryService.GetAll().ToList(), "Id", "Name");

                    return(result);
                }
                result = true;

                return(result);
            }

            return(result);
        }
Beispiel #3
0
        public ActionResult EditProduct(ProductPL productPL, HttpPostedFileBase imageUpload)
        {
            productPL.Categories = new SelectList(_categoryService.GetAll().ToList(), "Id", "Name");

            productPL.GalleryImage = Directory
                                     .EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/Thumbs/"))
                                     .Select(x => Path.GetFileName(x));

            if (!ModelState.IsValid)
            {
                return(View(productPL));
            }

            if (_productService.GetAll().Where(x => x.Id != productPL.Id).Any(x => x.Name == productPL.Name))
            {
                ModelState.AddModelError("", "Product with such name is already existing in the system");

                return(View(productPL));
            }

            var productBL = _mapper.Map <ProductBL>(productPL);

            _productService.Update(productBL);

            TempData["ProductEditedSuccess"] = "The product has been edited";
            if (imageUpload == null)
            {
                TempData["ProductEditedWOImage"] = "Please pay  attention. The image wasn't modified ";
            }
            else if (ImageValidation(imageUpload, productPL))
            {
                var imageDirectory = new DirectoryInfo(string.Format($"{Server.MapPath(@"\")}Images\\Uploads"));

                var pathToImg   = Path.Combine(imageDirectory.ToString(), "Products\\");
                var pathToThumb = Path.Combine(imageDirectory.ToString(), "Products\\Thumbs");

                DirectoryInfo ptImg   = new DirectoryInfo(pathToImg);
                DirectoryInfo ptThumb = new DirectoryInfo(pathToThumb);

                foreach (var fileImg in ptImg.GetFiles())
                {
                    fileImg.Delete();
                }

                foreach (var fileTmb in ptThumb.GetFiles())
                {
                    fileTmb.Delete();
                }

                string imageName = imageUpload.FileName;

                productBL.ImagePath = imageName;

                _productService.Update(productBL);

                var originalImgPath = string.Format($"{pathToImg}\\{imageName}");
                var thumbImgPath    = string.Format($"{pathToThumb}\\{imageName}");

                imageUpload.SaveAs(originalImgPath);

                WebImage img = new WebImage(imageUpload.InputStream);
                img.Resize(150, 150);
                img.Save(thumbImgPath);
            }
            else
            {
                TempData["ProductEditedFailIncImg"] = "Please add images with the next extentions: jpg, jpeg, gif, png.";
            }

            return(RedirectToAction("EditProduct"));
        }
Beispiel #4
0
        public ActionResult CreateProduct(ProductPL productPL, HttpPostedFileBase imageUpload)
        {
            if (!ModelState.IsValid)
            {
                productPL.Categories = new SelectList(_categoryService.GetAll().ToList(), "Id", "Name");

                return(View(productPL));
            }

            if (_productService.GetAll().Any(x => x.Name == productPL.Name))
            {
                productPL.Categories = new SelectList(_categoryService.GetAll().ToList(), "Id", "Name");

                ModelState.AddModelError("", "Such name is already existing in the system");
                return(View(productPL));
            }
            ;

            var productBL = _mapper.Map <ProductBL>(productPL);

            _productService.Add(productBL);

            TempData["CreateProductSuccess"] = "The product has been added.";

            productBL = _productService.GetAll().FirstOrDefault(x => x.Name == productPL.Name);

            if (ImageValidation(imageUpload, productPL))
            {
                var imageDirectory = new DirectoryInfo(string.Format($"{Server.MapPath(@"\")}Images\\Uploads"));

                var pathToImg   = Path.Combine(imageDirectory.ToString(), "Products\\");
                var pathToThumb = Path.Combine(imageDirectory.ToString(), "Products\\Thumbs");

                if (!Directory.Exists(pathToImg))
                {
                    Directory.CreateDirectory(pathToImg);
                }
                if (!Directory.Exists(pathToThumb))
                {
                    Directory.CreateDirectory(pathToThumb);
                }

                string imageName = imageUpload.FileName;

                productBL.ImagePath = imageName;

                _productService.Update(productBL);

                var originalImgPath = string.Format($"{pathToImg}\\{imageName}");
                var thumbImgPath    = string.Format($"{pathToThumb}\\{imageName}");

                imageUpload.SaveAs(originalImgPath);

                WebImage img = new WebImage(imageUpload.InputStream);
                img.Resize(150, 150);
                img.Save(thumbImgPath);
            }
            else
            {
                TempData["ImageWasNotAdded"] = "Please add images with the next extentions: jpg, jpeg, gif, png.";
            }

            return(RedirectToAction("CreateProduct"));
        }