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))); }
/// <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()); } }
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); }); }
public void AddIngredient(Ingredient ingredient) => _ingredientService.AddIngredient(ingredient);
public ActionResult <Ingredient> Post(Ingredient ingredient) { var result = _ingredientService.AddIngredient(ingredient); return(Ok(result)); }