Exemplo n.º 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));
        }
        public async Task <GetRecipeDto?> UpdateRecipe(UpdateRecipeDto updatedRecipe)
        {
            try
            {
                Recipe?recipe = await _context.Recipes.FindAsync(updatedRecipe.Id);

                RecipeCategory?category = await _context.RecipeCategories.FindAsync(updatedRecipe.recipeCategoryId);

                if (recipe is null || category is null)
                {
                    return(null);
                }

                _mapper.Map(updatedRecipe, recipe);
                recipe.RecipeCategory = category;

                await _context.SaveChangesAsync();

                return(_mapper.Map <GetRecipeDto>(recipe));
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        public void UpdateRecipeShouldThrowException()
        {
            var id                 = _recipeRepository.Recipes[0].Id;
            var updatedName        = "";
            var updatedDescription = "updatedDescription";
            var updatedRecipe      = new UpdateRecipeDto(updatedName, updatedDescription, id);

            _recipeRepository.Invoking(x => x.UpdateRecipe(updatedRecipe)).Should().Throw <Exception>().WithMessage($"Validation exception. Name: {updatedName}, Description: {updatedDescription}");
        }
Exemplo n.º 4
0
        /// <summary>Puts updated recipe to Cookbook.API</summary>
        public static async Task UpdateRecipeAsync(UpdateRecipeDto recipe)
        {
            var result = await _client.PutAsJsonAsync("api/recipes", recipe);

            if (!result.IsSuccessStatusCode)
            {
                throw new Exception(result.ReasonPhrase);
            }
        }
Exemplo n.º 5
0
        public int Save(UpdateRecipeDto recipe)
        {
            var model = this.mapper.ToDao(recipe);

            model.CreatedAtUtc = DateTime.UtcNow.ToString();
            model.UpdatedAtUtc = DateTime.UtcNow.ToString();

            return(this.recipeRepository.Update(model));
        }
Exemplo n.º 6
0
        public void UpdateRecipeShouldModifyNode()
        {
            var id                 = _recipeRepository.Recipes[0].Id;
            var updatedName        = "updatedName";
            var updatedDescription = "updatedDescription";
            var updatedRecipe      = new UpdateRecipeDto(updatedName, updatedDescription, id);

            _recipeRepository.Invoking(x => x.UpdateRecipe(updatedRecipe)).Should().NotThrow();
            _recipeRepository.GetAllRecipes().Should().Contain(x => x.Name == updatedName && x.Description == updatedDescription);
        }
Exemplo n.º 7
0
 public IActionResult UpdateRecipe(UpdateRecipeDto recipe)
 {
     if (_repo.UpdateRecipe(recipe.Title, recipe.Description, recipe.RecipeID))
     {
         return(new OkResult());
     }
     else
     {
         return(new UnprocessableEntityResult());
     }
 }
Exemplo n.º 8
0
        public IActionResult UpdateRecipe(UpdateRecipeDto updatedRecipe)
        {
            var recipe = _recipeRepo.GetRecipe(updatedRecipe.Id, includeIngredients: true);

            if (recipe == null)
            {
                return(NotFound());
            }
            _mapper.Map(updatedRecipe, recipe);
            _recipeRepo.Update(recipe);
            return(NoContent());
        }
Exemplo n.º 9
0
        public void UpdateRecipe_ReturnsUnprocessableEntityResult_IfUpdateWasUnsuccessful()
        {
            //Arrange
            var repoMock = new Mock <IRecipesRepository>();

            repoMock.Setup(repo => repo.UpdateRecipe(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>())).Returns(false);
            var controller      = new RecipesController(repoMock.Object);
            var updateRecipeDto = new UpdateRecipeDto();

            //Act
            var result = controller.UpdateRecipe(updateRecipeDto);

            //Assert
            Assert.IsType <UnprocessableEntityResult>(result);
        }
Exemplo n.º 10
0
        public Recipe ToDao(UpdateRecipeDto recipe)
        {
            if (recipe == null)
            {
                return(null);
            }

            var model = new Recipe();

            model.Id          = recipe.Id;
            model.Title       = recipe.Title;
            model.Image       = recipe.Image;
            model.Description = recipe.Description;
            model.SourceUrl   = recipe.SourceUrl;
            model.Tags        = recipe.Tags?.ToLowerInvariant();
            return(model);
        }
Exemplo n.º 11
0
    public void UpdateRecipe(UpdateRecipeDto existingRecipeDto)
    {
        var recipe = SimpleCrudHelper.Find <Recipe>(existingRecipeDto.RecipeId);

        foreach (var ingredientId in recipe.Ingredients.Select(x => x.IngredientId).ToList())
        {
            SimpleCrudHelper.Delete <Ingredient>(ingredientId);
        }

        var newIngredients = new List <Ingredient>();

        foreach (var newIngredientDto in existingRecipeDto.Ingredients)
        {
            var unit    = SimpleCrudHelper.Find <Unit>(newIngredientDto.Unit.UnitId);
            var article = SimpleCrudHelper.Find <Article>(newIngredientDto.Article.ArticleId);
            newIngredients.Add(Context.Ingredients.Add(new Ingredient(article, newIngredientDto.Quantity, unit)).Entity);
        }

        recipe.Name         = existingRecipeDto.Name;
        recipe.NumberOfDays = existingRecipeDto.NumberOfDays;
        recipe.Ingredients  = newIngredients;

        Context.SaveChanges();
    }
Exemplo n.º 12
0
 public IActionResult Put(int id, [FromBody] UpdateRecipeDto recipe) =>
 _mapper.Map <RecipeUpdateModel>(recipe)
 .Map(x => _commandRepository.Update(id, x))
 .Map(x => AllOk(new { updated = x }))
 .Reduce(_ => NotFound(), error => error is RecordNotFound, x => _logger.LogError(x.ToString()))
 .Reduce(_ => InternalServerError(), x => _logger.LogError(x.ToString()));
Exemplo n.º 13
0
 public void UpdateRecipe(UpdateRecipeDto updatedRecipe)
 {
     GetNodeById(updatedRecipe.Id).UpdateData(updatedRecipe.Name, updatedRecipe.Description);
 }
Exemplo n.º 14
0
 public void Put(int id, [FromBody] UpdateRecipeDto recipe)
 {
     this.recipeProvider.Save(recipe);
 }
Exemplo n.º 15
0
        public async Task <IActionResult> UpdateRecipe(UpdateRecipeDto updatedRecipe)
        {
            var result = await _recipeRepository.UpdateRecipe(updatedRecipe);

            return(result != null?Ok(result) : BadRequest());
        }
Exemplo n.º 16
0
 public async Task <IActionResult> UpdateNode([FromBody] UpdateRecipeDto recipe)
 {
     _recipeRepository.UpdateRecipe(recipe);
     return(Ok());
 }