Пример #1
0
        async Task <RecipeKeyModel> IDataService.CreateRecipeAsync(Guid bookId, AddRecipeModel model)
        {
            var now    = DateTime.UtcNow;
            var recipe = new RecipeModel
            {
                BookId         = bookId,
                RecipeId       = Guid.NewGuid(),
                Name           = model.Name,
                Description    = model.Description,
                Servings       = model.Servings,
                AddedAt        = now,
                LastModifiedAt = now
            };

            using (var connection = GetConnection())
            {
                await connection.ExecuteAsync(
                    @"
                    INSERT INTO BookRecipe (BookId, RecipeId, [Name], [Description], Servings, AddedAt, LastModifiedat)
                    VALUES (@bookId, @recipeId, @name, @description, @servings, @addedAt, @lastModifiedAt);
                    ",
                    recipe
                    );
            }
            return(new RecipeKeyModel
            {
                BookId = recipe.BookId,
                RecipeId = recipe.RecipeId
            });
        }
Пример #2
0
        public ActionResult Index(AddRecipeModel model)
        {
            var dbcon = new Db();

            if (dbcon.AddRecipe(model))
            {
                return(View());
            }
            return(RedirectToAction("Index", "Home"));
        }
Пример #3
0
        private int AddRecipes(AddRecipeModel model)
        {
            var recipe = new Recipe()
            {
                Title = model.Title,
            };

            _recipeRepository.Insert(recipe);

            return(recipe.Id);
        }
Пример #4
0
 private void AddRecipeDirection(AddRecipeModel model, int recipeId)
 {
     for (int i = 0; i < model.Directions.Count; i++)
     {
         var recipeDirection = new RecipeDirection()
         {
             RecipeId = recipeId,
             Step     = model.Directions[i].Step
         };
         _recipeDirectionRepository.Insert(recipeDirection);
     }
 }
Пример #5
0
 private void AddRecipeCategory(AddRecipeModel model, int recipeId)
 {
     for (int i = 0; i < model.Categories.Count; i++)
     {
         var recipeCategory = new RecipeCategory()
         {
             RecipeId   = recipeId,
             CategoryId = model.Categories[i]
         };
         _recipeCategoryRepository.Insert(recipeCategory);
     }
 }
Пример #6
0
        public async Task <IActionResult> AddRecipe(BookKeyModel bookKey, AddRecipeModel model)
        {
            var book = await GetBookAsync(bookKey);

            if (book == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var recipeKey = await _dataService.CreateRecipeAsync(book.BookId, model);

            recipeKey.Token = bookKey.Token;
            return(RedirectToAction("Index", "Recipe", recipeKey));
        }
Пример #7
0
        public bool AddRecipe(AddRecipeModel model)
        {
            OpenConnection();
            var id  = Guid.NewGuid();
            var sql = "INSERT INTO Recipe (Id,Name,Description,ImageUrl) values('" + id + "','" + model.Name +
                      "','" + model.Description + "','" + model.ImgUrl + "');";
            var cmd = new MySqlCommand(sql, _connection);

            var dataReader = cmd.ExecuteReader();

            dataReader.Read();
            dataReader.Close();

            var Degar = model.Degs.Split(';');

            foreach (var deg in Degar)
            {
                var ingredient = deg.Split(',');
                var sqlQ       = "INSERT INTO Ingredients (Name,Recipe,Quantity,Category) values('" + ingredient[0] + "','" + id +
                                 "','" + ingredient[1] + "','" + "Pajdeg" + "');";
                var commander   = new MySqlCommand(sqlQ, _connection);
                var dataReading = commander.ExecuteReader();
                dataReading.Read();
                dataReading.Close();
            }

            var fyllningar = model.Fylls.Split(';');

            foreach (var fyllning in fyllningar)
            {
                var ingredient = fyllning.Split(',');
                var sqlQ       = "INSERT INTO Ingredients (Name,Recipe,Quantity,Category) values('" + ingredient[0] + "','" + id +
                                 "','" + ingredient[1] + "','" + "Fyllning" + "');";
                var commander   = new MySqlCommand(sqlQ, _connection);
                var dataReading = commander.ExecuteReader();
                dataReading.Read();
                dataReading.Close();
            }

            CloseConnection();
            return(true);
        }
Пример #8
0
        public ActionResult AddRecipe([FromBody] AddRecipeModel model)
        {
            if (model == null)
            {
                return(BadRequest("Wrong Json Object"));
            }
            try
            {
                int recipeId = AddRecipes(model);
                AddRecipeCategory(model, recipeId);
                AddRecipeIngredients(model, recipeId);
                AddRecipeDirection(model, recipeId);

                return(Ok());
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Пример #9
0
        private void AddRecipeIngredients(AddRecipeModel model, int recipeId)
        {
            for (int i = 0; i < model.Ingredients.Count; i++)
            {
                var recipeIngredient = new RecipeIngredient()
                {
                    RecipeId     = recipeId,
                    IngredientId = model.Ingredients[i].Name
                };
                _recipeIngredientRepository.Insert(recipeIngredient);

                var recipeIngredientAmount = new RecipeIngredientAmount()
                {
                    RecipeIngredientId = recipeIngredient.Id,
                    Quantity           = model.Ingredients[i].Amount.Quantity,
                    Unit = model.Ingredients[i].Amount.Unit
                };
                _recipeIngredientAmountRepository.Insert(recipeIngredientAmount);
            }
        }