Exemplo n.º 1
0
        public async Task <IActionResult> Create(ProductCreateViewModel user_data)
        {
            IFormFile image         = user_data.Image;
            var       imageErrorMsg = new StringBuilder();

            if (ModelState.IsValid &&
                image.Length > 0 &&
                (_fileHandler.CheckContentType(image, new List <string> {
                "image/jpg", "image/jpeg", "image/svg+xml", "image/png"
            }, imageErrorMsg) &
                 _fileHandler.CheckSize(image, 2 * StorageUnits.Megabyte, imageErrorMsg)))
            {
                string imagePath = _fileHandler.Upload(image);

                var product = new Product
                {
                    Name     = user_data.Name,
                    Price    = user_data.Price,
                    Quantity = user_data.Quantity,
                    Image    = imagePath
                };

                await _context.AddAsync(product);

                _context.SaveChanges();

                return(RedirectToRoute("product-index"));
            }

            ModelState.AddModelError("image", imageErrorMsg.ToString());

            return(View(user_data));
        }