public async Task <ParserResult> ParseFull(string input) { var output = RecipeParser.Parse(input); var recipeHistory = new RecipeHistory { FullText = input, CreatedBy = await SecurityService.GetCurrentUserName(), ModifiedBy = await SecurityService.GetCurrentUserName(), Version = 1 }; var history = RecipeHistoryRepository.Create(recipeHistory); await RecipeHistoryRepository.UpdateSearchIndex(history.ID); output.Output.FullTextReference = history.ID; return(output); }
public async Task <Recipe> CreateAsync(Recipe recipe) { if (string.IsNullOrEmpty(recipe.Notes)) { recipe.Notes = "No Notes Yet."; } // TODO: Extract this out to a function of its own. if (string.IsNullOrEmpty(recipe.CreatedBy) || string.IsNullOrEmpty(recipe.ModifiedBy)) { recipe.CreatedBy = await SecurityService.GetCurrentUserName(); recipe.ModifiedBy = await SecurityService.GetCurrentUserName(); } var existingRecipe = await RecipeRepository.GetByKeyAsync(recipe.Key); if (existingRecipe != null && existingRecipe.CreatedBy == recipe.CreatedBy) { throw new BistroFiftyTwoDuplicateRecipeException( "A recipe with that key already exists. Edit the existing recipe instead."); //TODO: Once we have update, call update instead. } var createdRecipe = await RecipeRepository.CreateAsync(recipe); var recipeIngredientTasks = new List <Task <RecipeIngredient> >(); var recipeStepTasks = new List <Task <Step> >(); recipe.Ingredients.ToList().ForEach(async r => { r.RecipeId = createdRecipe.ID; if (string.IsNullOrEmpty(r.Notes)) { r.Notes = $"Notes for ingredient {r.Ordinal}"; } if (string.IsNullOrEmpty(r.Units)) { r.Units = "items"; } if (string.IsNullOrEmpty(r.CreatedBy) || string.IsNullOrEmpty(r.ModifiedBy)) { r.CreatedBy = await SecurityService.GetCurrentUserName(); r.ModifiedBy = await SecurityService.GetCurrentUserName(); } await RecipeIngredientRepository.CreateAsync(r); }); recipe.Steps.ToList().ForEach(async s => { s.RecipeId = createdRecipe.ID; if (string.IsNullOrEmpty(s.CreatedBy) || string.IsNullOrEmpty(s.ModifiedBy)) { s.CreatedBy = await SecurityService.GetCurrentUserName(); s.ModifiedBy = await SecurityService.GetCurrentUserName(); } await StepRepository.CreateAsync(s); }); // set the historical version correctly. - may need to do more work here for revisions, set version correctly. var recipeHistory = await RecipeHistoryRepository.GetAsync(recipe.FullTextReference); recipeHistory.RecipeID = createdRecipe.ID; await RecipeHistoryRepository.UpdateAsync(recipeHistory); // pull the recipe from the db which also will populate the cache. return(await GetByIdAsync(createdRecipe.ID)); }