public async Task <Unit> Handle(DeleteIngredientCommand request,
                                            CancellationToken cancellationToken)
            {
                var user = await _userAuth.GetCurrentUser();

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" });
                }

                if (!await _ingredient.IsIngredientExitById(request.IngredientId, user.Id))
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Ingredient = "Not found" });
                }

                if (await _ingredient.IsIngredientExitInRecipeIngredient(request.IngredientId))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Ingredient = "Recipe use this ingredient" });
                }

                var success = await _ingredient.Delete(user.Id, request.IngredientId);

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
            public async Task <Unit> Handle(AddIngredientCommand request,
                                            CancellationToken cancellationToken)
            {
                var user = await _userAuth.GetCurrentUser();

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" });
                }
                if (request.DishId < 1)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Dish = "Bad request" });
                }

                bool haveIngredients = false;

                if (request.NewIngredients != null && request.NewIngredients.Count > 0)
                {
                    haveIngredients = true;
                    foreach (var ingredient in request.NewIngredients)
                    {
                        if (!await _ingredient.IsIngredientExitById(ingredient.IngredientId, user.Id))
                        {
                            throw new RestException(HttpStatusCode.NotFound, new { Ingredient = "Not found" });
                        }
                        //Check wheather already exist in Reipe
                        if (await _recipe.IsIdsExitInRecipeIngredient(ingredient.IngredientId, request.DishId))
                        {
                            throw new RestException(HttpStatusCode.BadRequest, new { Ingredient = "Already exist" });
                        }
                    }
                }

                var dish = await _dish.GetDish(request.DishId, user.Id);

                if (dish == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Dish = "Not found" });
                }

                if (haveIngredients)
                {
                    foreach (var ingredient in request.NewIngredients)
                    {
                        bool dishIngredient = await _recipe.Create(request.DishId, ingredient.IngredientId, ingredient.Amount);

                        if (!dishIngredient)
                        {
                            throw new Exception("Problem adding Dish Ingredients");
                        }
                    }
                }

                return(Unit.Value);

                throw new Exception("Problem adding dish ingredients");
            }
            public async Task <Unit> Handle(UpdateDishCommand request,
                                            CancellationToken cancellationToken)
            {
                var user = await _userAuth.GetCurrentUser();

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" });
                }

                var dishCategory = await _dishCategory.GetDishCategory(request.DishCategoryId, user.Id);

                if (dishCategory == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { DishCategory = "Not found" });
                }

                var dish = await _dish.GetDish(request.DishId, user.Id);

                if (dish == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Dish = "Not found" });
                }

                if (await _dish.IsDishExitsWithTitle(request.Title, user.Id))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { DishTitle = "Already exist" });
                }

                bool haveIngredient = false;

                if (request.UpdateIngredients != null && request.UpdateIngredients.Count > 0)
                {
                    haveIngredient = true;
                    foreach (var test in request.UpdateIngredients)
                    {
                        if (!await _ingredient.IsIngredientExitById(test.IngredientId, user.Id))
                        {
                            throw new RestException(HttpStatusCode.NotFound, new { Ingredient = "Not found" });
                        }
                        //to check right combination of Ingredient and Dish
                        if (!await _recipe.IsIdsExitInRecipeIngredient(test.IngredientId, request.DishId))
                        {
                            throw new RestException(HttpStatusCode.NotFound, new { DishIngredient = "Not exist" });
                        }
                    }
                }

                var updateDish = new Dish
                {
                    Title          = request.Title ?? dish.Title,
                    Description    = request.Description ?? dish.Description,
                    DishCategoryId = request.DishCategoryId,
                };

                var success = await _dish.Update(user.Id, request.DishId, updateDish);

                if (!success)
                {
                    throw new Exception("Problem saving changes");
                }

                if (haveIngredient)
                {
                    foreach (var updateIngredient in request.UpdateIngredients)
                    {
                        var ingredientIdentityId = 0;

                        var ingredientFromDB = await _ingredient.GetIngredient(user.Id, updateIngredient.IngredientId);

                        if (ingredientFromDB != null)
                        {
                            var updateIngredent = new IngredientDto
                            {
                                Name        = updateIngredient.Name ?? ingredientFromDB.Name,
                                Description = updateIngredient.Description ?? ingredientFromDB.Description
                            };

                            ingredientIdentityId = await _ingredient.Update(user.Id, updateIngredient.IngredientId, updateIngredent);

                            if (ingredientIdentityId <= 0)
                            {
                                throw new Exception("Problem saving changes ingredients");
                            }
                        }

                        if (!string.IsNullOrWhiteSpace(updateIngredient.Amount))
                        {
                            var newMeasurementResult = await _recipe.Update(request.DishId, updateIngredient.IngredientId, updateIngredient.Amount);

                            if (newMeasurementResult <= 0)
                            {
                                throw new Exception("Problem saving changes with new dish list");
                            }
                        }
                    }
                }

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
Exemplo n.º 4
0
            public async Task <Unit> Handle(CreateDishCommand request,
                                            CancellationToken cancellationToken)
            {
                var user = await _userAuth.GetCurrentUser();

                if (user == null)
                {
                    throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" });
                }

                var dishCategory = await _dishCategory.GetDishCategory(request.DishCategoryId, user.Id);

                if (dishCategory == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { DishCategory = "Not found" });
                }

                if (await _dish.IsDishExitsWithSlug(user.Id, request.SlugUrl))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Dish_Slug = "Already exist" });
                }

                if (await _dish.IsDishExitsWithTitle(request.Title, user.Id))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { DishTitle = "Already exist" });
                }

                bool haveIngredients = false;

                if (request.Ingredients != null && request.Ingredients.Count > 0)
                {
                    haveIngredients = true;
                    foreach (var ingredient in request.Ingredients)
                    {
                        if (!await _ingredient.IsIngredientExitById(ingredient.IngredientId, user.Id))
                        {
                            throw new RestException(HttpStatusCode.NotFound, new { Ingredient = "Not found" });
                        }
                    }
                }
                var toCreateDish = new Dish
                {
                    Title          = request.Title,
                    Description    = request.Description,
                    SlugUrl        = request.SlugUrl,
                    DishCategoryId = request.DishCategoryId,
                };

                var createdDish = await _dish.Create(user.Id, toCreateDish);

                if (haveIngredients)
                {
                    foreach (var ingredient in request.Ingredients)
                    {
                        bool dishIngredient = await _recipe.Create(createdDish.Id, ingredient.IngredientId, ingredient.Amount);

                        if (!dishIngredient)
                        {
                            throw new Exception("Problem adding Dish Ingredients");
                        }
                    }
                }

                return(Unit.Value);

                throw new Exception("Problem creating dish");
            }