public async Task <ActionResult <IngredientDto> > AddIngredient(Ingredient ingredient)
        {
            PantryPlannerUser user;

            try
            {
                user = await _userManager.GetUserFromCookieOrJwtAsync(this.User);
            }
            catch (PermissionsException e)
            {
                // this will be thrown if the user is null
                return(Unauthorized(e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }


            try
            {
                _service.AddIngredient(ingredient, user);
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (UserNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (InvalidOperationException e)
            {
                return(StatusCode(StatusCodes.Status405MethodNotAllowed, e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }

            return(StatusCode(StatusCodes.Status201Created, new IngredientDto(ingredient)));
        }
예제 #2
0
        /// <summary>
        /// Method for adding new Ingredients
        /// </summary>
        private void SaveExecute()
        {
            try
            {
                Validations val = new Validations();
                if (val.IngredientNameChecker(Ingredient.IngredientName) != null)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show(val.IngredientNameChecker(Ingredient.IngredientName), "Naziv sastojka");
                    return;
                }

                ingrediantsData.AddIngredient(Ingredient);
                isUpdateIngredient = true;
                addIngredient.Close();
            }
            catch (Exception ex)
            {
                Xceed.Wpf.Toolkit.MessageBox.Show(ex.ToString());
            }
        }
예제 #3
0
        public void DeleteIngredient_UserWithNoRights_ThrowsPermissionsExceptions()
        {
            Category dairyCat = _context.Category.Where(c => c.Name == "Dairy and Egg Products").FirstOrDefault();

            Ingredient ingredient = new Ingredient()
            {
                Name        = "Buttery butter",
                Description = "better than butter",
                CategoryId  = dairyCat.CategoryId,
                IsPublic    = true
            };

            _ingredientService.AddIngredient(ingredient);

            Assert.Throws <PermissionsException>(() =>
            {
                _ingredientService.DeleteIngredient(ingredient, _testUser);
            });
        }
예제 #4
0
 public void AddIngredient(Ingredient ingredient) => _ingredientService.AddIngredient(ingredient);
        public ActionResult <Ingredient> Post(Ingredient ingredient)
        {
            var result = _ingredientService.AddIngredient(ingredient);

            return(Ok(result));
        }