Example #1
0
        public async Task <ActionResult> Post([FromBody] CreateRecipeDto model)
        {
            if (model == null || string.IsNullOrEmpty(model.Url))
            {
                return(BadRequest("No url"));
            }

            var uri      = new Uri(model.Url);
            var settings = this.siteSettingsProvider.GetSourceSiteSettings(uri.Host);

            if (settings == null)
            {
                return(BadRequest("No settings found for that site"));
            }

            if (await this.recipeProvider.Exist(model.Url))
            {
                return(BadRequest("Recipe already exist"));
            }

            var html         = this.scraper.Run(uri.OriginalString);
            var parsedRecipe = this.RecipeParser.Parse(html, settings);

            var recipe = new UpdateRecipeDto();

            recipe.Title       = parsedRecipe.Title;
            recipe.Image       = parsedRecipe.Image;
            recipe.Description = parsedRecipe.Description;
            recipe.SourceUrl   = model.Url;

            if (parsedRecipe.Tags != null)
            {
                recipe.Tags = string.Join(';', parsedRecipe.Tags);
            }

            var id = this.recipeProvider.Save(recipe);

            foreach (var ingredient in parsedRecipe.Ingredients)
            {
                var ingredientDto = new UpdateIngredientDto {
                    RecipeId = id,
                    Amount   = ingredient.Amount,
                    Unit     = ingredient.Unit,
                    Name     = ingredient.Name
                };

                this.ingredientProvider.Save(ingredientDto);
            }

            foreach (var step in parsedRecipe.CookingProcedureSteps)
            {
                var stepDto = new CookingProcedureDto {
                    RecipeId = id, Step = step
                };
                this.cookingProcedureProvider.Save(stepDto);
            }

            this.fileHandler.Download(recipe.Image, id.ToString() + ".jpg");
            return(Ok(id));
        }
Example #2
0
        public Ingredient FromDto(UpdateIngredientDto ingredient)
        {
            if (ingredient == null)
            {
                return(null);
            }

            var model = new Ingredient();

            model.Id       = ingredient.Id;
            model.RecipeId = ingredient.RecipeId;
            model.Amount   = ingredient.Amount;
            model.Unit     = ingredient.Unit;
            model.Name     = ingredient.Name;

            return(model);
        }
 public void Put(int id, [FromBody] UpdateIngredientDto ingredientDto)
 {
     this.ingredientProvider.Save(ingredientDto);
 }
Example #4
0
        public void Save(UpdateIngredientDto ingredient)
        {
            var model = this.mapper.FromDto(ingredient);

            this.ingredientRepository.Update(model);
        }