public async Task <ActionResult <ProductsImage> > Post(
            [FromBody] ProductsImage model,
            [FromServices] DataContext context
            )
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }



                //Add + Salvar DB
                context.ProductsImage.Add(model);
                await context.SaveChangesAsync();

                return(Ok(model));
            }
            catch (Exception)
            {
                return(BadRequest(new { message = "Não foi possível cadastrar a imagem do produto." }));
            }
        }
        public IResult AddAsync(List <IFormFile> file, ProductsImage productsImage)
        {
            var error = "";
            List <ProductsImage> products = new List <ProductsImage>();
            var imageCount = _productImageDal.GetAll(c => c.ProductId == productsImage.ProductId).Count;

            if (imageCount >= 5)
            {
                return(new ErrorResult("One product must have 5 or less images"));
            }

            for (int i = 0; i < file.Count; i++)
            {
                var newImage = new ProductsImage()
                {
                    ProductId = productsImage.ProductId
                };
                var imageResult = FileHelper.Upload(file[i]);

                if (!imageResult.Success)
                {
                    error = imageResult.Message;
                    break;
                }
                else
                {
                    newImage.ImagePath = imageResult.Message;

                    products.Add(newImage);
                }
            }

            _productImageDal.MultiAddAsync(products.ToArray());
            return(new SuccessResult("Product image added"));
        }
        public async Task <ActionResult <ProductsImage> > Put(
            int id,
            [FromBody] ProductsImage model,
            [FromServices] DataContext context)
        {
            try
            {
                //validar id produto passado
                if (id != model.Id)
                {
                    return(NotFound(new { message = "Nenhuma imagem encontrada" }));
                }

                //Valida model
                if (!ModelState.IsValid)
                {
                    return(BadRequest(model));
                }

                //Update DB
                context.Entry <ProductsImage>(model).State = EntityState.Modified;
                await context.SaveChangesAsync();

                return(Ok(model));
            }
            catch (DbUpdateConcurrencyException)
            {
                return(BadRequest(new { message = "Esse registro já foi atualizado" }));
            }
            catch (Exception)
            {
                return(BadRequest(new { message = "Não foi possível atualizar a imagem do produto" }));
            }
        }
Beispiel #4
0
        public ActionResult Save(Products products, HttpPostedFileBase productImageUpload)//handle the form data AND the image upload
        {
            if (!ModelState.IsValid)
            {
                return(View("Index"));
            }

            if (productImageUpload != null && productImageUpload.ContentLength > 0)//check to see if we actually uploaded a movie image
            {
                var image = new ProductsImage
                {
                    ImageName   = Path.GetFileName(productImageUpload.FileName),
                    ContentType = productImageUpload.ContentType
                };
                using (var reader = new System.IO.BinaryReader(productImageUpload.InputStream))
                {
                    image.ImageData = reader.ReadBytes(productImageUpload.ContentLength);
                }
                _context.ProductsImage.Add(image); //save the new image to the DB
                _context.SaveChanges();

                products.ProductsImage    = image;
                products.ProductsImagesId = image.Id;
            }

            if (products.Id == 0)//this is a new movie
            {
                _context.Products.Add(products);
            }
            else//we are updating an existing movie
            {
                //pull the existing movie out of the DB and include the movie's image
                var productInDb = _context.Products.Include(p => p.ProductsImagesId).Single(p => p.Id == products.Id);
                productInDb.Name         = products.Name;
                productInDb.Price        = products.Price;
                productInDb.Description  = products.Description;
                productInDb.CategoriesId = products.CategoriesId;
                productInDb.Quantity     = products.Quantity;
                if (products.ProductsImagesId != null && products.ProductsImagesId != 0)
                {
                    //delete the previous image in the DB first (if one exists)
                    if (productInDb.ProductsImage != null)
                    {
                        _context.ProductsImage.Remove(productInDb.ProductsImage);
                        _context.SaveChanges();
                    }
                    //assign new movie image to existing movie that we pulled out of the DB
                    productInDb.ProductsImage    = products.ProductsImage;
                    productInDb.ProductsImagesId = products.ProductsImagesId;
                }
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Products"));
        }
        public IActionResult Delete(ProductsImage image)
        {
            var result = _imagesService.Delete(image);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
        public IActionResult Add([FromForm(Name = ("Image"))] List <IFormFile> file, [FromForm] ProductsImage images)
        {
            var result = _imagesService.AddAsync(file, images);

            if (result.Success)
            {
                return(Ok(result));
            }

            return(BadRequest(result));
        }
        private IResult CarImageDelete(ProductsImage carImage)
        {
            try
            {
                File.Delete(carImage.ImagePath);
            }
            catch (Exception exception)
            {
                return(new ErrorResult(exception.Message));
            }

            return(new SuccessResult());
        }
Beispiel #8
0
        private static void SaveImage(Product product)
        {
            Console.WriteLine(product.ProductFullName);
            var newProductImage = new ProductsImage
            {
                ImageChecked = false,
                ImageHost    = imageHost,
                LastModified = DateTime.Now
            };

            d.ProductsImages.InsertOnSubmit(newProductImage);
            d.SubmitChanges();
            imageHost = "";
            var imageId = d.ProductsImages.Max(@t => @t.ImageID);

            // gen filePath
            var _filePath = homeFolder + imageId + ".jpeg";

            var fi = new FileInfo(homeFolder + "temp.jpeg");

            if (fi.Exists)
            {
                var fi2 = new FileInfo(_filePath);
                if (fi2.Exists)
                {
                    fi2.Delete();
                }
                fi.MoveTo(_filePath);
                d.SetProductImage(product.SiteProductId, imageId);
            }
            else
            {
                d.ProductsImages.DeleteAllOnSubmit(d.ProductsImages.Where(t => t.ImageID == imageId));
                d.SubmitChanges();
            }
        }
 public IResult Update(IFormFile file, ProductsImage productsImage)
 {
     _productImageDal.UpdateAsync(productsImage);
     return(new SuccessResult());
 }
 public IResult Delete(ProductsImage productsImage)
 {
     _productImageDal.DeleteAsync(productsImage);
     return(new SuccessResult());
 }
 public IResult Add(IFormFile file, ProductsImage entity)
 {
     throw new NotImplementedException();
 }
Beispiel #12
0
        private static void SaveImage(Product product)
        {
            Console.WriteLine(product.ProductFullName);
            var newProductImage = new ProductsImage
            {
                ImageChecked = false,
                ImageHost = imageHost,
                LastModified = DateTime.Now
            };
            d.ProductsImages.InsertOnSubmit(newProductImage);
            d.SubmitChanges();
            imageHost = "";
            var imageId = d.ProductsImages.Max(@t => @t.ImageID);

            // gen filePath
            var _filePath = homeFolder + imageId + ".jpeg";

            var fi = new FileInfo(homeFolder + "temp.jpeg");
            if (fi.Exists)
            {
                var fi2 = new FileInfo(_filePath);
                if (fi2.Exists) fi2.Delete();
                fi.MoveTo(_filePath);
                d.SetProductImage(product.SiteProductId, imageId);
            }
            else
            {
                d.ProductsImages.DeleteAllOnSubmit(d.ProductsImages.Where(t => t.ImageID == imageId));
                d.SubmitChanges();
            }
        }