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 })); }
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")); } }
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")); } }
public async Task<IActionResult> OnPost(int recipeId) { var recipe = await repository.GetRecipeAsync(recipeId); if (recipe == null) return RedirectToPage("./NotFound"); repository.Delete(recipe); await repository.SaveChangesAsync(); TempData["Message"] = $"{recipe.Name} deleted."; return RedirectToPage("./List"); }
public async Task <IActionResult> OnPost(int productId) { var product = await repository.GetProductAsync(productId); if (product == null) { return(RedirectToPage("./NotFound")); } repository.Delete(product); await repository.SaveChangesAsync(); TempData["Message"] = $"{product.Name} deleted."; return(RedirectToPage("./List")); }
public async Task <IActionResult> OnPost(IFormFile image, int recipeId) { if (image != null) { using (var stream = image.OpenReadStream()) { var imageId = await imageStore.SaveImage(stream); var recipeInDb = await repository.GetRecipeAsync(recipeId); recipeInDb.PhotoPath = imageStore.UriFor(imageId); await repository.SaveChangesAsync(); return(RedirectToPage("./Details", new { recipeId = recipeInDb.Id })); } } return(RedirectToPage("./NotFound")); }