Example #1
0
        public ActionResult CreateProduct(Product product)
        {
            var imageList = Session["imageList"] as List <ImageModel>;

            if (ModelState.IsValid && imageList.Count != 0)
            {
                var newProduct = new Product
                {
                    ProductId     = Guid.NewGuid(),
                    CategoryId    = product.CategoryId,
                    CompanyId     = product.CompanyId,
                    Description   = product.Description,
                    Count         = product.Count,
                    Price         = product.Price,
                    Name          = product.Name,
                    PriceOfBuying = product.PriceOfBuying,
                    ProductNumber = product.ProductNumber,
                };
                _productRepository.Save(newProduct);
                foreach (var image in imageList)
                {
                    var photo = new ProductPhoto
                    {
                        ProductPhotoId = Guid.NewGuid(),
                        ProductId      = newProduct.ProductId
                    };

                    var filename      = String.Format("{0}_{1}", photo.ProductPhotoId, image.ImageName);
                    var thumbfilename = String.Format("{0}_thumb_{1}", photo.ProductPhotoId, image.ImageName);

                    var path      = Path.Combine(Server.MapPath("~/Content/images/admin/ProductImage"), filename);
                    var thumbpath = Path.Combine(Server.MapPath("~/Content/images/admin/ProductImage"), thumbfilename);

                    photo.Photo = filename;


                    Image imagePhotoVert = image.Image;
                    using (var imagePhoto = ImageResize.ScaleByPercent(imagePhotoVert, 70))
                    {
                        imagePhoto.Save(path);
                    }

                    var i = imageList.IndexOf(image);
                    if (i == 0)
                    {
                        using (var thumbimagePhoto = ImageResize.Crop(imagePhotoVert, 70, 70, AnchorPosition.Center))
                        {
                            thumbimagePhoto.Save(thumbpath);
                            photo.ThumbnailPhoto = thumbfilename;
                        }
                    }
                    _productPhotoRepository.Save(photo);
                }
            }
            return(View());
        }