Beispiel #1
0
        public async Task <IActionResult> OnPost(IFormFile image)
        {
            if (!ModelState.IsValid)
            {
                Categories = htmlHelper.GetEnumSelectList <CategoryType>();
                return(Page());
            }

            if (Product.Id > 0)
            {
                var productInDb = await repository.GetProductAsync(Product.Id);

                if (productInDb == null)
                {
                    return(RedirectToPage("./NotFound"));
                }

                mapper.Map(Product, productInDb);
                await repository.SaveChangesAsync();

                if (image != null)
                {
                    using (var stream = image.OpenReadStream())
                    {
                        var imageId = await imageStore.SaveImage(stream);

                        productInDb.PhotoPath = imageStore.UriFor(imageId);
                        await repository.SaveChangesAsync();
                    }
                }
                TempData["Message"] = "Product updated!";
            }
            else
            {
                if (image != null)
                {
                    using (var stream = image.OpenReadStream())
                    {
                        var imageId = await imageStore.SaveImage(stream);

                        Product.PhotoPath = imageStore.UriFor(imageId);
                        await repository.SaveChangesAsync();
                    }
                }
                repository.Add(Product);
                await repository.SaveChangesAsync();

                TempData["Message"] = "Product saved!";
            }

            return(RedirectToPage("./Details", new { productId = Product.Id }));
        }
Beispiel #2
0
        public async Task <ActionResult <ProductDto> > Post([FromBody] ProductDto productDto)
        {
            repository.Add(mapper.Map <Product>(productDto));

            if (await repository.SaveChangesAsync())
            {
                return(CreatedAtAction("GetProduct", new { id = productDto.Id }, productDto));
            }
            else
            {
                return(BadRequest("Failed to save the product"));
            }
        }
Beispiel #3
0
        public async Task <ActionResult <RecipeDto> > Post([FromBody] RecipeDto recipeDto)
        {
            var existingRecipes = await repository.GetAllRecipesAsync();

            var isNameTaken = existingRecipes.Any(r => r.Name == recipeDto.Name);

            if (isNameTaken)
            {
                return(BadRequest("This name of recipe is already taken."));
            }

            var newRecipe = mapper.Map <Recipe>(recipeDto);

            var products = await repository.GetAllProductsAsync();

            for (int i = 0; i < recipeDto.Ingredients.Length; i++)
            {
                var currentProductName = recipeDto.Ingredients[i].ProductName;
                var currentProduct     = products.Where(p => p.Name == currentProductName).FirstOrDefault();

                if (currentProduct != null)
                {
                    newRecipe.Ingredients[i].Product = currentProduct;
                }
                else
                {
                    return(BadRequest($"There is no product with name {currentProductName}."));
                }
            }

            repository.Add(newRecipe);

            if (await repository.SaveChangesAsync())
            {
                return(CreatedAtAction("GetRecipe", new { id = newRecipe.Id }, mapper.Map <RecipeDto>(newRecipe)));
            }
            else
            {
                return(BadRequest("Failed to save the recipe"));
            }
        }