Пример #1
0
        public async System.Threading.Tasks.Task UpdateKitchenIngredient_Valid_ContextIsUpdatedAsync()
        {
            Kitchen           kitchen            = _testUser.KitchenUser.FirstOrDefault().Kitchen;
            KitchenIngredient ingredientToUpdate = kitchen?.KitchenIngredient.FirstOrDefault();

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


            string expectedNote = "hello world";
            int    expectedQty  = 2;

            ingredientToUpdate.Note     = expectedNote;
            ingredientToUpdate.Quantity = expectedQty;

            await _service.UpdateKitchenIngredientAsync(ingredientToUpdate, _testUser);

            KitchenIngredient updatedIngredient = _context.KitchenIngredient.Where(k => k.KitchenIngredientId == ingredientToUpdate.KitchenIngredientId).FirstOrDefault();

            Assert.Equal(expectedQty, updatedIngredient.Quantity);
            Assert.Equal(expectedNote, updatedIngredient.Note);
            Assert.True(updatedIngredient.LastUpdated.Date == DateTime.Now.Date);
        }
        public async Task <ActionResult> UpdateKitchenIngredientAsync(long id, [FromBody] KitchenIngredientDto kitchenIngredient)
        {
            PantryPlannerUser user;

            try
            {
                if (id != kitchenIngredient?.KitchenIngredientId)
                {
                    return(BadRequest("The ID specified does not match"));
                }

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


                await _service.UpdateKitchenIngredientAsync(kitchenIngredient, user);

                return(Ok());
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (IngredientNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (PermissionsException e)
            {
                return(Unauthorized(e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }