예제 #1
0
        public async Task <ResponseDTO <BaseModelDTO> > AddRecipe(AddRecipeBindingModel recipeBindingModel, IFormFile photo)
        {
            var response = new ResponseDTO <BaseModelDTO>();
            var fileName = "";


            if (photo != null)
            {
                string photosFolder = "wwwroot\\RecipePhotos";
                if (!Directory.Exists(photosFolder))
                {
                    Directory.CreateDirectory(photosFolder);
                }

                fileName = Guid.NewGuid() + Path.GetExtension(photo.FileName);
                var path = Path.Combine(photosFolder, fileName);

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await photo.CopyToAsync(stream);
                }
            }

            var recipe = _mapper.Map <Recipe>(recipeBindingModel);
            var result = await _recipeRepository.AddAsyncBool(recipe);

            if (!result)
            {
                response.Errors.Add("Wystąpił problem podczas dodawania przepisu.");
                return(response);
            }

            return(response);
        }
예제 #2
0
        public Recipe AddRecipe(AddRecipeBindingModel model)
        {
            var recipe = new Recipe
            {
                Name        = model.Name,
                Description = model.Description
            };

            var recipeFoodItems = model.FoodItems.Select(fi => new RecipeFoodItem
            {
                Recipe        = recipe,
                FoodItem      = this.context.FoodItems.FirstOrDefault(fidb => fidb.Name == fi.Name),
                AmountInGrams = fi.AmountInGrams
            }).ToList();

            recipe.RecipeFoodItems = recipeFoodItems;
            this.context.Recipes.Add(recipe);
            this.context.SaveChanges();

            if (model.Image != null)
            {
                var recipeId = this.context.Recipes.First(r => r.Name == model.Name).Id;

                var imageLocation = this.imagesService.CreateImage(model.Image, this.GetType().Name.Replace("Service", string.Empty), recipeId);

                this.context.Recipes.First(r => r.Id == recipeId).ImageLocation = imageLocation;
                recipe.ImageLocation = imageLocation;
                context.SaveChanges();
            }

            return(recipe);
        }
예제 #3
0
        public IActionResult Add_Recipe(AddRecipeBindingModel model)
        {
            var foodItems = this.contentService.GetFoodItemsNames();

            this.ViewData["FoodItemsNames"] = foodItems.ToList();

            if (this.ModelState.IsValid)
            {
                if (model.AddFieldsCount != 0)
                {
                    for (int i = 0; i < model.AddFieldsCount; i++)
                    {
                        var newField = new AddRecipeBindingModel.FoodItemInRecipeBindingModel();
                        model.FoodItems.Add(newField);
                    }

                    model.AddFieldsCount = 0;

                    var reloadModeWithNewFields = this.menuService.GetMenuItems(this.GetType(), typeof(HttpGetAttribute), typeof(AuthorizeAttribute), AreaName, "Add_Recipe", model);
                    return(View("ContentMenu", reloadModeWithNewFields));
                }

                this.contentService.AddRecipe(model);
                return(RedirectToAction("ContentMenu"));
            }

            var reloadModel = this.menuService.GetMenuItems(this.GetType(), typeof(HttpGetAttribute), typeof(AuthorizeAttribute), AreaName, "Add_Recipe", model);

            return(View("ContentMenu", reloadModel));
        }
예제 #4
0
        public RecipeControllerTest()
        {
            //sample models
            addRecipe = new AddRecipeBindingModel {
                Title = "Recipe Added", Method = "Cook", Ingredients = "Food", Type = DietType.Vegan, PictureUrl = ""
            };

            //controller setup
            mockRepo         = new Mock <IRepositoryWrapper>();
            recipeController = new RecipesController(mockRepo.Object);
        }
예제 #5
0
        public IActionResult CreateRecipe([FromBody] AddRecipeBindingModel bm)
        {
            var newRecipe = new Recipe
            {
                Title       = bm.Title,
                Method      = bm.Method,
                Ingredients = bm.Ingredients,
                Type        = bm.Type,
                PictureUrl  = bm.PictureUrl,
            };
            //var createdRecipe = repo.Recipes.Create(newRecipe);
            //repo.Save();
            var createdRecipe = databases.Recipes.Add(newRecipe).Entity;

            databases.SaveChanges();
            return(Ok(createdRecipe.GetViewModel()));
        }
예제 #6
0
        public async Task <IActionResult> AddRecipe([FromForm] AddRecipeBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelStateErrors()));
            }

            var userId = User.Identity.Name;
            var result = await _recipeService.AddRecipe(model, model.photo);

            if (result.ErrorOccured)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
예제 #7
0
        public IActionResult CreateRecipe(AddRecipeBindingModel recipeBindingModel, IFormFile fileUpload)
        {
            //Quick Validation for file uploads to avoid errors
            string image;

            //check if there is a new file to upload
            if (fileUpload != null)
            {
                //get new image
                image = GetFileBase64Async(fileUpload).Result;
            }
            else
            {
                //assign to placeholder's binary
                image = NoImagePlaceholder;
            }

            //Validation for empty input
            if (recipeBindingModel.Title != null || recipeBindingModel.Ingredients != null || recipeBindingModel.Method != null)
            {
                //create an entry in the recipes table
                var recipeToCreate = new Recipe
                {
                    Title       = recipeBindingModel.Title,
                    Image       = image,
                    Ingredients = recipeBindingModel.Ingredients,
                    Method      = recipeBindingModel.Method,
                    Servings    = recipeBindingModel.Servings,
                };
                repo.Recipes.Create(recipeToCreate);
                repo.Save();

                //create an entry in the recipe cards table
                var recipeCardToCreate = new RecipeCard
                {
                    Recipe = recipeToCreate,
                    Title  = recipeToCreate.Title,
                    Image  = image,
                };
                repo.RecipeCards.Create(recipeCardToCreate);
                repo.Save();
            }

            return(RedirectToAction("Index"));
        }
예제 #8
0
        public IActionResult CreateRecipe(AddRecipeBindingModel bm)
        {
            var newRecipe = new Recipe
            {
                Title       = bm.Title,
                Method      = bm.Method,
                Ingredients = bm.Ingredients,
                Type        = bm.Type,
                PictureUrl  = bm.PictureUrl,
                //CreatedById = bm.CreatedById,
            };

            repo.Recipes.Create(newRecipe);
            repo.Save();
            //databases.Recipes.Add(newRecipe);
            //databases.SaveChanges();
            return(RedirectToAction("Index"));
        }
예제 #9
0
        public IActionResult CreateRecipe(AddRecipeBindingModel bindingModel, int foodID)
        {
            bindingModel.FoodID = foodID;
            var RecipeToCreate = new Recipe
            {
                RecipeName        = bindingModel.RecipeName,
                Ingredients       = bindingModel.Ingredients,
                Method            = bindingModel.Method,
                Servings          = bindingModel.Servings,
                FoodID            = bindingModel.FoodID,
                PictureURL        = "https://static01.nyt.com/images/2021/01/26/well/well-foods-microbiome/well-foods-microbiome-superJumbo.jpg",
                LevelofDifficulty = bindingModel.LevelofDifficulty,
                CreatedAt         = DateTime.Now
            };

            repository.Recipe.Create(RecipeToCreate); //adds the food created to the database
            repository.Save();                        //saves the changes
            return(RedirectToAction("Index"));        //will return page to index
        }
예제 #10
0
        public IActionResult CreateUserRecipe([FromBody] AddRecipeBindingModel bm)
        {
            var newRecipe = new Recipe
            {
                Title       = bm.Title,
                Method      = bm.Method,
                Ingredients = bm.Ingredients,
                Type        = bm.Type,
                PictureUrl  = bm.PictureUrl,
                //CreatedBy = repo.Users.FindByCondition(u => u.Id == bm.CreatedById)
                CreatedBy = databases.Users.FirstOrDefault(u => u.Id == bm.CreatedById),
            };
            //var createdRecipe = repo.Recipes.Create(newRecipe);
            //repo.Save();
            var createdRecipe = databases.Recipes.Add(newRecipe).Entity;

            databases.SaveChanges();
            return(Ok(createdRecipe.GetViewModel()));
        }
예제 #11
0
        public IActionResult CreateRecipe(AddRecipeBindingModel bm, int createdById)
        {
            bm.CreatedById = createdById;
            var newRecipe = new Recipe
            {
                Title       = bm.Title,
                Method      = bm.Method,
                Ingredients = bm.Ingredients,
                Type        = bm.Type,
                PictureUrl  = bm.PictureUrl,
                CreatedBy   = repo.Users.FindByCondition(u => u.Id == createdById).FirstOrDefault()
                              //CreatedBy = databases.Users.FirstOrDefault(u => u.Id == createdById)
            };

            repo.Recipes.Create(newRecipe);
            repo.Save();
            //databases.Recipes.Add(newRecipe);
            //databases.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public FoodControllerTest()
        {
            //mock setup
            foodMock  = new Mock <IFood>();
            foodsMock = new List <IFood> {
                foodMock.Object
            };
            addFoodMock    = new Mock <IAddFood>();
            updatefoodMock = new Mock <IUpdateFood>();
            food           = new Food();
            foods          = new List <Food>();

            //sample models
            addFoods = new AddFoodBindingModel {
                Name = "Lasagne", Cuisine = "Italian", Description = "layers of meat, cheese and pasta"
            };
            updateFood = new UpdateFoods {
                Name = "Lasagne", Cuisine = "English", Description = "layers of meat, cheese and pasta"
            };
            addRestaurant = new AddRestaurantBindingModel {
            };
            addRecipe     = new AddRecipeBindingModel {
            };

            //controller setup
            var recipeMock  = new Mock <IRecipe>();
            var recipesMock = new List <IRecipe>()
            {
                recipeMock.Object
            };
            var foodResultsMock = new Mock <IActionResult>();


            mockRepo       = new Mock <IRepositoryWrapper>();
            foodController = new FoodController(mockRepo.Object);
            var allfoods   = GetFoods();
            var allrecipes = GetRecipes();
        }
예제 #13
0
        public HomeControllerTest()
        {
            //mock setup
            recipeMock  = new Mock <IRecipe>();
            recipesMock = new List <IRecipe> {
                recipeMock.Object
            };
            recipe        = new Recipe();
            recipes       = new List <Recipe>();
            addRecipeMock = new Mock <IAddRecipe>();

            //sample model
            addRecipe = new AddRecipeBindingModel {
                ID = 1, Title = "Cake", Image = "image", Ingredients = "list of ingredients", Method = "steps", Servings = 4
            };

            //IFormFile setup
            //Idea to create an in-memory instance of a form file is from user harishr at:
            //https://stackoverflow.com/questions/36858542/how-to-mock-an-iformfile-for-a-unit-integration-test-in-asp-net-core-1-mvc-6/55953099
            //Idea to make "stream content" inside a string variable from user schlingel in the comments
            string fileContent = "hello";

            file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes(fileContent)), 0, fileContent.Length, "Data", fileContent);

            //controller setup
            var recipeCardMock  = new Mock <IRecipeCard>();
            var recipeCardsMock = new List <IRecipeCard>()
            {
                recipeCardMock.Object
            };

            //var courseResultsMock = new Mock<IActionResult>();

            mockRepo = new Mock <IRepositoryWrapper>();
            var allRecipes = GetRecipes();

            homeController = new HomeController(mockRepo.Object);
        }