示例#1
0
 private void UpdateIngredientAmounts(RecipeEntity recipeEntity, IEnumerable <RecipeUpdateIngredientModel> recipeUpdateIngredientModelsToUpdate)
 {
     foreach (var recipeUpdateIngredientModel in recipeUpdateIngredientModelsToUpdate)
     {
         var ingredientAmountEntity =
             ingredientAmountRepository.GetByRecipeIdAndIngredientId(recipeEntity.Id,
                                                                     recipeUpdateIngredientModel.IngredientId);
         mapper.Map(recipeUpdateIngredientModel, ingredientAmountEntity);
         ingredientAmountRepository.Update(ingredientAmountEntity);
     }
 }
示例#2
0
        public void Add_Recipe_RecipeAdded()
        {
            //Arrange
            var entity = new RecipeEntity
            {
                Name        = "Lemonade",
                Description = "Sweet Summer Refreshing Lemonade",
                FoodType    = FoodType.Drink,
                Duration    = TimeSpan.FromMinutes(1),
                Ingredients = new []
                {
                    new IngredientAmountEntity
                    {
                        Amount     = 1,
                        Unit       = Unit.L,
                        Ingredient = new IngredientEntity
                        {
                            Name        = "Water",
                            Description = "Mineral"
                        }
                    },
                    new IngredientAmountEntity
                    {
                        Amount     = 100,
                        Unit       = Unit.G,
                        Ingredient = new IngredientEntity
                        {
                            Name        = "Sugar",
                            Description = "Brown"
                        }
                    },
                    new IngredientAmountEntity
                    {
                        Amount     = 50,
                        Unit       = Unit.Ml,
                        Ingredient = new IngredientEntity
                        {
                            Name        = "Lemon",
                            Description = "Mashed"
                        }
                    }
                }
            };

            //Act
            _testContext.CookBookDbContextSUT.Recipe.Add(entity);
            _testContext.CookBookDbContextSUT.SaveChanges();

            //Assert
            using (var dbx = _testContext.DbContextFactory.CreateDbContext())
            {
                var _ = dbx.Recipe.Single(i => i.Id == entity.Id);
            }
        }
示例#3
0
 public static RecipeListModel MapToListModel(RecipeEntity entity) =>
 entity == null
         ? null
         : new RecipeListModel
 {
     Id       = entity.Id,
     Name     = entity.Name,
     Duration = entity.Duration,
     FoodType = (FoodType)entity.FoodType,
     ImageUrl = entity.ImageUrl
 };
示例#4
0
        public ControllerMessage Add(RecipeAddDTO recipeAddDTO)
        {
            string message = String.Empty;
            bool   success = Validate(recipeAddDTO, ref message);

            if (success)
            {
                try
                {
                    DishEntity dish = unitOfWork.Dishes.Get(recipeAddDTO.DishName);
                    if (dish != null)
                    {
                        RecipeEntity recipeEntity = new RecipeEntity
                        {
                            Name              = recipeAddDTO.Name,
                            Description       = recipeAddDTO.Description,
                            Dish              = dish,
                            RecipeIngredients = new List <RecipeIngredientEntity>()
                        };

                        foreach (IngredientPortion ingredientPortion in recipeAddDTO.Ingredients)
                        {
                            IngredientEntity ingredientEntity = unitOfWork.Ingredients.Get(ingredientPortion.Ingredient);
                            if (ingredientEntity != null)
                            {
                                recipeEntity.RecipeIngredients.Add(new RecipeIngredientEntity
                                {
                                    Ingredient = ingredientEntity,
                                    Recipe     = recipeEntity,
                                    Portion    = ingredientPortion.Portion
                                });
                            }
                        }

                        unitOfWork.Recipes.Add(recipeEntity);
                        unitOfWork.Commit();

                        message = "Recipe added";
                    }
                    else
                    {
                        success = false;
                        message = "Dish not found";
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                    message = ExceptionMessageBuilder.BuildMessage(ex);
                }
            }

            return(new ControllerMessage(success, message));
        }
        public async Task Test_AddRecipe()
        {
            RecipeEntity recipe = new RecipeEntity();

            recipe.Name        = "hahahhaa";
            recipe.Description = "pasijuokt garsiai";
            recipe.CookingTime = 3;

            await reServices.AddRecipeAsync(recipe);

            //TODO: Add asserts!
        }
示例#6
0
 public RecipeDetailModel MapEntityToDetailModel(RecipeEntity entity)
 {
     return(new RecipeDetailModel()
     {
         Id = entity.Id,
         Name = entity.Name,
         Type = entity.Type,
         Description = entity.Description,
         Duration = entity.Duration,
         Ingredients = entity.Ingredients
     });
 }
示例#7
0
 public static RecipeDetailModel MapToDetailModel(RecipeEntity entity) =>
 entity == null
         ? null
         : new RecipeDetailModel
 {
     Id          = entity.Id,
     Name        = entity.Name,
     Description = entity.Description,
     FoodType    = (FoodType)entity.FoodType,
     Duration    = entity.Duration,
     Ingredients = entity.Ingredients.Select(IngredientAmountMapper.MapDetailModel).ToList()
 };
        public async Task Test_UpdateRecipe()
        {
            RecipeEntity recipe = new RecipeEntity();

            recipe.ID          = 38;
            recipe.Name        = "boboboboo";
            recipe.Description = "paliudet";
            recipe.CookingTime = 50;

            await reServices.UpdateRecipeAsync(recipe);

            //TODO: Add asserts!
        }
示例#9
0
 public async Task <int> UpdateAsync(RecipeEntity entity)
 {
     // Open Connection
     // Start Transaction
     // Update Recipes
     // Delete ingredients by recipeId
     // Insert new Ingredents
     // Delete instructions by recipeId
     // Insert new instructions
     // Commit transaction
     // Close Connections
     return(await new RecipeRepository_Solution(_connectionString, _ingredientRepository, _instructionRepository).UpdateAsync(entity));
 }
        public async Task <RecipeEntity> GetRecipeAsync(string path)
        {
            RecipeEntity recipe = null;

            HttpResponseMessage response = await client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                var str = await response.Content.ReadAsStringAsync();

                recipe = JsonConvert.DeserializeObject <RecipeEntity>(str);
            }
            return(recipe);
        }
示例#11
0
        internal void Remove(Guid id)
        {
            using (var cookBookDbContext = dbContextFactory())
            {
                var entity = new RecipeEntity()
                {
                    Id = id
                };
                cookBookDbContext.Recipes.Attach(entity);

                cookBookDbContext.Recipes.Remove(entity);
                cookBookDbContext.SaveChanges();
            }
        }
        public async Task AddRecipeAsync(RecipeEntity recipe)
        {
            using (var client = new HttpClient())
            {
                var json    = JsonConvert.SerializeObject(recipe);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;

                response = await client.PostAsync("http://localhost:63421/api/recipe/add", content);

                response.EnsureSuccessStatusCode();
            }
        }
示例#13
0
        public static RecipeEntity MapToEntity(RecipeDetailModel detailModel)
        {
            var mapToEntity = new RecipeEntity()
            {
                Id          = detailModel.Id,
                Name        = detailModel.Name,
                Description = detailModel.Description,
                Duration    = detailModel.Duration,
                FoodType    = (CookBook.DAL.Enums.FoodType)detailModel.FoodType,
                Ingredients = detailModel.Ingredients.Select(IngredientAmountMapper.MapEntity).ToList()
            };

            return(mapToEntity);
        }
示例#14
0
        public async Task UpdateAsync_Success()
        {
            // Arrange
            var recipeRepository = Setup();

            var recipe = new RecipeEntity()
            {
                Id          = _databaseFixture.RecipeId1,
                Title       = "Title Something new",
                Description = "Description New",
                Logo        = "Something",
                Ingredients = new List <IngredientEntity>()
                {
                    new IngredientEntity()
                    {
                        RecipeId        = _databaseFixture.RecipeId1,
                        OrdinalPosition = 0,
                        Unit            = "lbs",
                        Quantity        = 1,
                        Ingredient      = "Chicken"
                    },
                    new IngredientEntity()
                    {
                        RecipeId        = _databaseFixture.RecipeId1,
                        OrdinalPosition = 1,
                        Unit            = "lbs",
                        Quantity        = 1,
                        Ingredient      = "Chicken"
                    }
                },
                Instructions = null
            };

            // Act
            var rowsAffected = await recipeRepository.UpdateAsync(recipe);

            // Assert
            Assert.Equal(3, rowsAffected);

            // Verify properties are updated
            var recipeFromDatabase = await recipeRepository.GetAsync(_databaseFixture.RecipeId1);

            Assert.Equal(recipe.Title, recipeFromDatabase.Title);
            Assert.Equal(recipe.Description, recipeFromDatabase.Description);
            Assert.Equal(recipe.Logo, recipeFromDatabase.Logo);
            Assert.Equal(recipe.Ingredients.Count(), recipeFromDatabase.Ingredients.Count());
            Assert.Equal(0, recipeFromDatabase.Instructions?.Count());
        }
        public async Task Test_GetRecipe()
        {
            RecipeEntity recipe = new RecipeEntity();

            recipe.ID          = 1;
            recipe.Name        = "Sumuðtinis su sûriu";
            recipe.Description = "Ant duonos uþtepti sviesto ir uþdëti sûrio.";
            recipe.CookingTime = 3;

            RecipeEntity recipe2 = await reServices.GetRecipeAsync("api/recipe/get?recipeID=1");

            Assert.AreEqual(recipe.ID, recipe2.ID);
            Assert.AreEqual(recipe.Name, recipe2.Name);
            Assert.AreEqual(recipe.Description, recipe2.Description);
            Assert.AreEqual(recipe.CookingTime, recipe2.CookingTime);
        }
示例#16
0
        public void AddRecipeWithIngredientsTest()
        {
            //Arrange
            var recipeEntity = new RecipeEntity
            {
                Name        = "Lemonade",
                Description = "Simple lemon lemonade",
                Ingredients =
                {
                    new IngredientAmountEntity
                    {
                        Amount     = 1,
                        Unit       = Unit.L,
                        Ingredient = new IngredientEntity
                        {
                            Name        = "Water",
                            Description = "Filtered Water"
                        }
                    },
                    new IngredientAmountEntity()
                    {
                        Amount     = 50,
                        Unit       = Unit.Ml,
                        Ingredient = new IngredientEntity()
                        {
                            Name        = "Lime-juice",
                            Description = "Fresh lime-juice"
                        }
                    }
                }
            };

            //Act
            _testContext.CookBookDbContextSUT.Recipes.Add(recipeEntity);
            _testContext.CookBookDbContextSUT.SaveChanges();


            //Assert
            using (var dbx = _testContext.CreateCookBookDbContext())
            {
                var retrievedRecipe = dbx.Recipes
                                      .Include(entity => entity.Ingredients)
                                      .ThenInclude(amounts => amounts.Ingredient)
                                      .First(entity => entity.Id == recipeEntity.Id);
                Assert.Equal(recipeEntity, retrievedRecipe, RecipeEntity.RecipeEntityComparer);
            }
        }
示例#17
0
        internal RecipeEntity Map(RecipeDetailModel recipeDetailModel)
        {
            var recipeEntity = new RecipeEntity
            {
                Name        = recipeDetailModel.Name,
                Description = recipeDetailModel.Description,
                Type        = (DAL.Entities.FoodType)recipeDetailModel.Type,
                Duration    = recipeDetailModel.Duration
            };

            if (recipeDetailModel.Id == Guid.Empty)
            {
                recipeDetailModel.Id = recipeEntity.Id;
            }
            else
            {
                recipeEntity.Id = recipeDetailModel.Id;
            }

            foreach (var ingredientModel in recipeDetailModel.Ingredients)
            {
                var ingredientAmount = new IngredientAmountEntity
                {
                    Amount = ingredientModel.Amount,
                    Unit   = (DAL.Entities.Unit)ingredientModel.Unit
                };

                var ingredient = new IngredientEntity
                {
                    Name        = ingredientModel.Name,
                    Description = ingredientModel.Description
                };
                if (ingredientModel.Id == Guid.Empty)
                {
                    ingredientModel.Id = ingredient.Id;
                }
                else
                {
                    ingredient.Id = ingredientModel.Id;
                }

                ingredientAmount.Ingredient = ingredient;
                recipeEntity.Ingredients.Add(ingredientAmount);
            }
            return(recipeEntity);
        }
        public void AddRecipe(int id, string text)
        {
            var recipeEntity = new RecipeEntity(DefaultPartitionKey, id.ToString(CultureInfo.InvariantCulture)) {Data = text};

            CreateTableIfNotExist();

            CloudTableClient tableClient = GetCloudTableClient();

            // Get the data service context
            TableServiceContext serviceContext = tableClient.GetDataServiceContext();

            // Add the new recipe to the recipe table
            serviceContext.AddObject(TableName, recipeEntity);

            // Submit the operation to the table service
            serviceContext.SaveChangesWithRetries();
        }
示例#19
0
        public void AddNew_RecipeWithIngredients_Persisted()
        {
            //Arrange
            var recipeEntity = new RecipeEntity
            {
                Name        = "Lemonade",
                Description = "Simple lemon lemonade",
                Ingredients =
                {
                    new IngredientAmountEntity
                    {
                        Amount     = 1,
                        Unit       = Unit.L,
                        Ingredient = new IngredientEntity
                        {
                            Name        = "Water",
                            Description = "Filtered Water"
                        }
                    },
                    new IngredientAmountEntity
                    {
                        Amount     = 50,
                        Unit       = Unit.Ml,
                        Ingredient = new IngredientEntity
                        {
                            Name        = "Lime-juice",
                            Description = "Fresh lime-juice"
                        }
                    }
                }
            };

            //Act
            _cookBookDbContextSUT.Recipes.Add(recipeEntity);
            _cookBookDbContextSUT.SaveChanges();

            //Assert
            using var dbx = _dbContextFactory.Create();
            var retrievedRecipe = dbx.Recipes
                                  .Include(entity => entity.Ingredients)
                                  .ThenInclude(amounts => amounts.Ingredient)
                                  .Single(entity => entity.Id == recipeEntity.Id);

            Assert.Equal(recipeEntity, retrievedRecipe, RecipeEntity.RecipeEntityComparer);
        }
示例#20
0
        private void UpdateIngredientAmounts(RecipeDetailModel recipeModel, RecipeEntity recipeEntity)
        {
            var ingredientAmountsToDelete = recipeEntity.IngredientAmounts.Where(
                ingredientAmount =>
                !recipeModel.Ingredients.Any(ingredient => ingredient.Ingredient.Id == ingredientAmount.IngredientId));

            DeleteIngredientAmounts(ingredientAmountsToDelete);

            var recipeUpdateIngredientModelsToInsert = recipeModel.Ingredients.Where(
                ingredient => !recipeEntity.IngredientAmounts.Any(ingredientAmount => ingredientAmount.IngredientId == ingredient.Ingredient.Id));

            InsertIngredientAmounts(recipeEntity, recipeUpdateIngredientModelsToInsert);

            var recipeUpdateIngredientModelsToUpdate = recipeModel.Ingredients.Where(
                ingredient => recipeEntity.IngredientAmounts.Any(ingredientAmount => ingredientAmount.IngredientId == ingredient.Ingredient.Id));

            UpdateIngredientAmounts(recipeEntity, recipeUpdateIngredientModelsToUpdate);
        }
        public void AddNew_RecipeWithoutIngredients_Persisted()
        {
            //Arrange
            var recipeEntity = new RecipeEntity
            {
                Name = "Chicken soup",
                Description = "Grandma's delicious chicken soup."
            };

            //Act
            _cookBookDbContextSUT.Recipes.Add(recipeEntity);
            _cookBookDbContextSUT.SaveChanges();

            //Assert
            using var dbx = _dbContextFactory.Create();
            var retrievedRecipe = dbx.Recipes
                .Single(entity => entity.Id == recipeEntity.Id);
            Assert.Equal(recipeEntity, retrievedRecipe);
        }
示例#22
0
 public RecipeDetailModel MapDetailModel(RecipeEntity recipeEntity)
 {
     return(new RecipeDetailModel
     {
         Id = recipeEntity.Id,
         Name = recipeEntity.Name,
         Description = recipeEntity.Description,
         Type = (FoodType)recipeEntity.Type,
         Duration = recipeEntity.Duration,
         Ingredients = recipeEntity.Ingredients.Select(ia => new IngredienceModel
         {
             Id = ia.Id,
             Name = ia.Ingredient.Name,
             Description = ia.Ingredient.Description,
             Amount = ia.Amount,
             Unit = (Unit)ia.Unit
         }).ToList()
     });
 }
        public async Task AddNew_RecipeWithoutIngredients_Persisted()
        {
            //Arrange
            RecipeEntity?recipeEntity = new RecipeEntity
            {
                Name        = "Chicken soup",
                Description = "Grandma's delicious chicken soup."
            };

            //Act
            _cookBookDbContextSUT.Recipes.Add(recipeEntity);
            await _cookBookDbContextSUT.SaveChangesAsync();

            //Assert
            await using CookBookDbContext? dbx = _dbContextFactory.CreateDbContext();
            RecipeEntity?retrievedRecipe = await dbx.Recipes
                                           .SingleAsync(entity => entity.Id == recipeEntity.Id);

            Assert.Equal(recipeEntity, retrievedRecipe);
        }
示例#24
0
 // Recipe models and entity mappings.
 public RecipeDetailModel MapRecipeEntityToDetailModel(RecipeEntity recipeEntity)
 {
     return(new RecipeDetailModel
     {
         Id = recipeEntity.Id,
         Name = recipeEntity.Name,
         Directions = recipeEntity.Directions,
         Servings = recipeEntity.Servings,
         RecipeWasAdded = recipeEntity.RecipeWasAdded,
         PrepTime = recipeEntity.PrepTime,
         CookTime = recipeEntity.CookTime,
         AdditionalTime = recipeEntity.AdditionalTime,
         TotalTime = recipeEntity.TotalTime,
         FoodCategory = recipeEntity.FoodCategory,
         FoodSpecialDiet = recipeEntity.FoodSpecialDiet,
         DegreeOfDifficulty = recipeEntity.DegreeOfDifficulty,
         Author = MapAuthorEntityToDetailModel(recipeEntity.Author),
         NutritionInfo = MapNutritionInfoEntityToDetailModel(recipeEntity.NutritionInfo),
         Ingredients = recipeEntity.RecipeIngredients == null ? null : recipeEntity.RecipeIngredients.Select(MapRecipeIngredientEntityToListModel).ToList()
     });
 }
示例#25
0
 public Recipe(RecipeEntity entity)
 {
     Id              = entity.Id;
     BreadId         = entity.BreadId;
     Flour           = entity.Flour;
     Flour2          = entity.Flour2;
     FlourTypes      = entity.FlourTypes;
     Water           = entity.Water;
     WaterTemp       = entity.WaterTemp;
     Salt            = entity.Salt;
     Yeast           = entity.Yeast;
     YeastTsp        = entity.YeastTsp;
     Starter         = entity.Starter;
     BulkFermentTime = entity.BulkFermentTime;
     ProofTime       = entity.ProofTime;
     OvenTemp        = entity.OvenTemp;
     BakeTime        = entity.BakeTime;
     MixTime         = entity.MixTime;
     StarterType     = entity.StarterType;
     NumberOfSteps   = entity.NumberOfSteps;
 }
        protected override void Seed(CookBookDbContext context)
        {
            var ingredient = new IngredientEntity()
            {
                Name        = $"{nameof(IngredientEntity.Name)}-Seed",
                Description = nameof(RecipeEntity.Description),
            };

            var recipe = new RecipeEntity
            {
                Name        = $"{nameof(RecipeEntity.Name)}-Seed",
                Description = nameof(RecipeEntity.Description),
                Duration    = TimeSpan.FromMinutes(323),
                FoodType    = FoodType.Other
            };

            context.Ingredients.Add(ingredient);
            context.Recipes.Add(recipe);

            base.Seed(context);
        }
示例#27
0
        public async Task <int> InsertAsync(RecipeEntity entity)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync();

                using (var transaction = await connection.BeginTransactionAsync())
                {
                    var rowsAffected = await connection.ExecuteAsync(@"
                    INSERT INTO [dbo].[Recipes]
                                ([Id]
                                ,[Title]
                                ,[Description]
                                ,[Logo]
                                ,[CreatedDate])
                            VALUES
                                (@Id
                                ,@Title
                                ,@Description
                                ,@Logo
                                ,@CreatedDate)",
                                                                     new
                    {
                        entity.Id,
                        entity.Title,
                        entity.Description,
                        entity.Logo,
                        entity.CreatedDate
                    }, transaction : transaction);

                    rowsAffected += await _ingredientRepository.InsertAsync(connection, transaction, entity.Ingredients);

                    rowsAffected += await _instructionRepository.InsertAsync(connection, transaction, entity.Instructions);

                    transaction.Commit();

                    return(rowsAffected);
                }
            }
        }
示例#28
0
        public async Task <int> InsertAsync(RecipeEntity entity)
        {
            using (var connection = new SqlConnection("Data Source=host.docker.internal,5050; Initial Catalog=RecipeBook;User Id=sa;Password=P@ssword123;MultipleActiveResultSets=true"))
            {
                await connection.OpenAsync();

                using (var transaction = await connection.BeginTransactionAsync())
                {
                    var rowsAffected = await connection.ExecuteAsync(@"
               INSERT INTO [dbo].[Recipes]
                 ([Id]
                 ,[Title]
                 ,[Description]
                 ,[Logo]
                 ,[CreatedDate])
               VALUES
                 (@Id
                 ,@Title
                 ,@Description
                 ,@Logo
                 ,@CreatedDate)",
                                                                     new
                    {
                        entity.Id,
                        entity.Title,
                        entity.Description,
                        entity.Logo,
                        entity.CreatedDate
                    }, transaction : transaction);

                    rowsAffected += await _ingredientRepository.InsertAsync(connection, transaction, entity.Ingredients);

                    rowsAffected += await _instructionRepository.InsertAsync(connection, transaction, entity.Instructions);

                    transaction.Commit();

                    return(rowsAffected);
                }
            }
        }
示例#29
0
        public Either <Error, int> Insert(RecipeInsertModel entity)
        {
            try
            {
                using (var context = _factory.Create())
                {
                    var recipeEntity = new RecipeEntity()
                    {
                        MainCourseId = entity.MainCourseId,
                        RestaurantId = entity.RestaurantId
                    };

                    var addedEntity = context.Recipe.Add(recipeEntity).Entity;
                    context.SaveChanges();
                    return(new Right <Error, int>(addedEntity.Id));
                }
            }
            catch (Exception ex)
            {
                return(new Left <Error, int>(new UnknownError(ex.ToString())));
            }
        }
示例#30
0
        private static RecipeEntity RecipeToEntity(Recipe recipe)
        {
            var entity = new RecipeEntity
            {
                Id            = recipe.Id,
                Name          = recipe.Name,
                Description   = recipe.Description,
                EstimatedTime = recipe.EstimatedTime,

                PreviewImage = recipe.PreviewImage,
                Images       = recipe.Images,

                Ingredients = recipe.Ingredients,
                Steps       = recipe.Steps
            };

            entity.Pictograms = recipe.Pictograms
                                .Select(pictogram => new RecipePictogramPair(entity, pictogram))
                                .ToList();

            return(entity);
        }
示例#31
0
        public void AddRecipeTest()
        {
            //Arrange
            var recipeEntity = new RecipeEntity
            {
                Name        = "Chicken soup",
                Description = "Grandma's delicious chicken soup."
            };

            //Act
            _testContext.CookBookDbContextSUT.Recipes.Add(recipeEntity);
            _testContext.CookBookDbContextSUT.SaveChanges();


            //Assert
            using var dbx = _testContext.DbContextFactory.CreateDbContext();
            var retrievedRecipe = dbx.Recipes
                                  .Include(entity => entity.Ingredients)
                                  .ThenInclude(amount => amount.Ingredient)
                                  .First(entity => entity.Id == recipeEntity.Id);

            Assert.Equal(recipeEntity, retrievedRecipe, RecipeEntity.RecipeEntityComparer);
        }