Example #1
0
        private async Task <Recipe> MakeRecipe(RecipeCreateDTOin rec)
        {
            var tagsU  = rec.Tags.Select(x => x.ToUpper());
            var result = mapper.Map <Recipe>(rec);

            var dbTags = await this.tagRepo.All().Where(x => tagsU.Contains(x.NormalizedName)).Select(x => new { x.Id, x.NormalizedName }).ToArrayAsync();

            foreach (string tag in rec.Tags)
            {
                var tagFd = dbTags.FirstOrDefault(x => x.NormalizedName == tag.ToUpper());
                if (tagFd != null)
                {
                    result.RecipeTags.Add(new RecipeTag
                    {
                        TagId = tagFd.Id
                    });
                    continue;
                }
                result.RecipeTags.Add(new RecipeTag
                {
                    Tag = new Tag
                    {
                        Name           = tag,
                        NormalizedName = tag.ToUpper(),
                    }
                });
            }
            return(result);
        }
Example #2
0
        public async Task <Recipe> CreateAsync(RecipeCreateDTOin rec)
        {
            var result = await MakeRecipe(rec);

            await this.recipeRepo.AddAssync(result);

            await this.recipeRepo.SaveChangesAsync();

            return(result);
        }
Example #3
0
        public async Task <ActionResult <string> > Create(RecipeCreateDTOin recipe)
        {
            if (this.UserId != recipe.AuthorId)
            {
                return(BadRequest("UserId and AuthorId missmatch!"));
            }
            Recipe newRecipe = await this.recipeService.CreateAsync(recipe);

            if (newRecipe is null)
            {
                return(BadRequest("Recipe was not created!"));
            }
            return(newRecipe.Id);
        }