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");
            }
コード例 #2
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");
            }