Exemple #1
0
        public async Task <IActionResult> PutRecipe(int id, Recipe apiRecipe)
        {
            var userId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (userId != apiRecipe.AuthorId)
            {
                return(Unauthorized());
            }

            if (id != apiRecipe.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <ActionResult <ApiRecipeInCollection> > PostRecipeInCollection(
            ApiRecipeInCollection apiRecipeInCollection)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (apiRecipeInCollection.CollectionUserId != currentUserId)
            {
                return(Unauthorized());
            }

            var recipeInCollection = new RecipeInCollection()
            {
                CollectionTitle  = apiRecipeInCollection.CollectionTitle,
                RecipeId         = apiRecipeInCollection.RecipeId,
                CollectionUserId = apiRecipeInCollection.CollectionUserId
            };

            _context.RecipeInCollection.Add(recipeInCollection);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (RecipeInCollectionExists(recipeInCollection))
                {
                    return(Conflict());
                }

                throw;
            }

            return(CreatedAtAction("GetRecipeInCollection", new { id = apiRecipeInCollection.RecipeId },
                                   apiRecipeInCollection));
        }
        public async Task <IActionResult> PutIngrInShoppingList(ApiIngrInShoppingList apiIngredient)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (apiIngredient.UserId != currentUserId)
            {
                return(Unauthorized());
            }

            var query = from ing in _context.IngrInShoppingList where ing.IngrName == apiIngredient.IngrName select ing;

            foreach (var ingrInShoppingList in query)
            {
                _context.Entry(ingrInShoppingList).State = EntityState.Deleted;
            }

            var updatedIngredient = apiIngredient.ToIngrInShoppingList();
            await _context.IngrInShoppingList.AddAsync(updatedIngredient);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IngrInShoppingListExists(updatedIngredient))
                {
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutSupplies(string userId, QuantifiedIngredient quantifiedIngredient)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (userId != currentUserId)
            {
                return(Unauthorized());
            }

            var query = from s in _context.Supplies where s.IngrName == quantifiedIngredient.Ingredient.Name select s;

            foreach (var s in query)
            {
                _context.Entry(s).State = EntityState.Deleted;
            }
            var updatedSupplies = new Supplies()
            {
                Quantity = quantifiedIngredient.Quantity,
                UnitName = quantifiedIngredient.Unit.Name,
                IngrName = quantifiedIngredient.Ingredient.Name,
                UserId   = userId
            };

            await _context.Supplies.AddAsync(updatedSupplies);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SuppliesExists(updatedSupplies))
                {
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }
Exemple #5
0
        public async Task <IActionResult> PutApiUses(ApiUses apiUses)
        {
            var query = from u in _context.Uses where u.IngrName == apiUses.IngrName select u;

            foreach (var u in query)
            {
                _context.Entry(u).State = EntityState.Deleted;
            }

            Uses uses = new Uses
            {
                RecipeId = apiUses.RecipeId,
                IngrName = apiUses.IngrName,
                Quantity = apiUses.Quantity,
                UnitName = apiUses.UnitName
            };
            await _context.Uses.AddAsync(uses);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {
                System.Console.WriteLine(e.Message);
                if (!ApiUsesExists(apiUses.RecipeId, apiUses.UnitName, apiUses.IngrName))
                {
                    System.Console.WriteLine(apiUses.RecipeId);
                    System.Console.WriteLine(apiUses.UnitName);
                    System.Console.WriteLine(apiUses.IngrName);
                    return(NotFound());
                }

                throw;
            }

            return(NoContent());
        }
        public async Task <ActionResult <ApiRecipeCollection> > PostRecipeCollection(ApiRecipeCollection apiRecipeCollection)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (currentUserId != apiRecipeCollection.UserId)
            {
                return(Unauthorized());
            }

            var collection = new RecipeCollection
            {
                UserId      = apiRecipeCollection.UserId,
                Title       = apiRecipeCollection.Title,
                Description = apiRecipeCollection.Description,
                Visibility  = apiRecipeCollection.Visibility
            };

            _context.RecipeCollection.Add(collection);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (RecipeCollectionExists(collection))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetRecipeCollection", new { id = apiRecipeCollection.UserId }, apiRecipeCollection));
        }
        public async Task <ActionResult <ApiRecipeRating> > PostRecipeRating(ApiRecipeRating apiRecipeRating)
        {
            //convert api model to regular model
            var recipeRating = RecipeRating.FromApiToRecipeRating(apiRecipeRating);
            await _context.RecipeRating.AddAsync(recipeRating);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (RecipeRatingExists(recipeRating))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetRecipeRating", new { id = apiRecipeRating.UserId }, apiRecipeRating));
        }
        public async Task <IActionResult> PutSupplier(string id, ApiSupplier apiSupplier)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (id != currentUserId)
            {
                return(Unauthorized());
            }

            if (id != apiSupplier.UserId)
            {
                return(BadRequest());
            }

            var supplier = new Supplier()
            {
                Email           = apiSupplier.Email,
                Website         = apiSupplier.Website,
                PhoneNo         = apiSupplier.PhoneNo,
                SupplierName    = apiSupplier.SupplierName,
                UserId          = apiSupplier.UserId,
                StoreVisitCount = apiSupplier.StoreVisitCount
            };

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

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

            return(NoContent());
        }
        public async Task <IActionResult> PutCustomer(string id, ApiCustomer apiCustomer)
        {
            var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (id != currentUserId)
            {
                return(Unauthorized());
            }

            if (id != apiCustomer.UserId)
            {
                return(BadRequest());
            }

            var customer = new Customer()
            {
                Age          = apiCustomer.Age,
                Weight       = apiCustomer.Weight,
                FavMeal      = apiCustomer.FavMeal,
                UserId       = apiCustomer.UserId,
                CustomerName = apiCustomer.CustomerName
            };

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

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

                throw;
            }

            return(NoContent());
        }