示例#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));
        }
示例#2
0
        public CookingProcedureStep FromDto(CookingProcedureDto step)
        {
            if (step == null)
            {
                return(null);
            }

            var model = new CookingProcedureStep();

            model.Id       = step.Id;
            model.RecipeId = step.RecipeId;
            model.Step     = step.Step;

            return(model);
        }
示例#3
0
        public void Save(CookingProcedureDto step)
        {
            var model = this.mapper.FromDto(step);

            this.cookingProcedureRepository.Update(model);
        }
 public void Put(int id, [FromBody] CookingProcedureDto howTo)
 {
     this.cookingProcedureProvider.Save(howTo);
 }