public async Task UpdateIngredientLine()
        {
            //Arrange
            IngredientLine ingredientLine = new IngredientLine();

            ingredientLine.Id           = 0;
            ingredientLine.RecipeId     = 1;
            ingredientLine.IngredientId = 1;
            ingredientLine.Amount       = 2;
            ingredientLine.MeasureUnit  = MeasureUnit.Stk;

            //Act
            var addResult = await ingredientLineRepository.AddAsync(ingredientLine);

            addResult.Amount      = 5;
            addResult.MeasureUnit = MeasureUnit.Kg;
            var updateResult = await ingredientLineRepository.UpdateAsync(addResult);

            //Assert
            Assert.AreEqual(1, updateResult);
            var getResult = await ingredientLineRepository.GetByIdAsync(addResult.Id);

            Assert.AreEqual(5, getResult.Amount);
            Assert.AreEqual(MeasureUnit.Kg, getResult.MeasureUnit);
        }
        public async Task DeleteIngredientLine()
        {
            //Arrange
            IngredientLine ingredientLine = new IngredientLine();

            ingredientLine.Id           = 0;
            ingredientLine.RecipeId     = 1;
            ingredientLine.IngredientId = 1;
            ingredientLine.Amount       = 2;
            ingredientLine.MeasureUnit  = MeasureUnit.Stk;

            //Act
            var addResult = await ingredientLineRepository.AddAsync(ingredientLine);

            //Assert
            Assert.IsNotNull(addResult);
            var deleteResult = await ingredientLineRepository.DeleteAsync(addResult.Id);

            Assert.AreEqual(1, deleteResult);

            try
            {
                var getResult = await ingredientLineRepository.GetByIdAsync(addResult.Id);
            }
            catch (InvalidOperationException)
            {
                //Success.
            }
            catch (Exception)
            {
                //Failure.
                //This assert statement ensures failure.
                Assert.AreEqual(1, 2);
            }
        }
예제 #3
0
        public async Task <RecipeDTO> AddAsync(RecipeDTO recipe)
        {
            //Validate recipe
            Validation(recipe);

            //Create recipe in DB
            var newRecipe = await dbAccess.Recipes.AddAsync(new Recipe()
            {
                Id          = 0,
                Title       = recipe.Title,
                Slug        = String.Format("{0}-{1}", recipe.User.Id, SlugHelper.GenerateSlug(recipe.Title)),
                Instruction = recipe.Instruction,
                UserId      = recipe.User.Id,
                CreatedAt   = DateTime.Now
            });

            //Create ingredientlines
            foreach (var ing in recipe.IngredientLines)
            {
                //Get or create the required ingredient
                Ingredient ingredient = (await dbAccess.Ingredients.FindByCondition(nameof(ing.Ingredient.Name), ing.Ingredient.Name)).FirstOrDefault();
                if (ingredient == null)
                {
                    var ingredientToBeCreated = new Ingredient();
                    ingredientToBeCreated.Id   = 0;
                    ingredientToBeCreated.Name = ing.Ingredient.Name;

                    ingredient = await dbAccess.Ingredients.AddAsync(ingredientToBeCreated);
                }
                //Create the ingredientline
                IngredientLine ingredientLine = new IngredientLine();
                ingredientLine.Id           = 0;
                ingredientLine.RecipeId     = newRecipe.Id;
                ingredientLine.IngredientId = ingredient.Id;
                ingredientLine.Amount       = ing.Amount;
                ingredientLine.MeasureUnit  = ConvertToEnum(ing.MeasureUnitText);

                var newIngredientLine = await dbAccess.IngredientLines.AddAsync(ingredientLine);
            }
            //Create images (if any)
            if (recipe.Images != null)
            {
                foreach (var image in recipe.Images)
                {
                    var imageToBeCreated = new Image();
                    imageToBeCreated.Id       = 0;
                    imageToBeCreated.RecipeId = newRecipe.Id;
                    imageToBeCreated.FileName = image.FileName;

                    var newImage = await dbAccess.Images.AddAsync(imageToBeCreated);
                }
            }

            return(await GetByIdAsync(newRecipe.Id));
        }
 public InsertPageViewModel()
 {
     RecipeVM = new Recipe
     {
         Lines = new List <IngredientLine>()
     };
     CollectLine = new List <IngredientLineDetail>();
     Line        = new IngredientLine();
     Ingredient  = new List <Ingredient>();
     Measure     = new List <Measure>();
 }
        public async Task InsertNewIngredientLine()
        {
            //Arrange
            IngredientLine ingredientLine = new IngredientLine();

            ingredientLine.Id           = 0;
            ingredientLine.RecipeId     = 1;
            ingredientLine.IngredientId = 1;
            ingredientLine.Amount       = 2;
            ingredientLine.MeasureUnit  = MeasureUnit.Stk;

            //Act
            var addResult = await ingredientLineRepository.AddAsync(ingredientLine);

            //Assert
            Assert.IsNotNull(addResult);
            Assert.AreEqual(ingredientLine.RecipeId, addResult.RecipeId);
            Assert.AreEqual(ingredientLine.IngredientId, addResult.IngredientId);
            Assert.AreEqual(ingredientLine.Amount, addResult.Amount);
            Assert.AreEqual(ingredientLine.MeasureUnit, addResult.MeasureUnit);
        }
예제 #6
0
        private void UpdateIngredientLine(RecipeDTO dto, List <IngredientLine> existing)
        {
            //Add ingredient - Identify ingredientLine to be created
            List <IngredientLineDTO> toBeCreated = new List <IngredientLineDTO>();

            foreach (var item in dto.IngredientLines)
            {
                if (existing.Any(ex => ex.Id == item.Id))
                {
                    //Nothing happens!
                }
                else
                {
                    toBeCreated.Add(item);
                }
            }

            //Update ingredient - Identify ingredientLine to be updated
            List <IngredientLineDTO> toBeUpdated = new List <IngredientLineDTO>();

            foreach (var item in dto.IngredientLines)
            {
                if (existing.Any(ex => ex.Id == item.Id))
                {
                    toBeUpdated.Add(item);
                }
                //else - Nothing happens!
            }

            //Delete ingredient - Identify ingredientLine to be deleted
            //Type is not DTO, as we delete from the database
            List <IngredientLine> toBeDeleted = new List <IngredientLine>();

            foreach (var e in existing)
            {
                if (toBeCreated.Any(ex => ex.Id == e.Id) || toBeUpdated.Any(ex => ex.Id == e.Id))
                {
                    //Nothing happens!
                }
                else
                {
                    toBeDeleted.Add(e);
                }
            }

            //Create new ingredientLine
            foreach (var item in toBeCreated)
            {
                //Get or create the required ingredient
                Ingredient ingredient = dbAccess.Ingredients.FindByCondition(nameof(Ingredient.Name), item.Ingredient.Name).Result.ToList().FirstOrDefault();
                if (ingredient == null)
                {
                    var ingredientToBeCreated = new Ingredient();
                    ingredientToBeCreated.Id   = 0;
                    ingredientToBeCreated.Name = item.Ingredient.Name;

                    ingredient = dbAccess.Ingredients.AddAsync(ingredientToBeCreated).Result;
                }

                //Create new ingredientLine
                IngredientLine il = new IngredientLine();
                il.Id           = 0;
                il.RecipeId     = dto.Id;
                il.IngredientId = ingredient.Id;
                il.Amount       = item.Amount;
                il.MeasureUnit  = ConvertToEnum(item.MeasureUnitText);

                //Send to DB
                dbAccess.IngredientLines.AddAsync(il);
            }

            //Update existing ingredientLine
            foreach (var item in toBeUpdated)
            {
                IngredientLine il = dbAccess.IngredientLines.GetByIdAsync(item.Id).Result;

                il.Amount      = item.Amount;
                il.MeasureUnit = ConvertToEnum(item.MeasureUnitText);

                //Send to DB
                dbAccess.IngredientLines.UpdateAsync(il);
            }

            //Delete existing ingredientLine
            foreach (var item in toBeDeleted)
            {
                dbAccess.IngredientLines.DeleteAsync(item.Id);
            }
        }
예제 #7
0
        public async Task <int> UpdateAsync(RecipeDTO recipeDTO)
        {
            Recipe recipe = await dbAccess.Recipes.GetByIdAsync(recipeDTO.Id);

            recipe.Title       = recipeDTO.Title;
            recipe.Instruction = recipeDTO.Instruction;
            recipe.RowVer      = recipeDTO.RowVer; // convert to byte array

            int updateResult = await dbAccess.Recipes.UpdateAsync(recipe);

            if (updateResult == 0)
            {
                return(0);
            }

            var existingIngredientLines = (await dbAccess.IngredientLines.FindByCondition(nameof(IngredientLine.RecipeId), recipeDTO.Id)).ToList();
            var existingImages          = (await dbAccess.Images.FindByCondition(nameof(Image.RecipeId), recipeDTO.Id)).ToList();

            // delete old ingredient lines on the recipe
            foreach (IngredientLine existingIngredientLine in existingIngredientLines)
            {
                await dbAccess.IngredientLines.DeleteAsync(existingIngredientLine.Id);
            }

            // delete old images on the recipe
            foreach (Image existingImage in existingImages)
            {
                await dbAccess.Images.DeleteAsync(existingImage.Id);
            }

            // loop thru the ingredient lines sent by the user to be inserted
            foreach (IngredientLineDTO ingredientLineDTO in recipeDTO.IngredientLines)
            {
                // check ingredient name exists
                var existingIngredientByName = await dbAccess.Ingredients.FindByCondition("Name", ingredientLineDTO.Ingredient.Name);

                IngredientLine newIngredientLine = new IngredientLine();

                if (existingIngredientByName.Any())
                {
                    // if the ingredient already exists by name, assign the existing ingredient to the ingredient line
                    newIngredientLine.IngredientId = existingIngredientByName.Single().Id;
                }
                else
                {
                    // the ingredient does not exist by name, we create it, get the new IngredientId and assign it to the ingredient line
                    Ingredient newIngredient = new Ingredient();
                    newIngredient.Name = ingredientLineDTO.Ingredient.Name;

                    var res = await dbAccess.Ingredients.AddAsync(newIngredient);

                    newIngredientLine.IngredientId = res.Id;
                }

                newIngredientLine.RecipeId    = recipe.Id;
                newIngredientLine.Amount      = ingredientLineDTO.Amount;
                newIngredientLine.MeasureUnit = (MeasureUnit)ingredientLineDTO.MeasureUnit;

                // saves the data to database
                await dbAccess.IngredientLines.AddAsync(newIngredientLine);
            }

            foreach (ImageDTO imageDTO in recipeDTO.Images)
            {
                Image image = new Image();
                image.FileName = imageDTO.FileName;
                image.RecipeId = recipe.Id;

                await dbAccess.Images.AddAsync(image);
            }

            return(updateResult);
        }