public JsonResult ProductAddPhoto(int id)
        {
            var file     = Request.Files.Get("myfile");
            var uploader = new FileUploader(file);

            var result = new
            {
                filename      = uploader.Name,
                contentType   = uploader.ContentType,
                contentLength = uploader.ContentLength,
                newFilename   = uploader.UniqueName
            };

            try
            {
                uploader.Save("products");
                _productImageRepository.Save(new ProductImage()
                {
                    ProductId = id,
                    Link      = uploader.UniqueName
                });
            }
            catch (Exception)
            {
                return(Json(new { status = "fail" }, "text/html"));
            }
            return(Json(result, "text/html"));
        }
Example #2
0
        public ActionResult Edit(Product product, HttpPostedFileBase ProductImage)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            _productRepository.Update(product);
            _productRepository.Save();

            if (ProductImage == null || ProductImage.ContentLength <= 0)
            {
                return(RedirectToAction("Index"));
            }
            var img = new ProductImage()
            {
                ImageName   = Path.GetFileName(ProductImage.FileName),
                ContentType = ProductImage.ContentType
            };

            using (var reader = new BinaryReader(ProductImage.InputStream))
            {
                img.Content   = reader.ReadBytes(ProductImage.ContentLength);
                img.ProductId = product.ProductId;
            }
            var existingImage = _productRepository.GetById(product.ProductId).ProductImages;

            if (existingImage != null && existingImage.Count > 0)
            {
                existingImage.ForEach(x => _productImagerepository.Delete(x.ProductImageId));
            }
            _productImagerepository.Insert(img);
            _productImagerepository.Save();
            return(RedirectToAction("Index"));
        }
Example #3
0
        public ActionResult Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var           ProductId        = Guid.NewGuid().ToString();
                    List <string> productImageList = ProcessUploadedFile(model);
                    _productRepository.Add(new Product()
                    {
                        Id            = ProductId,
                        ProductCode   = model.ProductCode,
                        ProductName   = model.ProductName,
                        Slug          = model.Slug,
                        Description   = model.Description,
                        ProductTypeId = model.ProductTypeId,
                        MaterialId    = model.MaterialId,
                        CategoryId    = model.CategoryId,
                        PriceType     = int.Parse(model.PriceType),
                        Price         = model.Price
                    });
                    _productRepository.Save(requestContext);

                    if (productImageList != null && productImageList.Count > 0)
                    {
                        foreach (var image in productImageList)
                        {
                            _productImageRepository.Add(new ProductImage()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = ProductId,
                                Url       = image,
                                CreateAt  = DateTime.UtcNow
                            });
                        }
                        _productImageRepository.Save(requestContext);
                    }
                    if (model.TagList != null && model.TagList.Count > 0)
                    {
                        foreach (var item in model.TagList)
                        {
                            _productTagRepository.Add(new ProductTag()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = ProductId,
                                TagId     = item
                            });
                        }
                    }
                    _productTagRepository.Save(requestContext);
                }
                catch (Exception)
                {
                    SetComboData();
                    return(View());
                }
                return(RedirectToAction("Index"));
            }
            SetComboData();
            return(View());
        }
        public async Task <IActionResult> Create(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var           productId        = Guid.NewGuid().ToString();
                    List <string> productImageList = ProcessUploadedFile(model);
                    await _productRepository.AddAsync(new Product
                    {
                        Id            = productId,
                        ProductCode   = model.ProductCode,
                        ProductName   = model.ProductName,
                        Slug          = model.Slug,
                        Description   = model.Description,
                        ProductTypeId = model.ProductTypeId,
                        MaterialId    = model.MaterialId,
                        CategoryId    = model.CategoryId,
                        PriceType     = int.Parse(model.PriceType),
                        Price         = model.Price,
                        IsFeatured    = model.IsFeatured,
                        IsNew         = model.IsNew,
                        Actived       = model.Actived,
                        Discount      = model.Discount,
                        ExtraDiscount = model.ExtraDiscount,
                        SeoAlias      = TextHelper.ToUnsignString(model.ProductName),
                        PriceAfter    = Math.Round((double)((1 - model.Discount / 100 - model.ExtraDiscount / 100) * model.Price.GetValueOrDefault()), 1,
                                                   MidpointRounding.AwayFromZero)
                    });

                    await _productRepository.SaveAsync(RequestContext);

                    _cache.Remove("CACHE_MASTER_PRODUCT");
                    if (productImageList != null && productImageList.Count > 0)
                    {
                        foreach (var image in productImageList)
                        {
                            _productImageRepository.Add(new ProductImage()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = productId,
                                Url       = image,
                                CreateAt  = DateTime.UtcNow
                            });
                        }
                        _productImageRepository.Save(RequestContext);
                    }
                    if (model.TagList != null && model.TagList.Count > 0)
                    {
                        foreach (var item in model.TagList)
                        {
                            _productTagRepository.Add(new ProductTag()
                            {
                                Id        = Guid.NewGuid().ToString(),
                                ProductId = productId,
                                TagId     = item
                            });
                        }
                    }
                    _productTagRepository.Save(RequestContext);
                }
                catch (Exception)
                {
                    SetComboData();
                    ViewBag.ProductCode = HttpContext.Session.GetString("ProductCode");
                    return(View());
                }
                HttpContext.Session.Remove("ProductCode");
                return(RedirectToAction("Index"));
            }
            SetComboData();
            ViewBag.ProductCode = HttpContext.Session.GetString("ProductCode");
            return(View());
        }