public async Task <ActionResult <KitchenIngredientDto> > AddKitchenIngredientAsync([FromBody] KitchenIngredientDto kitchenIngredient) { PantryPlannerUser user; try { user = await _userManager.GetUserFromCookieOrJwtAsync(this.User); KitchenIngredient kitchenIngredientToAdd = KitchenIngredientDto.Create(kitchenIngredient); KitchenIngredient newIngredient = _service.AddKitchenIngredient(kitchenIngredientToAdd, user); return(Ok(new KitchenIngredientDto(newIngredient))); } catch (ArgumentNullException e) { return(BadRequest(e.Message)); } catch (UserNotFoundException e) { return(NotFound(e.Message)); } catch (InvalidOperationException e) { return(StatusCode(StatusCodes.Status405MethodNotAllowed, e.Message)); } catch (PermissionsException e) { return(Unauthorized(e.Message)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public void AddKitchenIngredient_ValidIngredientAndUser_ContextIsUpdated() { Kitchen kitchen = _testUser.KitchenUser.FirstOrDefault().Kitchen; List <long> existingIngredients = kitchen.KitchenIngredient.Select(ki => ki.IngredientId).ToList(); Ingredient ingredient = _context.Ingredient.Where(i => existingIngredients.Contains(i.IngredientId) == false).FirstOrDefault(); if (ingredient == null || kitchen == null) { throw new ArgumentNullException("ingredient or kitchen is not setup for testing"); } KitchenIngredient ingredientToAdd = new KitchenIngredient() { KitchenId = kitchen.KitchenId, IngredientId = ingredient.IngredientId, }; int countBeforeAdd = kitchen.KitchenIngredient.Count; _service.AddKitchenIngredient(ingredientToAdd, _testUser); Assert.Equal(countBeforeAdd + 1, kitchen.KitchenIngredient.Count); Assert.NotNull(_context.KitchenIngredient.Where(i => i.KitchenIngredientId == ingredientToAdd.KitchenIngredientId).FirstOrDefault()); }