public IngredientDTO AddNewIngredient(PostIngredientDTO ingredientDTO, string userId) { var newIngredient = _ingredientFactory.Create(ingredientDTO); newIngredient.UserId = userId; _uow.Ingredients.Add(newIngredient); _uow.SaveChanges(); return(_ingredientFactory.Create(newIngredient)); }
public Ingredient Create(PostIngredientDTO ingredientDTO) { return(new Ingredient { Name = ingredientDTO.Name, Description = ingredientDTO.Description, AmountUnit = ingredientDTO.AmountUnit }); }
public IActionResult PostIngredient([FromBody] PostIngredientDTO ingredientDTO) { _requestLogService.SaveRequest(User.Identity.GetUserId(), "POST", "api/v1/ingredients", "PostIngredient"); if (!ModelState.IsValid) { return(BadRequest("Invalid fields provided, please double check the parameters")); } var newIngredient = _ingredientService.AddNewIngredient(ingredientDTO, User.Identity.GetUserId()); return(CreatedAtAction(nameof(GetIngredient), new { id = newIngredient.IngredientId }, newIngredient)); }
public IngredientDTO UpdateIngredient(int id, PostIngredientDTO updatedIngredientDTO) { if (_uow.Ingredients.Exists(id)) { Ingredient ingredient = _uow.Ingredients.Find(id); ingredient.Name = updatedIngredientDTO.Name; ingredient.Description = updatedIngredientDTO.Description; ingredient.AmountUnit = updatedIngredientDTO.AmountUnit; _uow.Ingredients.Update(ingredient); _uow.SaveChanges(); } return(GetIngredientById(id)); }
public IActionResult UpdateIngredient(int id, [FromBody] PostIngredientDTO ingredientDTO) { _requestLogService.SaveRequest(User.Identity.GetUserId(), "PUT", "api/v1/ingredients", "UpdateIngredient"); if (!ModelState.IsValid) { return(BadRequest("Invalid fields provided, please double check the parameters")); } var ingredient = _ingredientService.GetIngredientById(id); if (ingredient == null) { return(NotFound()); } if (ingredient.UserId != User.Identity.GetUserId()) { return(StatusCode(403, "Ingredients can be amended only by admins or users that created them. Please provide id of ingredient that is created by user.")); } IngredientDTO updatedIngredient = _ingredientService.UpdateIngredient(id, ingredientDTO); return(Ok(updatedIngredient)); }