public async Task UpdateRecipeAsync(string userId, string recipeId, RecipeForm recipeForm) { try { var recipe = await GetRecipeAsync(recipeId); if (recipe == null) { throw new NoResultsFoundException($"No recipe found for ID: {recipeId}"); } if (userId == recipe.AuthorID) { // To-do: use AutoMapper recipe.Calories = recipeForm.Calories; recipe.ImageReferencePath = recipeForm.ImageReferencePath; recipe.Ingredients = recipeForm.Ingredients; recipe.Name = recipeForm.Name; recipe.PrepTimeMinutes = recipeForm.PrepTimeMinutes; recipe.RootImagePath = recipeForm.RootImagePath; recipe.Steps = recipeForm.Steps; recipe.Tags = recipeForm.Tags; await _dbClient.UpdateRecipeAsync(recipe); } else { throw new UnauthorizedAccessException(); } } catch (Exception) { throw; } }
public async Task <IActionResult> UpdateRecipe(string recipeId, [FromBody] RecipeForm recipeForm) { var authHeaderContents = Request.Headers["Authorization"]; var accessToken = authHeaderContents.ToString().Split(' ')[1]; var uid = await _authClient.GetUid(accessToken); await _recipeClient.UpdateRecipeAsync(uid, recipeId, recipeForm); return(NoContent()); }
public async Task <IActionResult> AddRecipe([FromBody] RecipeForm recipeForm) { var authHeaderContents = Request.Headers["Authorization"]; var accessToken = authHeaderContents.ToString().Split(' ')[1]; var uid = await _authClient.GetUid(accessToken); var recipe = await _recipeClient.CreateRecipeAsync(uid, recipeForm); return(Ok(recipe)); }
public ActionResult AddRecipe(RecipeForm form) { if (ModelState.IsValid) { form.AddFromForm(); return(RedirectToAction("Recipes")); } else { return(View()); } }
public async Task <Recipe> CreateRecipeAsync(string userId, RecipeForm recipeForm) { try { var recipe = new Recipe { AuthorID = userId, Name = recipeForm.Name, Calories = recipeForm.Calories, PrepTimeMinutes = recipeForm.PrepTimeMinutes, Ingredients = recipeForm.Ingredients, Steps = recipeForm.Steps, ImageReferencePath = recipeForm.ImageReferencePath, RootImagePath = recipeForm.RootImagePath }; return(await _dbClient.AddRecipeAsync(recipe)); } catch (Exception) { throw; } }