Пример #1
0
        public async Task <IActionResult> OnGet(int?recipeId)
        {
            Units = htmlHelper.GetEnumSelectList <UnitType>();

            var products = await repository.GetAllProductsAsync();

            Products = new SelectList(products.Select(p => p.Name));

            if (recipeId.HasValue)
            {
                var recipe = await repository.GetRecipeAsync(recipeId.Value);

                if (recipe == null)
                {
                    return(RedirectToPage("./NotFound"));
                }

                RecipeDto = mapper.Map <RecipeDto>(recipe);
            }
            else
            {
                RecipeDto = new RecipeDto();
            }

            return(Page());
        }
Пример #2
0
        public async Task <ActionResult <RecipeDto> > Post([FromBody] RecipeDto recipeDto)
        {
            var existingRecipes = await repository.GetAllRecipesAsync();

            var isNameTaken = existingRecipes.Any(r => r.Name == recipeDto.Name);

            if (isNameTaken)
            {
                return(BadRequest("This name of recipe is already taken."));
            }

            var newRecipe = mapper.Map <Recipe>(recipeDto);

            var products = await repository.GetAllProductsAsync();

            for (int i = 0; i < recipeDto.Ingredients.Length; i++)
            {
                var currentProductName = recipeDto.Ingredients[i].ProductName;
                var currentProduct     = products.Where(p => p.Name == currentProductName).FirstOrDefault();

                if (currentProduct != null)
                {
                    newRecipe.Ingredients[i].Product = currentProduct;
                }
                else
                {
                    return(BadRequest($"There is no product with name {currentProductName}."));
                }
            }

            repository.Add(newRecipe);

            if (await repository.SaveChangesAsync())
            {
                return(CreatedAtAction("GetRecipe", new { id = newRecipe.Id }, mapper.Map <RecipeDto>(newRecipe)));
            }
            else
            {
                return(BadRequest("Failed to save the recipe"));
            }
        }
Пример #3
0
        public async Task <IActionResult> OnGet()
        {
            Products = await repository.GetAllProductsAsync(SearchTerm);

            foreach (string category in Enum.GetNames(typeof(CategoryType)))
            {
                if (Products.Any(p => p.Category.ToString() == category))
                {
                    Categories.Add(category);
                }
            }

            return(Page());
        }
Пример #4
0
        public async Task <IActionResult> OnGet(int recipeId)
        {
            var recipe = await repository.GetRecipeAsync(recipeId);

            if (recipe == null)
            {
                return(RedirectToPage("./NotFound"));
            }

            RecipeDto = mapper.Map <RecipeDto>(recipe);

            var products = await repository.GetAllProductsAsync();

            foreach (var ingredient in RecipeDto.Ingredients)
            {
                var product = products.FirstOrDefault(p => p.Name == ingredient.ProductName);
                Products.Add(product);
            }

            return(Page());
        }
Пример #5
0
        public async Task <ActionResult <ProductDto[]> > GetAllProducts(string name = null)
        {
            var products = await repository.GetAllProductsAsync(name);

            return(mapper.Map <ProductDto[]>(products));
        }