public IHttpActionResult Create(RecipeViewModel model) { Recipe recipe = new Recipe() { Name = model.Name, Description = model.Description }; foreach (RecipeProductsViewModel recipeProduct in model.Products) { Product product = _context.Products.Where(x => x.Id == recipeProduct.Product.Id).FirstOrDefault(); if (product != null) { recipe.RecipeProducts.Add(new RecipeProducts() { Product = product, Weight = recipeProduct.Weight }); } } _context.Recipes.Add(recipe); _context.SaveChanges(); return(Ok(RecipeMapper.RecipeViewModelMapper(recipe))); }
public IHttpActionResult GetRecipe(int id) { Recipe recipe = _context.Recipes.Include(x => x.RecipeProducts.Select(xx => xx.Product)).Include(x => x.Images).Where(x => x.Id == id && x.IsDeleted == false).FirstOrDefault(); if (recipe == null) { return(NotFound()); } return(Ok(RecipeMapper.RecipeViewModelMapper(recipe))); }
public IHttpActionResult GetRecipes() { ICollection <RecipeViewModel> recipes = _context.Recipes .Include(x => x.RecipeProducts.Select(xx => xx.Product)) .Include(x => x.Images) .Where(x => x.IsDeleted == false).ToList() .Select(x => RecipeMapper.RecipeViewModelMapper(x)).ToList(); return(Ok(recipes)); }