public IActionResult Update(RecipeUpdateModel recipe)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var response = _recipesService.Update(recipe.ToModel());

                    if (response.IsSuccessful)
                    {
                        return(RedirectToAction("ManageOverview", new { SuccessMessage = "Recipe updated successfuly" }));
                    }
                    else
                    {
                        return(RedirectToAction("ManageOverview", new { ErrorMessage = response.Message }));
                    }
                }
                catch (Exception)
                {
                    return(RedirectToAction("InternalError", "Info"));
                }
            }

            return(View(recipe));
        }
 public async Task <RecipeModel> CreateNew(Guid recipeId, [FromBody] RecipeUpdateModel request)
 {
     return(await _mediator.Send(new UpdateRecipeRequest()
     {
         RecipeId = recipeId,
         Name = request.Name,
         User = _identityProvider.Current
     }));
 }
 private Recipe UpdateModelToDomainModel(RecipeUpdateModel updateModel)
 {
     return(new Recipe()
     {
         Name = updateModel.Name,
         Time = updateModel.Time.Value,
         TotalCost = updateModel.TotalCost.Value,
         IngridientsIds = updateModel.IngridientIds
     });
 }
Exemple #4
0
        public Guid?Update(RecipeUpdateModel recipeUpdateModel)
        {
            var recipeEntityExisting = recipeRepository.GetById(recipeUpdateModel.Id);

            recipeEntityExisting.IngredientAmounts = ingredientAmountRepository.GetByRecipeId(recipeUpdateModel.Id);
            UpdateIngredientAmounts(recipeUpdateModel, recipeEntityExisting);

            var recipeEntityUpdated = mapper.Map <RecipeEntity>(recipeUpdateModel);

            return(recipeRepository.Update(recipeEntityUpdated));
        }
        public void UpdateRecipe_ShouldThrow_EntityException(RecipeUpdateModel recipe, string expectedMessage)
        {
            //Arrange
            var(recipeRepository, ingridientService, dataBase) = GetMocks();
            var recipeService = new RecipeService(recipeRepository.Object, ingridientService.Object);

            //Act
            var exception = Assert.ThrowsAsync <EntityException>(() => recipeService.UpdateRecipe(1, recipe));

            //Assert
            Assert.AreEqual(expectedMessage, exception.Message);
        }
Exemple #6
0
 public static Recipe ToModel(this RecipeUpdateModel viewModel)
 {
     return(new Recipe
     {
         Id = viewModel.Id,
         Title = viewModel.Title,
         ImageUrl = viewModel.ImageUrl,
         Description = viewModel.Description,
         Directions = viewModel.Directions,
         Ingredients = viewModel.Ingredients,
         RecipeTypeId = viewModel.RecipeTypeId
     });
 }
Exemple #7
0
        private void UpdateIngredientAmounts(RecipeUpdateModel recipeUpdateModel, RecipeEntity recipeEntity)
        {
            var ingredientAmountsToDelete = recipeEntity.IngredientAmounts.Where(
                ingredientAmount =>
                !recipeUpdateModel.Ingredients.Any(ingredient => ingredient.IngredientId == ingredientAmount.IngredientId));

            DeleteIngredientAmounts(ingredientAmountsToDelete);

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

            InsertIngredientAmounts(recipeEntity, recipeUpdateIngredientModelsToInsert);

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

            UpdateIngredientAmounts(recipeEntity, recipeUpdateIngredientModelsToUpdate);
        }
Exemple #8
0
 public RecipeEntity MapRecipeUpdateModelToEntity(RecipeUpdateModel recipeUpdateModel)
 {
     return(new RecipeEntity
     {
         Id = recipeUpdateModel.Id,
         Name = recipeUpdateModel.Name,
         Directions = recipeUpdateModel.Directions,
         Servings = recipeUpdateModel.Servings,
         PrepTime = recipeUpdateModel.PrepTime,
         CookTime = recipeUpdateModel.CookTime,
         AdditionalTime = recipeUpdateModel.AdditionalTime,
         FoodCategory = recipeUpdateModel.FoodCategory,
         FoodSpecialDiet = recipeUpdateModel.FoodSpecialDiet,
         DegreeOfDifficulty = recipeUpdateModel.DegreeOfDifficulty,
         Author = MapAuthorDetailModelToEntity(recipeUpdateModel.Author),
         NutritionInfo = MapNutritionInfoUpdateModelToEntity(recipeUpdateModel.NutritionInfo),
         RecipeIngredients = recipeUpdateModel.RecipeIngredients == null ? null : recipeUpdateModel.RecipeIngredients.Select(MapRecipeIngredientUpdateModelToEntity).ToList()
     });
 }
        public async Task UpdateRecipe_ShouldUpdateModel()
        {
            //Arrange
            var(recipeRepository, ingridientService, dataBase) = GetMocks();
            var recipeService         = new RecipeService(recipeRepository.Object, ingridientService.Object);
            var idOfRecipeToBeUpdated = 1;
            var recipeUpdateModel     = new RecipeUpdateModel()
            {
                Name = "Recipe1", Time = 1.5f, TotalCost = 1000, IngridientIds = new List <int> {
                    1
                }
            };

            //Act
            var updatedRecipeModel = await recipeService.UpdateRecipe(idOfRecipeToBeUpdated, recipeUpdateModel);

            //Assert
            Assert.AreEqual(updatedRecipeModel.Name, dataBase[idOfRecipeToBeUpdated].Name);
            Assert.AreEqual(updatedRecipeModel.TotalCost, dataBase[idOfRecipeToBeUpdated].TotalCost);
            Assert.AreEqual(updatedRecipeModel.Time, dataBase[idOfRecipeToBeUpdated].Time);
            Assert.AreEqual(updatedRecipeModel.IngridientsIds, dataBase[idOfRecipeToBeUpdated].IngridientsIds);
        }
        public async Task <Recipe> UpdateRecipe(int id, RecipeUpdateModel recipeUpdateModel)
        {
            await CheckExisting(id);

            await _ingridientService.GetIngridients(recipeUpdateModel.IngridientIds); //Checking that each ingridient exists

            if ("".Equals(recipeUpdateModel.Name))
            {
                throw new EntityException("Recipe's name can't be empty");
            }

            if (recipeUpdateModel.Time.HasValue && recipeUpdateModel.Time < 0)
            {
                throw new EntityException("Time can't be negative");
            }

            if (recipeUpdateModel.TotalCost.HasValue && recipeUpdateModel.TotalCost < 0)
            {
                throw new EntityException("TotalCost can't be negative");
            }

            return(await _recipeRepository.UpdateRecipe(id, recipeUpdateModel));
        }
Exemple #11
0
 public ActionResult <Guid?> Update(RecipeUpdateModel recipe)
 {
     return(recipeFacade.Update(recipe));
 }
 public ActionResult <Recipe> UpdateRecipe(
     [FromRoute] int recipeId, [FromBody] RecipeUpdateModel recipe)
 {
     throw new NotImplementedException();
 }
Exemple #13
0
        public ActionResult InsertRecipe(RecipeUpdateModel recipeUpdate)
        {
            var jsonResult      = new JsonResult();
            var loginSuccessful = false;

            if (Session["loggedIn"] != null || m_helper.Login(recipeUpdate.LoginDetails, Constants.ODBCString))
            {
                loginSuccessful = true;
                var connectionString = Constants.ODBCString;
                using (var dbConnection = new ODBConnection(connectionString))
                    using (var persister = new ODBCPersister(dbConnection))
                    {
                        var count = int.Parse(persister.ExecuteScalar(string.Format(m_helper.GetQueryValue("checkCafeUser"), recipeUpdate.LoginDetails.UserName)).ToString());
                        if (count > 0)
                        {
                            // if it exists
                            var itemExists = int.Parse(persister.ExecuteScalar(string.Format(m_helper.GetQueryValue("checkCafe"), recipeUpdate.CafeID, "active")).ToString()) > 0;
                            if (itemExists)
                            {
                                //update updateStoreHouseInventory
                                persister.ExecuteNonQueryCmd("FoodItem", string.Format(m_helper.GetQueryValue("updateFoodItem"),
                                                                                       recipeUpdate.Food.FoodItemID,
                                                                                       recipeUpdate.Food.FoodItemName,
                                                                                       recipeUpdate.Food.FoodItemType,
                                                                                       recipeUpdate.Food.FoodItemDescription,
                                                                                       recipeUpdate.Food.FoodItemCost,
                                                                                       recipeUpdate.Food.FoodItemStatus,
                                                                                       recipeUpdate.Food.CafeMenuID));

                                persister.ExecuteScalar(string.Format(m_helper.GetQueryValue("updateRecipe"), recipeUpdate.Recipe.FoodItemIngredientQuantity, recipeUpdate.Recipe.FoodItemID));
                            }
                            else
                            {
                                persister.ExecuteNonQueryCmd("FoodItem", string.Format(m_helper.GetQueryValue("insertFoodItem"),
                                                                                       recipeUpdate.Food.FoodItemID,
                                                                                       recipeUpdate.Food.FoodItemName,
                                                                                       recipeUpdate.Food.FoodItemType,
                                                                                       recipeUpdate.Food.FoodItemDescription,
                                                                                       recipeUpdate.Food.FoodItemCost,
                                                                                       recipeUpdate.Food.FoodItemStatus,
                                                                                       recipeUpdate.Food.CafeMenuID));

                                persister.ExecuteScalar(string.Format(m_helper.GetQueryValue("insertRecipe"),
                                                                      recipeUpdate.Recipe.FoodItemID,
                                                                      recipeUpdate.Recipe.FoodItemIngredientQuantity,
                                                                      recipeUpdate.Recipe.FoodItemIngredientQuantityUnit,
                                                                      recipeUpdate.Recipe.IngredientID));
                            }
                        }
                        else
                        {
                            var msg = "You are not previliged to complete this action";
                            jsonResult.Data = new { msg };
                        }
                    }
            }
            else
            {
                jsonResult.Data = new { loginSuccessful };
            }

            return(jsonResult);
        }
        public async Task <Recipe> UpdateRecipe(int id, RecipeUpdateModel recipeUpdateModel)
        {
            var recipe = await GetRecipe(id);

            if (recipeUpdateModel.Name is {})