public async Task <ActionResult <Cookbook> > PostCookbook(PostCookbookVM cookbook) { try { var addedCookbook = await _businessL.PostCookbook(cookbook); if (addedCookbook == null) { return(NoContent()); } return(addedCookbook); } catch (Exception e) { _logger.LogError($"Error: Exception thrown in CookbooksController.PostCookbook: {e}"); return(StatusCode(500)); } }
/// <summary> /// Saves a recipe into a user cookbook. /// If the recipe already exist in the cook book, the checkIfExists will return an instance of /// Cookbook and return null/error to the user. /// </summary> /// <param name="cookbook">includes user Id and recipe Id to save to cookbook</param> /// <returns></returns> public async Task <Cookbook> PostCookbook(PostCookbookVM cookbook) { var checkIfExists = _unitOfWork.Cookbooks.GetByUserEager(cookbook.UserId) .Result.ToList().Find(x => x.Recipe.Id == cookbook.RecipeId); if (checkIfExists == null) { var newCookbook = new Cookbook(); newCookbook.User = await _unitOfWork.Users.Get(cookbook.UserId); newCookbook.Recipe = await _unitOfWork.Recipes.GetEager(cookbook.RecipeId); _unitOfWork.Cookbooks.Add(newCookbook); await _unitOfWork.Complete(); return(newCookbook); } return(null); }