public async Task <IActionResult> Create(ProductCreateInputModel productCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(productCreateInputModel));
            }

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

            string pictureUrl = await this.cloudinaryService
                                .UploadPictureAsync(productCreateInputModel.ImageFormFile, productCreateInputModel.Name);

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

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


            return(this.Redirect("All"));
        }
Ejemplo n.º 2
0
        public async Task <bool> CreateAsync(ProductCreateInputModel productServiceModel)
        {
            if (productServiceModel.Name == null ||
                productServiceModel.Description == null ||
                productServiceModel.Price == 0 ||
                productServiceModel.Price < 0 ||
                productServiceModel.Picture == null)
            {
                throw new ArgumentNullException("One or more required properties are null");
            }

            var product = productServiceModel.To <Product>();

            await this.productRepository.AddAsync(product);

            var result = await this.productRepository.SaveChangesAsync();

            return(result > 0);
        }