Example #1
0
 private RecipeData GetRecipeData(IDataLayer.RecipeRevision recipeRevision)
 {
     return(recipeRevision == null ?
            null :
            new RecipeData()
     {
         Name = recipeRevision.Name,
         Description = recipeRevision.Description
     });
 }
Example #2
0
        public void UpdateRecipe(Recipe recipe)
        {
            if (recipe == null || recipe.Data == null || recipe.Id <= 0)
            {
                this._logger.LogWarning("Can't update recipe. Provided data is invalid - null or empty");
                throw new ArgumentNullException();
            }

            var revisions = this._context.RecipeRevisions
                            .Where(i => i.RecipeId == recipe.Id).ToList();

            if (revisions == null)
            {
                this._logger.LogWarning($"No existing recipe revisions for recipe. Id = {recipe.Id}");
                throw new NullReferenceException($"Can't update recipe. No existing recipe revisions");
            }
            ;

            var latestRevision = GetRecipeCurrentRevision(revisions);

            latestRevision.EndDate = DateTimeOffset.UtcNow;
            var dbRecipeRevision = new IDataLayer.RecipeRevision()
            {
                Name        = recipe.Data.Name,
                Description = recipe.Data.Description,
                StartDate   = DateTimeOffset.UtcNow,
                EndDate     = null,
                Version     = latestRevision.Version + 1,
                RecipeId    = latestRevision.RecipeId,
            };

            this._context.RecipeRevisions.Update(latestRevision); //Update latest revision - from now - old
            this._context.RecipeRevisions.Add(dbRecipeRevision);  //Add new revision
            this._context.SaveChanges();
            this._logger.LogInformation("Recipe updated. Id = {0}", recipe.Id);
        }