public ActionResult AddImages(AddProductImageViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return View(new { model.ProductId, model.CategoryName });//TODO REFACTOR!
            }
            var product = this.products.GetById(model.ProductId);
            var productImages = model.images
                                        .AsQueryable()
                                        .To<AddProductImageRequestModel>()
                                        .To<ProductImage>()
                                        .ToList();

            foreach (var pr in productImages)
            {
                pr.ProductId = model.ProductId;
                pr.CategoryName = model.CategoryName;
            }

            this.images.SaveImageFile(productImages);
            this.images.SaveImageRecord(productImages);

            var latestImages = this.images.GetLatest();
            this.products.SaveImagesToProduct(productImages, product);

            return RedirectToAction("Index", "Home", new { area = string.Empty });
        }
Esempio n. 2
0
        public async Task <IActionResult> AddImage(AddProductImageViewModel model)
        {
            if (ModelState.IsValid)
            {
                Product product = await _context.Products.Include(p => p.ProductImages).FirstOrDefaultAsync(p => p.Id == model.ProductId);

                if (product == null)
                {
                    return(NotFound());
                }

                try
                {
                    Guid imageId = await _blobHelper.UploadBlobAsync(model.ImageFile, "products");

                    if (product.ProductImages == null)
                    {
                        product.ProductImages = new List <ProductImage>();
                    }

                    product.ProductImages.Add(new ProductImage {
                        ImageId = imageId
                    });
                    _context.Update(product);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Details", "Products", new { id = product.Id }));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            return(View(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddImage(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Product product = await _context.Products.FindAsync(id);

            AddProductImageViewModel model = new AddProductImageViewModel
            {
                ProductId = product.Id,
            };

            return(View(model));
        }
Esempio n. 4
0
        public JsonResult AddProductImage(AddProductImageViewModel model)
        {
            var currentUser = GetAuthenticatedUser();

            try
            {
                var entity = _context.Product.Single(x => x.Id == model.id);

                if (Request.Files.Count > 0)
                {
                    if (!string.IsNullOrEmpty(entity.FileId))
                    {
                        AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "product");
                    }

                    var fileEntity = AsefianFileContextHelper.Save(Request.Files[0], currentUser.id, GetCurrentIp(), "admin", "product");

                    model.productFileId   = fileEntity.id;
                    model.productFileName = fileEntity.fileName;
                }
                else if (string.IsNullOrEmpty(model.productFileId) && !string.IsNullOrEmpty(entity.FileId))
                {
                    if (!string.IsNullOrEmpty(entity.FileId))
                    {
                        AsefianFileContextHelper.DeleteFile(entity.FileId, entity.FileName, currentUser.id, "admin", "Product");
                    }
                }
                entity.FileId   = model.productFileId;
                entity.FileName = model.productFileName;
                _context.SaveChanges();
                var data = new
                {
                    id       = entity.Id,
                    fileName = entity.FileName,
                    fileId   = entity.FileId
                };
                return(Success("Done", data));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));
            }
        }