コード例 #1
0
        public async Task <IActionResult> Create(
            [Bind("Id,LibraryId,Name,MixTypeId,Instructions,Source")] Recipe recipe,
            RecipeViewModel recipeVM)
        {
            if (ModelState.IsValid)
            {
                recipe.Id = Guid.NewGuid();
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                foreach (var ingredientVM in recipeVM.IngredientViewModels)
                {
                    var ingredient = new Ingredient
                    {
                        Id          = Guid.NewGuid(),
                        RecipeId    = recipe.Id,
                        ComponentId = ingredientVM.ComponentId,
                        Quantity    = ingredientVM.Quantity,
                        UnitId      = ingredientVM.UnitId,
                        Number      = ingredientVM.Number
                    };
                    _context.Add(ingredient);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(recipe));
        }
コード例 #2
0
        public async Task ImportComponentTypes_GivenMixedSet_InsertsAndDoesNotDoubleInsert()
        {
            var existingNames = new []
            {
                "Absinthe",
                "Liqueur",
                "Rum"
            };

            var newNames = new []
            {
                "Fruit",
                "Gin",
                "Vodka"
            };

            await _db.ComponentType.AddRangeAsync(existingNames.Select(a => new ComponentType
            {
                Name = a
            }));

            await _db.SaveChangesAsync();

            await _sut.ImportComponentTypes(existingNames.Concat(newNames));

            Assert.Equal(6, _db.ComponentType.Count());
        }
コード例 #3
0
        public async Task <Guid> ImportRecipe(Guid libraryId, ImportRecipe recipe)
        {
            var dbRecipe = await _context
                           .Recipe
                           .FirstOrDefaultAsync(a => a.LibraryId == libraryId && a.Name == recipe.RecipeName);

            if (dbRecipe != null)
            {
                return(dbRecipe.Id);
            }

            dbRecipe = new Recipe
            {
                Id           = Guid.NewGuid(),
                LibraryId    = libraryId,
                Name         = recipe.RecipeName,
                MixTypeId    = recipe.MixMethod,
                Instructions = recipe.Instructions,
                Source       = "Automated import"
            };

            await _context.Recipe.AddAsync(dbRecipe);

            await _context.SaveChangesAsync();

            return(dbRecipe.Id);
        }
コード例 #4
0
 public async Task<IActionResult> Create([Bind("Id,Name")] Unit unit)
 {
     if (ModelState.IsValid)
     {
         _context.Add(unit);
         await _context.SaveChangesAsync();
         return RedirectToAction(nameof(Index));
     }
     return View(unit);
 }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("Id,Name")] ComponentType componentType)
        {
            if (ModelState.IsValid)
            {
                _context.Add(componentType);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(componentType));
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("Id,RecipeId,ComponentId,Quantity,UnitId,Number")] Ingredient ingredient)
        {
            if (ModelState.IsValid)
            {
                ingredient.Id = Guid.NewGuid();
                _context.Add(ingredient);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(ingredient));
        }
コード例 #7
0
        public async Task <IActionResult> Create([Bind("Id,ComponentTypeId,Name,Description")] Component component)
        {
            if (ModelState.IsValid)
            {
                component.Id = Guid.NewGuid();
                _context.Add(component);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(component));
        }
コード例 #8
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Library library)
        {
            if (ModelState.IsValid)
            {
                library.Id = Guid.NewGuid();
                _context.Add(library);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(library));
        }
コード例 #9
0
        public async Task GivenExistingName_DoesNotReAdd()
        {
            var libraryName = "Flagon Enterprises";
            await _db.Library.AddAsync(new Library
            {
                Id   = Guid.NewGuid(),
                Name = libraryName
            });

            await _db.SaveChangesAsync();

            await _sut.ImportLibraries(new [] { libraryName });

            Assert.Equal(1, _db.Library.Count());
        }
コード例 #10
0
        public async Task ImportComponentTypes(IEnumerable <string> componentTypeNames)
        {
            var existingComponentTypes = await _context.ComponentType.ToListAsync();

            var newComponentTypes = componentTypeNames
                                    .Distinct()
                                    .Except(existingComponentTypes.Select(a => a.Name))
                                    .Select(a => new ComponentType
            {
                Name = a
            });

            await _context.ComponentType.AddRangeAsync(newComponentTypes);

            await _context.SaveChangesAsync();
        }
コード例 #11
0
        public async Task <IDictionary <string, Guid> > ImportLibraries(IEnumerable <string> libraries)
        {
            var uniqueNames = libraries.Distinct();
            var response    = new Dictionary <string, Guid>();

            // Assumes a small number of libraries in the set
            foreach (var libraryName in libraries)
            {
                var dbLibrary = await _context
                                .Library
                                .FirstOrDefaultAsync(a => a.Name == libraryName);

                if (dbLibrary != null)
                {
                    response[libraryName] = dbLibrary.Id;
                    continue;
                }

                dbLibrary = new Library
                {
                    Id   = Guid.NewGuid(),
                    Name = libraryName
                };
                await _context.Library.AddAsync(dbLibrary);

                response[libraryName] = dbLibrary.Id;
            }

            await _context.SaveChangesAsync();

            return(response);
        }
コード例 #12
0
        public async Task GivenMixedSet_DoesNotDoubleInsert()
        {
            var existingUnits = new []
            {
                "oz",
                "tsp",
                "g"
            };

            var newUnits = new []
            {
                "pint",
                "gallon",
                "gross"
            };

            await _db.Unit.AddRangeAsync(existingUnits.Select(a => new Unit
            {
                Name = a
            }));

            await _db.SaveChangesAsync();

            var result = await _sut.ImportUnits(existingUnits.Concat(newUnits));

            Assert.Equal(6, _db.Unit.Count());
            Assert.Equal(6, result.Count);
        }
コード例 #13
0
        public async Task ImportRecipe_GivenExistingName_DoesNotReAdd()
        {
            var recipeName = "The GOB";
            await _db.Recipe.AddAsync(new Recipe
            {
                Id           = Guid.NewGuid(),
                LibraryId    = LibraryId,
                Name         = recipeName,
                MixTypeId    = 1,
                Instructions = "Instructions",
                Source       = "Test"
            });

            await _db.SaveChangesAsync();

            await _sut.ImportRecipe(LibraryId, new ImportRecipe
            {
                RecipeName   = recipeName,
                MixMethod    = 1,
                Instructions = "Instructions"
            });

            Assert.Equal(1, _db.Recipe.Count());
        }
コード例 #14
0
        public async Task <IDictionary <string, int> > ImportUnits(IEnumerable <string> unitNames)
        {
            var existingUnits = await _context.Unit.ToListAsync();

            var newUnits = unitNames
                           .Distinct()
                           .Except(existingUnits.Select(a => a.Name))
                           .Select(a => new Unit
            {
                Name = a
            });

            await _context.Unit.AddRangeAsync(newUnits);

            await _context.SaveChangesAsync();

            return((await _context
                    .Unit
                    .ToListAsync())
                   .ToDictionary(a => a.Name ?? "-", a => a.Id));
        }