public async Task <ActionResult <KitchenIngredientDto> > AddIngredientToKitchenAsync(long kitchenId, long ingredientId)
        {
            PantryPlannerUser user;

            try
            {
                user = await _userManager.GetUserFromCookieOrJwtAsync(this.User);

                KitchenIngredient newIngredient = _service.AddIngredientToKitchen(ingredientId, kitchenId, user);
                return(Ok(new KitchenIngredientDto(newIngredient)));
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (KitchenNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (IngredientNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (UserNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (PermissionsException e)
            {
                return(Unauthorized(e.Message));
            }
            catch (InvalidOperationException e)
            {
                return(StatusCode(StatusCodes.Status405MethodNotAllowed, e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Ejemplo n.º 2
0
        public void GetKitchenIngredientsByName_ValidKitchenAndUser_ReturnsCorrectResult()
        {
            Kitchen kitchen = _testUser.KitchenUser.FirstOrDefault().Kitchen;

            if (kitchen == null)
            {
                throw new ArgumentNullException("kitchen is not setup for testing");
            }

            // add two new ingredients to the database
            Ingredient testIngredient = new Ingredient()
            {
                AddedByUserId = _testUser.Id,
                Name          = "test ingredient 1"
            };

            Ingredient testIngredient2 = new Ingredient()
            {
                AddedByUserId = _testUser.Id,
                Name          = "test ingredient 2"
            };

            _context.Ingredient.Add(testIngredient);
            _context.Ingredient.Add(testIngredient2);
            _context.SaveChanges();

            // make sure the new ingredients are in the kitchen
            _service.AddIngredientToKitchen(testIngredient, kitchen, _testUser);
            _service.AddIngredientToKitchen(testIngredient2, kitchen, _testUser);


            List <KitchenIngredient> ingredients = _service.GetKitchenIngredientsByName(kitchen, "test ingredient", _testUser);


            Assert.Equal(2, ingredients.Count);
        }