internal ReadRecipeDto CreateRecipe(CreateRecipeDto inRecipe, string userId, string userName) { var recipe = new Recipe { Title = inRecipe.Title, ShortDescription = inRecipe.ShortDescription, Description = inRecipe.Description, UserId = userId, UserName = userName, ImgUrl = inRecipe.ImgUrl, Ingredients = inRecipe.Ingredients, Tags = new List <Tag>(), Votes = new List <Vote>() }; foreach (var tag in inRecipe.Tags) { var foundTag = _tagRepository.GetTagByName(tag); recipe.Tags.Add(foundTag); } _recipeRepository.CreateRecipe(recipe); _recipeRepository.SaveChanges(); return(_mapper.Map <ReadRecipeDto>(recipe, opt => opt.Items["UserId"] = userId)); }
public IActionResult Create([FromBody] Recipe recipe) { try { var user = _userRepository.GetByAuthId(recipe.UserId); recipe.UserId = user.Id.ToString(); _recipeRepository.CreateRecipe(recipe, true); return(Ok()); } catch (Exception e) { _logger.LogError(e, e.Message); return(StatusCode(500)); } }
public IActionResult CreateRecipe([FromBody] RecipeDto recipeDto) { try { if (!ModelState.IsValid) { return(BadRequest()); } _recipeRepository.CreateRecipe(recipeDto); _log.Info("Creating a new recipe."); return(Ok($"Recipe was created successfully :)")); } catch (Exception e) { _log.Error($"Failed to create recipe. {e}"); return(BadRequest($"Failed to create recipe. {e}")); } }
public IActionResult CreteRecipe([FromQuery] List <int> authorsId, [FromQuery] List <int> categoriesId, [FromQuery] List <int> stepsId, [FromQuery] List <int> ingredientsId , [FromBody] Recipe recipeToCreate) { recipeToCreate.Country = _countryRepository.GetCountry(recipeToCreate.Country.Id); var statusCode = ValidateRecipe(authorsId, categoriesId, stepsId, ingredientsId, recipeToCreate); if (!ModelState.IsValid) { return(StatusCode(statusCode.StatusCode)); } if (!_recipeRepository.CreateRecipe(authorsId, categoriesId, stepsId, ingredientsId, recipeToCreate)) { ModelState.AddModelError("", $"Something went wrong saving the recipe " + $"{recipeToCreate.Name}"); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetRecipe", new { recipeId = recipeToCreate.Id }, recipeToCreate)); }
public async Task <Recipe> CreateRecipe(Recipe recipe) { await _ingridientService.GetIngridients(recipe.IngridientsIds); //Throws exception when one or more ingridients don't exist if (string.IsNullOrEmpty(recipe.Name)) { throw new EntityException("Recipe's name can't be null or empty"); } if (recipe.Time <= 0) { throw new EntityException("Time can't be null or negative"); } if (recipe.TotalCost <= 0) { throw new EntityException("Total cost can't be null or negative"); } return(await _recipeRepository.CreateRecipe(recipe)); }