Ejemplo n.º 1
0
 public async Task <Recipe> InsertRecipe(
     [Service] IMutationService mutationService,
     [Service] IEventSender eventSender,
     InsertRecipeInput recipeInput)
 {
     return(await mutationService.InsertRecipe(recipeInput, eventSender));
 }
Ejemplo n.º 2
0
        public async Task <object> PostRecipe(InsertRecipeInput recipeInputRaw)
        {
            var userId = ParseUserId.GetUserId(Request.Headers);

            var recipeInput = recipeInputRaw.input;

            // parse the ingredient text and map it
            List <RecipeIngredient> recipeIngredients = new();

            if (recipeInput.ingredients is not null)
            {
                recipeIngredients = (await Task.WhenAll(recipeInput.ingredients
                                                        .Select(Parser.RunParser)))
                                    .ToList();
            }

            // map everything to their proper location for db write
            Recipe recipe = new()
            {
                Directions        = recipeInput.directions,
                Owner             = userId,
                Img               = recipeInput.img,
                TotalTime         = recipeInput.total_time,
                Yields            = recipeInput.yields,
                Cuisine           = recipeInput.cuisine,
                MealType          = recipeInput.meal_type,
                Title             = recipeInput.title,
                RecipeIngredients = recipeIngredients
            };


            _context.Recipes.Add(recipe);
            await _context.SaveChangesAsync();

            return(new { id = recipe.Id });
        }
    }
Ejemplo n.º 3
0
        public async Task <Recipe> InsertRecipe(InsertRecipeInput recipeInput, IEventSender eventSender)
        {
            var user = await userRepository.GetUserByIdWithRecipes(recipeInput.UserId);

            if (user == null)
            {
                return(null);
            }

            var recipe = new Recipe()
            {
                UserId   = user.Id,
                Title    = recipeInput.Title,
                PrepTime = recipeInput.PrepTime,
                Image    = recipeInput.Image,
                Kcal     = recipeInput.Kcal,
                Protein  = recipeInput.Protein,
                Fat      = recipeInput.Fat,
                Carbs    = recipeInput.Carbs
            };

            await recipeRepository.InsertRecipe(recipe);

            var savedRecipe = await recipeRepository.GetRecipeByTitle(recipe.Title);

            var foundTags = await tagRepository.GetTagsByNames(recipeInput.Tags.Select(t => t.Name).ToList());

            foreach (var inputTag in recipeInput.Tags)
            {
                if (foundTags.Any() && foundTags.Select(t => t.Name).Contains(inputTag.Name))
                {
                    var existingTag = foundTags.First(t => t.Name == inputTag.Name);

                    var recipeTag = new RecipeTag()
                    {
                        Recipe   = savedRecipe,
                        RecipeId = savedRecipe.Id,
                        Tag      = existingTag,
                        TagId    = existingTag.Id,
                    };

                    recipe.RecipeTags.Add(recipeTag);
                    existingTag.RecipeTags.Add(recipeTag);
                }
                else
                {
                    var tag = new Tag()
                    {
                        Name = inputTag.Name
                    };

                    await tagRepository.InsertTag(tag);

                    var savedTag = await tagRepository.GetTagByName(inputTag.Name);

                    var recipeTag = new RecipeTag()
                    {
                        Recipe   = savedRecipe,
                        RecipeId = savedRecipe.Id,
                        Tag      = savedTag,
                        TagId    = savedTag.Id,
                    };

                    recipe.RecipeTags.Add(recipeTag);
                    savedTag.RecipeTags.Add(recipeTag);
                }
            }

            await recipeRepository.Save();

            var userRecipeInfo = new UserRecipeInfo()
            {
                UserId      = user.Id,
                UserName    = user.Name,
                RecipeId    = savedRecipe.Id,
                RecipeCount = user.Recipes.Count
            };

            await eventSender.SendAsync(new OnInsertRecipeMessage(user.Id, userRecipeInfo));

            return(savedRecipe);
        }