示例#1
0
        public async Task AddOrUpdateIngrediantToRecipeAsync(AlterIngrediantViewModel model)
        {
            try
            {
                if (model == null)
                {
                    throw new ArgumentNullException(nameof(model));
                }

                var ingrediant = await Repository.FindAsync(model.IngrediantId, x => x.Recipes);

                if (ingrediant == null)
                {
                    throw new DataObjectNotFoundException($"No Ingrediant with the id {model.IngrediantId} found");
                }
                var recipeIngrediant = ingrediant.Recipes.FirstOrDefault(x => x.RecipeId.Equals(model.RecipeId));
                if (recipeIngrediant == null)
                {
                    ingrediant.Recipes.Add(new RecipeIngrediant()
                    {
                        RecipeId     = model.RecipeId,
                        Amount       = model.Amount,
                        CookingUnit  = model.Unit,
                        IngrediantId = ingrediant.Id
                    });
                }
                else
                {
                    recipeIngrediant.CookingUnit = model.Unit;
                    recipeIngrediant.Amount      = model.Amount;
                }
                await Repository.SaveChangesAsync();

                Logger.LogDebug($"Recipe '{model.RecipeId}': Changed Ingrediant '{ingrediant.Name}({ingrediant.Id}) successfully'");
            }
            catch (Exception e)
            {
                var message = String.Concat("Error occured on altering ", (model?.IngrediantId.ToString() ?? "an ingrediant"), " for recipe ", (model?.RecipeId.ToString() ?? "a recipe"));
                Logger.LogError(new EventId(), e, message);
                throw new Exception(message, e);
            }
        }
示例#2
0
        public async Task AddIngrediantToRecipe()
        {
            await InitializeAsync();

            using (var service = GetService())
            {
                var recipe = await DbContext
                             .Recipes
                             .AsNoTracking()
                             .Include(x => x.Ingrediants)
                             .FirstAsync();

                var newIngrediant = await DbContext
                                    .Ingrediants
                                    .AsNoTracking()
                                    .Include(x => x.Recipes)
                                    .FirstAsync(x => x.Recipes
                                                .All(y => y.RecipeId != recipe.Id));

                var alterModel = new AlterIngrediantViewModel()
                {
                    RecipeId     = recipe.Id,
                    IngrediantId = newIngrediant.Id,
                    Amount       = 1,
                    Unit         = CookingUnit.Gramm
                };
                await service.AddOrUpdateIngrediantToRecipeAsync(alterModel);

                var newRecipe = await DbContext
                                .Recipes
                                .AsNoTracking()
                                .Include(x => x.Ingrediants)
                                .FirstAsync(x => x.Id.Equals(recipe.Id));

                Assert.Equal(recipe.Ingrediants.Count + 1, newRecipe.Ingrediants.Count);
                Assert.False(recipe.Ingrediants.Any(x => x.IngrediantId.Equals(newIngrediant.Id)));
                Assert.True(newRecipe.Ingrediants.Any(x => x.IngrediantId.Equals(newIngrediant.Id)));
            }
        }
示例#3
0
 /// <summary>
 /// Adds or updates an ingrediant of a recipe. If the ingrediant with the given id is not available within the recipe it will be addded.
 /// </summary>
 /// <param name="recipeId">Id of the recipe</param>
 /// <param name="model">new or updated data</param>
 /// <returns></returns>
 public async Task AddOrUpdateIngrediantOfRecipe(int recipeId, AlterIngrediantViewModel model)
 {
     await this.Client.PutAsJsonAsync(model, $"Recipes/{recipeId}/Ingrediants");
 }
示例#4
0
        public async Task <IActionResult> AddIngrediantToRecipeAsync(int recipeId, [FromBody] AlterIngrediantViewModel model)
        {
            await _service.AddOrUpdateIngrediantToRecipeAsync(model);

            return(Ok());
        }