public Recipe GetRecipeByIdAndCookId(int recipeId, int cookId)
        {
            DAL.Recipe recipe = this.cookbookDbContext.recipes.FirstOrDefault(r => r.RecipeId == recipeId && r.CookId == cookId);

            if (recipe is null)
            {
                throw new RecordNotFoundException($"No record found.");
            }

            IEnumerable <Ingredient> ingredients = this.cookbookDbContext.recipeIngredients
                                                   .Where(ri => ri.RecipeId == recipe.RecipeId)
                                                   .Select(ri => new Ingredient
            {
                IngredientId = ri.IngredientId,
                Name         = ri.Ingredient.Name
            });

            return(new Recipe
            {
                RecipeId = recipe.RecipeId,
                Name = recipe.Name,
                Description = recipe.Description,
                Ingredients = ingredients.ToList()
            });
        }
Beispiel #2
0
        public int Insert(string email, string recipeName, PreparedRecipe preparedRecipe)
        {
            if (this.cookbookDbContext.preparedRecipes.Any(pr => pr.Cook.Email == email && pr.Recipe.Name == recipeName && pr.Alias == preparedRecipe.Alias && !pr.Complete))
            {
                throw new RecordAlreadyExistException("Record alread exist.");
            }

            DAL.Cook cook = this.cookbookDbContext.cooks.FirstOrDefault(c => c.Email == email);

            DAL.Recipe recipe = this.cookbookDbContext.recipes.FirstOrDefault(r => r.Name == recipeName);

            if (cook is null || recipe is null)
            {
                throw new RecordNotFoundException("Record not found.");
            }

            DAL.PreparedRecipe newPreparedRecipe = new DAL.PreparedRecipe
            {
                Alias        = preparedRecipe.Alias,
                CookId       = cook.CookId,
                RecipeId     = recipe.RecipeId,
                Complete     = false,
                PreparedWhen = DateTime.Now,
            };

            this.cookbookDbContext.preparedRecipes.Add(newPreparedRecipe);
            this.cookbookDbContext.SaveChanges();

            return(newPreparedRecipe.PreparedRecipeId);
        }
        public int Insert(int cookId, Recipe recipe)
        {
            if (this.cookbookDbContext.recipes.Any(r => r.Name == recipe.Name && r.CookId == cookId))
            {
                throw new RecordAlreadyExistException($"Record already exists.");
            }

            DAL.Recipe newRecipe = new DAL.Recipe
            {
                Name        = recipe.Name,
                Description = recipe.Description,
                CookId      = cookId
            };

            this.cookbookDbContext.recipes.Add(newRecipe);

            return(newRecipe.RecipeId);
        }
        public int Insert(string email, Recipe recipe)
        {
            if (this.cookbookDbContext.recipes.Any(r => r.Name == recipe.Name && r.Cook.Email == email))
            {
                throw new RecordAlreadyExistException($"Record already exists.");
            }

            DAL.Cook cook = this.cookbookDbContext.cooks.FirstOrDefault(c => c.Email == email);
            if (cook is null)
            {
                throw new RecordNotFoundException("Record not found.");
            }

            DAL.Recipe newRecipe = new DAL.Recipe
            {
                Name        = recipe.Name,
                Description = recipe.Description,
                CookId      = cook.CookId
            };

            this.cookbookDbContext.recipes.Add(newRecipe);

            return(newRecipe.RecipeId);
        }