예제 #1
0
        public async Task <IActionResult> AddRecipeToFav(int userId, int recipeId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var like = await _recipesRepository.GetFav(userId, recipeId);

            if (like != null)
            {
                return(BadRequest("You allready like this recipe"));
            }

            if (await _recipesRepository.GetRecipe(recipeId) == null)
            {
                return(NotFound());
            }

            like = new FavouriteRecipe
            {
                UserId   = userId,
                RecipeId = recipeId
            };

            _recipesRepository.Add <FavouriteRecipe>(like);

            if (await _recipesRepository.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed  to fav  recipe"));
        }
        public async Task <IActionResult> Create([FromBody] RecipeDTO recipeDto)
        {
            var recipe = Recipe.Create(recipeDto.Name,
                                       recipeDto.Description,
                                       recipeDto.Servings,
                                       recipeDto.Calories,
                                       recipeDto.CookingTime,
                                       0,
                                       0,
                                       0,
                                       0);
            await _recipesRepository.Add(recipe);

            foreach (var ingredient in recipeDto.Ingredients)
            {
                await _ingredientsRepository.AddIngredient(recipe.Id,
                                                           ingredient.Name,
                                                           ingredient.Quantity,
                                                           ingredient.UnitOfMeasurement);
            }

            foreach (var instruction in recipeDto.InstructionSteps)
            {
                await _instructionsRepository.AddInstruction(recipe.Id,
                                                             instruction.Description,
                                                             instruction.InstructionNr);
            }
            return(Ok(recipe));
        }
예제 #3
0
        public async Task <IActionResult> Create(CreateRecipeViewModel recipeViewModel, List <IFormFile> files)
        {
            var recipe = new Recipe();

            if (ModelState.IsValid)
            {
                recipe.ApplicationUserId = _userManager.GetUserId(HttpContext.User);
                var now = DateTime.Now;
                recipe.CreatedAt    = now;
                recipe.UpdatedAt    = now;
                recipe.Name         = recipeViewModel.Name;
                recipe.Text         = recipeViewModel.Text;
                recipe.DetailedText = recipeViewModel.DetailedText;
                recipe.TimeNeeded   = recipeViewModel.TimeNeeded;

                var tags = tagsRepository.GetAllTags().Where(tag => recipeViewModel.SelectedTags.Contains(tag.Id));
                recipe.Tags = tags.Select(tag => new RecipeTag()
                {
                    Recipe = recipe, Tag = tag
                }).ToList();
                recipe.Ingredients = new List <RecipeIngredient>();

                foreach (var formFile in files)
                {
                    if (formFile.Length > 0)
                    {
                        using (var fileStream = formFile.OpenReadStream())
                            using (var ms = new MemoryStream())
                            {
                                fileStream.CopyTo(ms);
                                var    fileBytes = ms.ToArray();
                                string Image     = Convert.ToBase64String(fileBytes);
                                recipe.Image = Image;
                            }
                    }
                }

                foreach (var pair in recipeViewModel.SelectedIngredient.Zip(recipeViewModel.SelectedIngredientAmount, (ingredient, amount) => new { ingredient, amount }))
                {
                    var ingredient = ingredientsRepository.GetAllIngredients().FirstOrDefault(ing => ing.Id == pair.ingredient);
                    if (ingredient != null)
                    {
                        recipe.Ingredients.Add(new RecipeIngredient()
                        {
                            Recipe = recipe, Ingredient = ingredient, Amount = pair.amount
                        });
                    }
                }

                await recipesRepository.Add(recipe);

                return(RedirectToAction("Index"));
            }

            ViewData["ApplicationUserId"] = new SelectList(usersRepository.GetAllUsers(), "Id", "Id", recipe.ApplicationUserId);
            return(View(recipeViewModel));
        }
예제 #4
0
        public async Task CreateAsync(
            Guid userId, string title, bool isPrivate, string description, string image, TimeSpan?duration, int?servings, string notes,
            IList <Ingredient> ingredients, IList <Step> steps, IList <Tag> tags)
        {
            var recipe     = Recipe.Create(userId, title, isPrivate, description, image, duration, servings, notes, ingredients, steps);
            var recipeTags = await CreateRecipeTags(recipe, tags);

            recipe.AddTags(recipeTags);

            recipesRepository.Add(recipe);
            await unitOfWork.CommitAsync();
        }
예제 #5
0
        public async Task <IActionResult> Create([FromBody] RecipeDTO recipeDto)
        {
            User adminUser = await _usersRepository.GetByName("admin");

            var recipe = Recipe.Create(adminUser.Id, recipeDto.Name, recipeDto.Content, RecipeStatusType.Approved,
                                       recipeDto.PreparationTime, recipeDto.Servings, KitchenType.Unspecified);
            await _recipesRepository.Add(recipe);

            foreach (var ingredient in recipeDto.Ingredients)
            {
                await _ingredientsRepository.AddIngredientCustom(recipe.Id, ingredient.Category, ingredient.MeasurementUnit,
                                                                 ingredient.Name, ingredient.Quantity, ingredient.Cost, ingredient.Weight);
            }
            return(Ok(recipe));
        }
예제 #6
0
        public async Task <int> Create(RecipeRequest request)
        {
            var userId = _userContextService.GetUserId;

            if (userId is null)
            {
                throw new ForbidException();
            }

            var newRecipe = _mapper.Map <Recipe>(request);

            newRecipe.UserId = userId;

            await _recipesRepository.Add(newRecipe);

            return(newRecipe.Id);
        }
예제 #7
0
            public async Task <Result> Handle(AddRecipeCommand command)
            {
                var recipe = new Recipe(command.Title, command.Description, command.Slug, command.Category);

                try
                {
                    await _recipesRepository.Add(recipe);
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.
                    return(Result.Fail("Unable to save changes. " +
                                       "Try again, and if the problem persists " +
                                       "see your system administrator."));
                }

                return(Result.Ok());
            }