コード例 #1
0
        public async Task <IActionResult> PutReview(string userId, int recipeId, Review review)
        {
            if (userId != review.AppUserId || recipeId != review.RecipeId)
            {
                return(BadRequest());
            }

            if (!ReviewExists(userId, recipeId))
            {
                await PostReview(review);
            }
            else
            {
                _context.Entry(review).State = EntityState.Modified;

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException e)
                {
                    throw e;
                }
            }

            return(CreatedAtAction("GetReview", new { appUserId = review.AppUserId, recipeId = review.RecipeId }, review));
        }
コード例 #2
0
ファイル: RecipeItemsController.cs プロジェクト: Ananas97/TSD
        public async Task <IActionResult> PutRecipeItem(int id, RecipeItem recipeItem)
        {
            if (id != recipeItem.ID)
            {
                return(BadRequest());
            }

            _context.Entry(recipeItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> Post(UserFridgeIngredient userIngredient)
        {
            _context.Add(userIngredient);
            await _context.SaveChangesAsync();

            return(Ok(userIngredient.Id));
        }
コード例 #4
0
        public async Task <IHttpActionResult> PutIngredient(long id, Ingredient ingredient)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != ingredient.IngredientId)
            {
                return(BadRequest());
            }

            db.Entry(ingredient).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IngredientExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #5
0
        public async Task <IActionResult> PutCategory([FromRoute] int id, [FromBody] Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.ID)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #6
0
        public async Task <IActionResult> PutBrand(int id, Brand brand)
        {
            if (id != brand.Id)
            {
                return(BadRequest());
            }

            _context.Entry(brand).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BrandExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #7
0
        public async Task <IActionResult> Post(UserShoppingList userIngredient)
        {
            _context.Add(userIngredient);
            await _context.SaveChangesAsync();

            return(Ok(userIngredient));
        }
コード例 #8
0
        public async Task <bool> PutRecipe(long id, Recipe recipe)
        {
            _context.Entry(recipe).State = EntityState.Modified;

            foreach (var recipeIngredientMeasurement in recipe.RecipeIngredientMeasurement)
            {
                _context.Entry(recipeIngredientMeasurement.Ingredient).State  = EntityState.Modified;
                _context.Entry(recipeIngredientMeasurement.Measurement).State = EntityState.Modified;
            }

            foreach (var recipeTool in recipe.RecipeTool)
            {
                _context.Entry(recipeTool.Tool).State = EntityState.Modified;
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RecipeExists(id))
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }

            return(true);
        }
コード例 #9
0
        public async Task <IActionResult> PutFavouriteRecipe(string userId, int recipeId, FavouriteRecipe favouriteRecipe)
        {
            if (userId != favouriteRecipe.AppUserId || recipeId != favouriteRecipe.RecipeId)
            {
                return(BadRequest());
            }

            _context.Entry(favouriteRecipe).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FavouriteRecipeExists(userId, recipeId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #10
0
        public async Task Recipe_CanCreateBatch()
        {
            var recipe = new Recipe.Dal.Models.Recipe
            {
                Title           = "Add Test",
                ServingMeasure  = "Bites",
                ServingQuantity = 42
            };

            dc.Recipes.Add(recipe);
            for (int i = 0; i < 10; i++)
            {
                recipe.Ingredients.Add(new Ingredient {
                    Description = $"Ing {i}", SortOrder = i, Units = i.ToString(), UnitType = "pn"
                });
                recipe.Directions.Add(new Direction {
                    Description = $"Step {i} - Stir", LineNumber = i
                });
            }
            await dc.SaveChangesAsync();

            Assert.IsTrue(recipe.Id > 0);

            // Cleanup
            dc.Ingredients.RemoveRange(recipe.Ingredients);
            dc.Directions.RemoveRange(recipe.Directions);
            dc.Recipes.Remove(recipe);
            await dc.SaveChangesAsync();
        }
コード例 #11
0
        public async Task <IActionResult> Post(Ingredient ingredient)
        {
            _context.Add(ingredient);
            await _context.SaveChangesAsync();

            return(Ok(ingredient.Id));
        }
コード例 #12
0
        public async Task <IActionResult> Create(PreparationStepViewModel PreparationStepViewModel)
        {
            if (ModelState.IsValid)
            {
                var recipeId = PreparationStepViewModel.NewStep.RecipeId;
                var stepListCurrentRecipe = _context.PreparationSteps
                                            .Where(p => p.RecipeId == recipeId)
                                            .ToList();
                int highestRank;
                if (stepListCurrentRecipe.Count > 0)
                {
                    highestRank = stepListCurrentRecipe
                                  .Max(p => p.Rank);
                }
                else
                {
                    highestRank = 0;
                }

                PreparationStepViewModel.NewStep.Rank = ++highestRank;

                _context.Add(PreparationStepViewModel.NewStep);
                await _context.SaveChangesAsync();

                return(RedirectToAction("List", new { recipeId = recipeId }));
            }
            return(View());
        }
コード例 #13
0
ファイル: KeysController.cs プロジェクト: kasiawwww/RecipeApi
        public async Task <IActionResult> PutKey([FromRoute] string id, [FromBody] Key key)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != key.Name)
            {
                return(BadRequest());
            }

            _context.Entry(key).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!KeyExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #14
0
ファイル: UserController.cs プロジェクト: wongn27/Recipe
        public async Task <IActionResult> Post(User user)
        {
            _context.Add(user);
            await _context.SaveChangesAsync();

            return(Ok(user.Id));
        }
コード例 #15
0
        public async Task <IActionResult> Create([Bind("ID,Name,Products,Description,Time,Difficulty,Rating")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(recipe));
        }
コード例 #16
0
        public async Task <IActionResult> Create([Bind("RecipeId,RecipeTitle,RecipeDescription,RecipeUploadDate,RecipeUploader,Ingredient1,Ingredient2,Ingredient3,Ingredient4,Ingredient5,Ingredient6,Ingredient7,Ingredient8,Ingredient9,Ingredient10,Ingredient11,Ingredient12,Ingredient13,Ingredient14,Ingredient15,Ingredient16,Ingredient17,Ingredient18,Ingredient19,Ingredient20,StepNumber1,Step1Description,StepNumber2,Step2Description,StepNumber3,Step3Description,StepNumber4,Step4Description,StepNumber5,Step5Description,StepNumber6,Step6Description,StepNumber7,Step7Description,StepNumber8,Step8Description,StepNumber9,Step9Description,StepNumber10,Step10Description,StepNumber11,Step11Description,StepNumber12,Step12Description,StepNumber13,Step13Description,StepNumber14,Step14Description,StepNumber15,Step15Description,StepNumber16,Step16Description,StepNumber17,Step17Description,StepNumber18,Step18Description,StepNumber19,Step19Description,StepNumber20,Step20Description")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(recipe));
        }
コード例 #17
0
        public async Task <IActionResult> Create([Bind("Id,Name,Time,Difficulty,NumberOfLikes,Ingredients,Process,TipsAndTricks")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(recipe));
        }
コード例 #18
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction("List", "Ingredients", new { RecipeId = recipe.Id }));
            }
            return(View(recipe));
        }
コード例 #19
0
        public async Task <ActionResult> Create([Bind(Include = "ID,Name,EngName,Pieces,NamePieces,EngNamePieces,Teaspoon,Tablespoon")] Ingredient ingredient)
        {
            if (ModelState.IsValid)
            {
                db.Ingredients.Add(ingredient);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(ingredient));
        }
コード例 #20
0
        public async Task <ActionResult> Create([Bind(Include = "Id,Name,Type,Ingridient,Cooking")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                db.Recipes.Add(recipe);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(recipe));
        }
コード例 #21
0
        public async Task <IActionResult> Create(IngredientViewModel ingredientViewModel)
        {
            if (ModelState.IsValid)
            {
                var recipeId = ingredientViewModel.NewIngredient.RecipeId;

                _context.Add(ingredientViewModel.NewIngredient);
                await _context.SaveChangesAsync();

                return(RedirectToAction("List", new { recipeId = recipeId }));
            }
            return(View());
        }
コード例 #22
0
ファイル: RecipeController.cs プロジェクト: tomi/recipe-bank
        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));
        }
コード例 #23
0
        public async Task <ActionResult <Recipe> > PostRecipe(Recipe recipe)
        {
            _context.Recipe.Add(recipe);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetRecipe", new { id = recipe.RecipeId }, recipe));
        }
コード例 #24
0
        protected override async Task Handle(UpdateRecipeCommand request, CancellationToken cancellationToken)
        {
            var recipeName      = request.Recipe.Name;
            var recipeVariation = _mapper.Map <RecipeVariation>(request.Recipe);

            var existedRecipe = await _context.Recipes
                                .Include(r => r.Variations)
                                .FirstOrDefaultAsync(r => r.Name.Equals(recipeName), cancellationToken: cancellationToken)
                                .ConfigureAwait(false);

            if (existedRecipe == null)
            {
                throw new InvalidOperationException($"Recipe \"{recipeName}\" doesn't exist.");
            }

            var existedVariation = existedRecipe.Variations
                                   .FirstOrDefault(rv => rv.CreatedBy == recipeVariation.CreatedBy);

            if (existedVariation == null)
            {
                throw new InvalidOperationException($"There is no such recipe with name \"{recipeName}\" by current user. Please use Create method");
            }

            await _context.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);
        }
コード例 #25
0
        public async Task <ActionResult <RecipeItem> > PostRecipeItem(RecipeItem item)
        {
            _context.RecipeItems.Add(item);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetRecipeItem), new { id = item.Id }, item));
        }
コード例 #26
0
        public async Task Post(Ingredient Ingredient)
        {
            Ingredient.Id = Guid.NewGuid();
            db.Ingredients.Add(Ingredient);

            await db.SaveChangesAsync();
        }
コード例 #27
0
        public async Task Post(Recipe recipe)
        {
            recipe.Id = Guid.NewGuid();
            db.Recipes.Add(recipe);

            await db.SaveChangesAsync();
        }
コード例 #28
0
        public async Task <IActionResult> CreateRecipe([FromBody] RecipeCardViewModel recipeVM)
        {
            if (recipeVM == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var recipe = _context.Recipes.Add(
                new Recipe()
            {
                ImageURL    = recipeVM.ImageURL,
                IsFavorite  = recipeVM.Favorite,
                Name        = recipeVM.Name,
                Portions    = recipeVM.Portions,
                TotalTime   = recipeVM.TotalTime,
                WorkingTime = recipeVM.WorkingTime
            }
                );
            await _context.SaveChangesAsync();

            return(Ok());
        }
コード例 #29
0
        public async Task <StatusCodeResult> AddDownloadURL(Recipe r)
        {
            _context.recipe.Attach(r);
            _context.Entry(r).Property(x => x.ImagePath).IsModified = true;
            await _context.SaveChangesAsync();

            return(new StatusCodeResult(200));
        }
コード例 #30
0
        public async Task RemoveRecipeWithProduct(int productId)
        {
            IEnumerable <Models.Recipe> recipes = context.Recipes.Where(recipe => recipe.Products.Any(product => product.ProductId == productId));

            context.RemoveRange(recipes);

            await context.SaveChangesAsync();
        }