public async Task <IActionResult> Edit(ProductEditInputModel productEditInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            ProductServiceModel productServiceModel = productEditInputModel.To <ProductServiceModel>();

            if (productEditInputModel.ImageFormFile != null)
            {
                string pictureUrl = await this.cloudinaryService
                                    .UploadPictureAsync(productEditInputModel.ImageFormFile, productEditInputModel.Name);

                ProductServiceModel productFromDb = await this.productService.EditAsync(productServiceModel);

                await this.imageService.CreateWithProductAsync(pictureUrl, productFromDb.Id);

                return(this.RedirectToAction("All", "Products"));
            }

            await this.productService.EditAsync(productServiceModel);

            return(this.RedirectToAction("All", "Products"));
        }
Esempio n. 2
0
        public async Task <bool> EditProductAsync(ProductEditInputModel productEditInputModel)
        {
            var product = await this.productRepository.All().SingleOrDefaultAsync(x => x.Id == productEditInputModel.Id);

            if (product == null || productEditInputModel.Price <= 0)
            {
                throw new ArgumentNullException("Product was null or price was equal or less than zero !");
            }

            if (product.Name != productEditInputModel.Name &&
                await this.ProductNameIsNotUnique(productEditInputModel.Name))
            {
                return(false);
            }

            if (productEditInputModel.PictureFile != null)
            {
                var pictureUrl = await this.cloudinaryService.UploadPictureAsync(
                    productEditInputModel.PictureFile,
                    productEditInputModel.Name,
                    GlobalConstants.CloudinaryProductPictureFolder);

                product.Picture = pictureUrl;
            }

            if (productEditInputModel.Price < product.Price)
            {
                product.OldPrice = product.Price;
            }

            productEditInputModel.To(product);

            await this.productRepository.SaveChangesAsync();

            return(true);
        }