public RecipeListModel[] GetAll()
 {
     using (var dbx = new CookBookDbContext())
     {
         return(dbx.Recipes.Select(_mapper.Map).ToArray());
     }
 }
Exemple #2
0
        public async void CanEditIngredientBadRequest()
        {
            DbContextOptions <CookBookDbContext> options = new DbContextOptionsBuilder <CookBookDbContext>().UseInMemoryDatabase("CanEditIngredientBadRequest").Options;

            using (CookBookDbContext context = new CookBookDbContext(options))
            {
                //Arrange
                Ingredients ingredient = new Ingredients();
                ingredient.ID   = 1;
                ingredient.Name = "Swag";
                Ingredients ingredient2 = new Ingredients();
                ingredient2.ID   = 2;
                ingredient2.Name = "Energy";
                Ingredients ingredient3 = new Ingredients();
                ingredient3.ID   = 3;
                ingredient3.Name = "Heart";

                //Act
                IngredientsController ingredientsController = new IngredientsController(context, configuration);
                await ingredientsController.Post(ingredient);

                await ingredientsController.Post(ingredient2);

                var data = await ingredientsController.Put(6, ingredient3);

                //Assert
                Assert.IsType <BadRequestObjectResult>(data);
            }
        }
 public IngredienceDetailModel[] GetAllIngrediences()
 {
     using (var dbx = new CookBookDbContext())
     {
         return(dbx.Ingredients.Select(_mapper.Map).ToArray());
     }
 }
Exemple #4
0
        private void SynchronizeCollections(DbContext dbContext, TEntity entity)
        {
            if (CollectionsToBeSynchronized == null)
            {
                return;
            }

            IQueryable <TEntity> query = dbContext.Set <TEntity>();
            TEntity?entityInDb;

            using (CookBookDbContext dbContextGetById = DbContextFactory.CreateDbContext())
            {
                entityInDb = GetById(dbContextGetById, entity.Id);
                if (entityInDb == null)
                {
                    return;
                }
            }

            foreach (Func <TEntity, IEnumerable <IEntity> >?collectionSelector in CollectionsToBeSynchronized)
            {
                IEntity[]             updatedCollection = collectionSelector(entity).ToArray();
                IEnumerable <IEntity> collectionInDb    = collectionSelector(entityInDb);

                foreach (IEntity item in collectionInDb)
                {
                    if (!updatedCollection.Contains(item, RepositoryExtensions.IdComparer))
                    {
                        dbContext.Remove(dbContext.Find(item.GetType(), item.Id));
                    }
                }
            }
        }
Exemple #5
0
        public async void CanGetOneIngredientOkResult()
        {
            DbContextOptions <CookBookDbContext> options = new DbContextOptionsBuilder <CookBookDbContext>().UseInMemoryDatabase("CanGetOneIngredientOkResult").Options;

            using (CookBookDbContext context = new CookBookDbContext(options))
            {
                //Arrange
                Ingredients ingredient = new Ingredients();
                ingredient.ID   = 1;
                ingredient.Name = "Swag";
                Ingredients ingredient2 = new Ingredients();
                ingredient2.ID   = 2;
                ingredient2.Name = "Energy";

                //Act
                IngredientsController ingredientsController = new IngredientsController(context, configuration);
                await ingredientsController.Post(ingredient);

                await ingredientsController.Post(ingredient2);

                var data = ingredientsController.Get(2);

                //Assert
                Assert.IsType <OkObjectResult>(data);
            }
        }
Exemple #6
0
        public void SeededWater_DeleteById_Deleted()
        {
            _ingredientRepositorySUT.Delete(IngredientSeeds.Water.Id);

            using CookBookDbContext dbxAssert = _dbContextFactory.CreateDbContext();
            Assert.False(dbxAssert.Ingredients.Any(i => i.Id == IngredientSeeds.Water.Id));
        }
 public void ClearDatabase()
 {
     using (var dbx = new CookBookDbContext())
     {
         dbx.TruncateTables();
     }
 }
 public void InsertRecipe(RecipeDetailModel recipeDetailModel)
 {
     using (var dbx = new CookBookDbContext())
     {
         dbx.Recipes.Add(_mapper.Map(recipeDetailModel));
         dbx.SaveChanges();
     }
 }
Exemple #9
0
        public IngredientRepositoryTests()
        {
            _dbContextFactory           = new DbContextInMemoryFactory(nameof(IngredientRepositoryTests));
            using CookBookDbContext dbx = _dbContextFactory.CreateDbContext();
            dbx.Database.EnsureCreated();

            _ingredientRepositorySUT = new IngredientRepository(_dbContextFactory);
        }
Exemple #10
0
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            using (var db = new CookBookDbContext())
            {
                ViewBag.CookBooks = db.CookBooks.ToList();
            }

            return(View());
        }
Exemple #11
0
        public void CreateDbContext()
        {
            this.CookBookDbContext = new CookBookDbContext();

            if (this.CookBookDbContext.Database.Exists())
            {
                this.CookBookDbContext.Database.Delete();
            }
        }
 public void InsertOrUpdateRecipe(RecipeDetailModel detail)
 {
     using (var dbx = new CookBookDbContext())
     {
         var state = detail.Id == Guid.Empty ?
                     EntityState.Added : EntityState.Modified;
         dbx.Entry(this._mapper.Map(detail)).State = state;
         dbx.SaveChanges();
     }
 }
 public RecipeDetailModel GetById(Guid id)
 {
     using (var dbx = new CookBookDbContext())
     {
         return(_mapper.MapDetailModel(
                    dbx.Recipes.Include(
                        r => r.Ingredients.Select(i => i.Ingredient)
                        ).FirstOrDefault(r => r.Id == id)));
     }
 }
Exemple #14
0
        public IEnumerable <TListModel> GetAll()
        {
            using CookBookDbContext dbContext = DbContextFactory.CreateDbContext();
            IQueryable <TEntity> query = dbContext.Set <TEntity>();

            if (ListIncludes != null)
            {
                query = ListIncludes(query);
            }

            return(query.AsEnumerable().Select(e => MapListModel(e) !).ToArray());
        }
        public RecipeRepositoryTests(ITestOutputHelper output)
        {
            XUnitTestOutputConverter converter = new XUnitTestOutputConverter(output);

            Console.SetOut(converter);

            _dbContextFactory           = new DbContextInMemoryFactory(nameof(RecipeRepositoryTests));
            using CookBookDbContext dbx = _dbContextFactory.CreateDbContext();
            dbx.Database.EnsureCreated();

            _repositorySUT = new RecipeRepository(_dbContextFactory);
        }
Exemple #16
0
        public TDetailModel InsertOrUpdate(TDetailModel detailModel)
        {
            using CookBookDbContext dbContext = DbContextFactory.CreateDbContext();
            TEntity entity = MapEntity(detailModel) !;

            dbContext.Update <TEntity>(entity);
            SynchronizeCollections(dbContext, entity);

#if DEBUG
            DisplayStates(dbContext.ChangeTracker.Entries());
#endif

            dbContext.SaveChanges();

            return(MapDetailModel(entity) !);
        }
Exemple #17
0
        public void NewIngredient_InsertOrUpdate_IngredientAdded()
        {
            //Arrange
            IngredientDetailModel ingredient = new IngredientDetailModel()
            {
                Name        = "Water",
                Description = "Mineral water"
            };

            //Act
            ingredient = _ingredientRepositorySUT.InsertOrUpdate(ingredient);

            //Assert
            using CookBookDbContext dbxAssert = _dbContextFactory.CreateDbContext();
            IngredientEntity ingredientFromDb = dbxAssert.Ingredients.Single(i => i.Id == ingredient.Id);

            Assert.Equal(ingredient, IngredientMapper.MapEntityToDetailModel(ingredientFromDb));
        }
Exemple #18
0
        public void InsertAndGetIngredient_IngredientsAdd_IngredientAddedAndObtained()
        {
            //Arrange
            var ingredient = CreateSugarIngredient();

            //Act
            this.CookBookDbContext.Ingredients.Add(ingredient);
            var savedEntities = this.CookBookDbContext.SaveChanges();

            IngredientEntity ingredientFromDb;

            using (var dbx = new CookBookDbContext())
            {
                ingredientFromDb = dbx.Ingredients.First(i => i.Id == ingredient.Id);
            }

            //Assert
            Assert.Equal(1, savedEntities);

            Assert.Equal(ingredient.Id, ingredientFromDb.Id);
        }
Exemple #19
0
        public async void CanCreateIngredient()
        {
            DbContextOptions <CookBookDbContext> options = new DbContextOptionsBuilder <CookBookDbContext>().UseInMemoryDatabase("CanCreateIngredient").Options;

            using (CookBookDbContext context = new CookBookDbContext(options))
            {
                //Arrange
                Ingredients ingredient = new Ingredients();
                ingredient.ID   = 1;
                ingredient.Name = "Swag";

                //Act
                IngredientsController ingredientsController = new IngredientsController(context, configuration);
                await ingredientsController.Post(ingredient);

                var result = context.Ingredients.FirstOrDefault(c => c.ID == ingredient.ID);

                //Assert
                Assert.Equal(ingredient, result);
            }
        }
Exemple #20
0
        public void SeededWater_InsertOrUpdate_IngredientUpdated()
        {
            //Arrange
            IngredientDetailModel ingredient = new IngredientDetailModel()
            {
                Id          = IngredientSeeds.Water.Id,
                Name        = IngredientSeeds.Water.Name,
                Description = IngredientSeeds.Water.Description,
            };

            ingredient.Name        += "updated";
            ingredient.Description += "updated";

            //Act
            _ingredientRepositorySUT.InsertOrUpdate(ingredient);

            //Assert
            using CookBookDbContext dbxAssert = _dbContextFactory.CreateDbContext();
            IngredientEntity ingredientFromDb = dbxAssert.Ingredients.Single(i => i.Id == ingredient.Id);

            Assert.Equal(ingredient, IngredientMapper.MapEntityToDetailModel(ingredientFromDb));
        }
Exemple #21
0
        public async void CanCreateRecipe()
        {
            DbContextOptions <CookBookDbContext> options = new DbContextOptionsBuilder <CookBookDbContext>().UseInMemoryDatabase("CanDeleteRecipe").Options;

            using (CookBookDbContext context = new CookBookDbContext(options))
            {
                //Arrange
                Recipes recipe = new Recipes();
                recipe.ID   = 1;
                recipe.Name = "Sketti n Ketchup";


                //Act
                RecipesController recipesController = new RecipesController(context, configuration);
                await recipesController.Post(recipe);

                var result = context.Recipes.FirstOrDefault(c => c.ID == recipe.ID);

                //Assert
                Assert.Equal(recipe, result);
            }
        }
Exemple #22
0
        public async void CanCreateInstructions()
        {
            DbContextOptions <CookBookDbContext> options = new DbContextOptionsBuilder <CookBookDbContext>().UseInMemoryDatabase("CanCreateInstructions").Options;

            using (CookBookDbContext context = new CookBookDbContext(options))
            {
                //Arrange
                Instructions recipe = new Instructions();
                recipe.RecipeID     = 1;
                recipe.StepNumberID = 2;
                recipe.Action       = "Boil water";

                //Act
                InstructionsController InstructionsController = new InstructionsController(context, configuration);
                await InstructionsController.Post(recipe);

                var result = await context.Instructions.FirstOrDefaultAsync(c => c.RecipeID == recipe.RecipeID);

                //Assert
                Assert.Equal(recipe, result);
            }
        }
Exemple #23
0
 public UnitOfWork()
 {
     _context = new CookBookDbContext();
 }
Exemple #24
0
 public IngredientsController(CookBookDbContext context, IConfiguration configuration)
 {
     _context      = context;
     Configuration = configuration;
 }
Exemple #25
0
 public GenericRepository(CookBookDbContext context)
 {
     this.context = context;
     this.dbSet   = context.Set <TEntity>();
 }
Exemple #26
0
 private static void ClearDatabase(CookBookDbContext dbContext)
 {
     dbContext.RemoveRange(dbContext.Recipes);
     dbContext.RemoveRange(dbContext.Ingredients);
     dbContext.SaveChanges();
 }
Exemple #27
0
        private static void SeedData(CookBookDbContext dbContext)
        {
            var darkChocolate = new IngredientEntity
            {
                Id          = new Guid("5abdfee1-c970-4afd-aff8-aa3cfef8b1ac"),
                Name        = "dark chocolate",
                Description = "80% cocoa"
            };

            dbContext.Ingredients.Add(darkChocolate);

            var wholeMilk = new IngredientEntity
            {
                Id          = new Guid("83041385-cb60-401b-bf11-cc5ffb8bc570"),
                Name        = "alpine milk",
                Description = "4% fat"
            };

            dbContext.Ingredients.Add(wholeMilk);

            var almondFlour = new IngredientEntity
            {
                Id          = new Guid("cb181669-4e02-449f-bf02-ab6020dfecb4"),
                Name        = "almond flour",
                Description = ""
            };

            dbContext.Ingredients.Add(almondFlour);

            var egg = new IngredientEntity
            {
                Id          = new Guid("012ac89a-94e3-4bc2-94b5-c9b05fc83375"),
                Name        = "egg",
                Description = ""
            };

            dbContext.Ingredients.Add(egg);

            dbContext.Recipes.Add(
                new RecipeEntity
            {
                Id          = new Guid("cb8db9b3-799c-4ef2-9d85-ce32a9ffa843"),
                Name        = "chocolate cake",
                Duration    = TimeSpan.FromMinutes(30),
                FoodType    = FoodType.Dessert,
                Ingredients =
                {
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("1d2e7873-3e35-4d40-877c-a3d0d78de3c0"),
                        Amount     = 0.5,
                        Unit       = Unit.Kg,
                        Ingredient = darkChocolate
                    },
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("2711f535-3566-446c-9ac6-58261efe3fa3"),
                        Amount     = 0.3,
                        Unit       = Unit.L,
                        Ingredient = wholeMilk
                    },
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("c8cdbff9-6692-42ad-93aa-69cb56f95019"),
                        Amount     = 5,
                        Unit       = Unit.Pieces,
                        Ingredient = egg
                    },
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("b417ad46-b94c-487e-8cc1-97ebd7551b13"),
                        Amount     = 7,
                        Unit       = Unit.Spoon,
                        Ingredient = almondFlour
                    }
                }
            });
            dbContext.SaveChanges();
        }
Exemple #28
0
        private static void SeedData(CookBookDbContext dbContext)
        {
            var darkChocolate = new IngredientEntity
            {
                Id          = new Guid("5abdfee1-c970-4afd-aff8-aa3cfef8b1ac"),
                Name        = "Tmavá čokoláda",
                Description = "Tmavá čokoláda s 80% kakaa.",
            };

            dbContext.Ingredients.Add(darkChocolate);

            var wholeMilk = new IngredientEntity
            {
                Id          = new Guid("83041385-cb60-401b-bf11-cc5ffb8bc570"),
                Name        = "Plnotučné mlieko",
                Description = "Plnotučné mlieko so 4% tuku.",
            };

            dbContext.Ingredients.Add(wholeMilk);

            var almondFlour = new IngredientEntity
            {
                Id          = new Guid("cb181669-4e02-449f-bf02-ab6020dfecb4"),
                Name        = "Mandlová múka",
                Description = "Najemno umletá mandlová múka",
            };

            dbContext.Ingredients.Add(almondFlour);

            var egg = new IngredientEntity
            {
                Id          = new Guid("012ac89a-94e3-4bc2-94b5-c9b05fc83375"),
                Name        = "Vajíčko",
                Description = "Slepačie vajíčko.",
            };

            dbContext.Ingredients.Add(egg);

            dbContext.Recipes.Add(
                new RecipeEntity
            {
                Id          = new Guid("cb8db9b3-799c-4ef2-9d85-ce32a9ffa843"),
                Name        = "Čokoládová torta",
                Duration    = TimeSpan.FromMinutes(30),
                FoodType    = FoodType.Dessert,
                Ingredients =
                {
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("1d2e7873-3e35-4d40-877c-a3d0d78de3c0"),
                        Amount     = 0.5,
                        Unit       = Unit.Kg,
                        Ingredient = darkChocolate
                    },
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("2711f535-3566-446c-9ac6-58261efe3fa3"),
                        Amount     = 0.3,
                        Unit       = Unit.L,
                        Ingredient = wholeMilk
                    },
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("c8cdbff9-6692-42ad-93aa-69cb56f95019"),
                        Amount     = 5,
                        Unit       = Unit.Pieces,
                        Ingredient = egg
                    },
                    new IngredientAmountEntity
                    {
                        Id         = new Guid("b417ad46-b94c-487e-8cc1-97ebd7551b13"),
                        Amount     = 7,
                        Unit       = Unit.Spoon,
                        Ingredient = almondFlour
                    }
                }
            });
            dbContext.SaveChanges();
        }
Exemple #29
0
 public CookBookDbContextTests()
 {
     _dbContextFactory     = new DbContextInMemoryFactory(nameof(CookBookDbContextTests));
     _cookBookDbContextSUT = _dbContextFactory.Create();
     _cookBookDbContextSUT.Database.EnsureCreated();
 }
Exemple #30
0
 public UserService(IOptions <AppSettings> appSettings, CookBookDbContext context)
 {
     this.appSettings       = appSettings.Value;
     this.cookbookDbContext = context;
 }