Ejemplo n.º 1
0
        public async Task <IActionResult> CreateRecipe(CreateRecipeDto recipeDto)
        {
            if (await _recipeRepo.ExistsAsync(recipeDto.Id))
            {
                return(Conflict("Recipe already exists!"));
            }

            var recipe = new Recipe
            {
                Id                = recipeDto.Id,
                CategoryId        = recipeDto.CategoryId,
                Cook              = TestData.TestUser,
                DurationInMinutes = recipeDto.DurationInMinutes,
                Ingredients       = recipeDto.Ingredients,
                NumberOfPortions  = recipeDto.NumberOfPortions,
                Steps             = recipeDto.Steps,
                Story             = recipeDto.Story,
                Tags              = recipeDto.Tags,
                Title             = recipeDto.Title,
                UserId            = TestData.TestUser.Id
            };

            await _recipeRepo.CreateRecipeAsync(recipe);

            return(Ok());
        }
Ejemplo n.º 2
0
        public void ForkRecipeShouldThrowException()
        {
            var id        = _recipeRepository.Recipes[0].Id;
            var newRecipe = new CreateRecipeDto(_recipeRepository.Recipes[0].Name, _recipeRepository.Recipes[0].Description);

            _recipeRepository.Invoking(x => x.ForkRecipe(id, newRecipe)).Should().Throw <Exception>().WithMessage($"Recipe already exsts. Name: {newRecipe.Name}");
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <Recipe> > PostRecipe([FromBody] CreateRecipeDto toCreate)
        {
            var recipe = new Recipe()
            {
                Duration     = toCreate.Duration,
                Instructions = toCreate.Instructions,
                Name         = toCreate.Name,
                OriginalUrl  = toCreate.OriginalUrl,
                NumPortions  = toCreate.NumPortions,
                Categories   = toCreate.Categories,
                Tags         = toCreate.Tags,
                Ingredients  = toCreate.Ingredients.Select(dto => new RecipeIngredient()
                {
                    IngredientText = dto.IngredientText,
                    Modifier       = dto.Modifier,
                    Order          = dto.Order,
                    MinQuantity    = dto.MinQuantity,
                    MaxQuantity    = dto.MaxQuantity,
                    IngredientId   = dto.IngredientId,
                    Unit           = dto.Unit
                }).ToList()
            };

            _context.Recipes.Add(recipe);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetRecipe), new { id = recipe.Id }, recipe));
        }
Ejemplo n.º 5
0
        internal ReadRecipeDto UpdateRecipe(CreateRecipeDto inRecipe, int recipeId, string userId)
        {
            var recipe = _recipeRepository.GetRecipeById(recipeId);

            if (recipe.UserId != userId)
            {
                return(null);
            }

            recipe.Title            = inRecipe.Title;
            recipe.ShortDescription = inRecipe.ShortDescription;
            recipe.Description      = inRecipe.Description;
            recipe.ImgUrl           = inRecipe.ImgUrl;
            recipe.Ingredients      = inRecipe.Ingredients;
            recipe.Tags             = new List <Tag>();

            foreach (var tag in inRecipe.Tags)
            {
                var foundTag = _tagRepository.GetTagByName(tag);
                recipe.Tags.Add(foundTag);
            }

            _recipeRepository.UpdateRecipe(recipe);
            _recipeRepository.SaveChanges();

            return(_mapper.Map <ReadRecipeDto>(recipe, opt => opt.Items["UserId"] = userId));
        }
Ejemplo n.º 6
0
        internal ReadRecipeDto CreateRecipe(CreateRecipeDto inRecipe, string userId, string userName)
        {
            var recipe = new Recipe
            {
                Title            = inRecipe.Title,
                ShortDescription = inRecipe.ShortDescription,
                Description      = inRecipe.Description,
                UserId           = userId,
                UserName         = userName,
                ImgUrl           = inRecipe.ImgUrl,
                Ingredients      = inRecipe.Ingredients,
                Tags             = new List <Tag>(),
                Votes            = new List <Vote>()
            };

            foreach (var tag in inRecipe.Tags)
            {
                var foundTag = _tagRepository.GetTagByName(tag);
                recipe.Tags.Add(foundTag);
            }

            _recipeRepository.CreateRecipe(recipe);
            _recipeRepository.SaveChanges();

            return(_mapper.Map <ReadRecipeDto>(recipe, opt => opt.Items["UserId"] = userId));
        }
Ejemplo n.º 7
0
        public async Task <ActionResult <Recipe> > PostRecipe(CreateRecipeDto createRecipeData)
        {
            var recipe = new Recipe(createRecipeData);

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

            return(CreatedAtAction("GetRecipe", new { id = recipe.Id }, recipe));
        }
Ejemplo n.º 8
0
        public ActionResult <ReadRecipeDto> UpdateRecipe([FromBody] CreateRecipeDto recipe, int id)
        {
            var updatedRecipe = _recipeService.UpdateRecipe(recipe, id, GetUserId());

            if (updatedRecipe == null)
            {
                return(Forbid());
            }
            return(Ok(updatedRecipe));
        }
Ejemplo n.º 9
0
        public void ForkRecipeShouldBeCreated()
        {
            var id          = _recipeRepository.Recipes[0].Id;
            var name        = "testRecipe";
            var description = "testDescription";
            var newRecipe   = new CreateRecipeDto(name, description);

            _recipeRepository.ForkRecipe(id, newRecipe);

            _recipeRepository.GetAllRecipes().Should().Contain(x => x.Name == name && x.Description == description);
        }
Ejemplo n.º 10
0
        public void ForkRecipe(Guid rootId, CreateRecipeDto recipe)
        {
            var currentRecipe = GetNodeById(rootId);

            //if(currentRecipe.Children.Any(x => (x.Name == recipe.Name)))
            if (!(FindRecipeInTree(currentRecipe, x => (x.Name == recipe.Name)) is null))
            {
                throw new Exception($"Recipe already exsts. Name: {recipe.Name}");
            }

            currentRecipe.Children.Add(new Recipe(recipe.Name, recipe.Description, rootId));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> CreateRecipe(CreateRecipeDto model)
        {
            var medic = await _userManager.FindByIdAsync(model.MedicId);

            if (medic == null)
            {
                return(BadRequest("No such user"));
            }

            var user = await _userManager.FindByEmailAsync(model.PatientEmail);

            var recipe = new Recipe();

            if (await _userManager.IsInRoleAsync(medic, "Medic"))
            {
                recipe = new Recipe
                {
                    MedicId             = model.MedicId,
                    PatientId           = user.Id,
                    IsDeletedForMedic   = false,
                    IsDeletedForPatient = false,
                    DateCreated         = DateTime.Now
                };

                _uow.RecipeRepository.Add(recipe);
                _uow.Complete();
            }


            foreach (var r in model.RecipeDrugs)
            {
                var recipeDrug = _mapper.Map <RecipeDrug>(r);

                recipeDrug.IsSold = false;

                recipeDrug.RecipeId = recipe.Id;

                _uow.RecipeDrugRepository.Add(recipeDrug);
            }

            _uow.Complete();
            return(Ok());
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> Post([FromBody] CreateRecipeDto createDto)
        {
            var dto = await _recipesService.AddFull(createDto);

            return(NoContent());
        }
Ejemplo n.º 13
0
        public ActionResult <ReadRecipeDto> CreateRecipe([FromBody] CreateRecipeDto recipe)
        {
            var createdRecipe = _recipeService.CreateRecipe(recipe, GetUserId(), GetUserName());

            return(Ok(createdRecipe));
        }
Ejemplo n.º 14
0
 public async Task <IActionResult> ForkRecipe([FromRoute] Guid id, [FromBody] CreateRecipeDto recipe)
 {
     _recipeRepository.ForkRecipe(id, recipe);
     return(Ok());
 }