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 Cook GetByCookId(int id) { DAL.Cook cook = this.cookbookDbContext.cooks .Include(c => c.Recipes) .FirstOrDefault(c => c.CookId == id); if (cook is null) { throw new RecordNotFoundException($"No record found."); } List <Recipe> recipes = null; if (cook.Recipes.Any()) { recipes = new List <Recipe>(); foreach (var recipe in cook.Recipes) { var ingredients = this.cookbookDbContext.recipeIngredients.Where(ri => ri.RecipeId == recipe.RecipeId) .Select(ri => new Ingredient { IngredientId = ri.IngredientId, Name = ri.Ingredient.Name }); var preparedRecipes = this.cookbookDbContext.preparedRecipes.Where(pr => pr.RecipeId == recipe.RecipeId) .Select(pr => new PreparedRecipe { Alias = string.IsNullOrEmpty(pr.Alias) ? "Empty Recipe Alias" : pr.Alias, Complete = pr.Complete, PreparedWhen = pr.PreparedWhen.ToString("MMMM dd, yyyy hh:mm tt"), PreparedRecipeId = pr.PreparedRecipeId }); recipes.Add(new Recipe { RecipeId = recipe.RecipeId, Name = recipe.Name, Description = recipe.Description, Ingredients = ingredients.ToList(), PreparedRecipes = preparedRecipes.ToList() }); } } return(new Cook { CookId = cook.CookId, Email = cook.Email, FirstName = cook.FirstName, LastName = cook.LastName, Recipes = recipes }); }
public int Insert(Cook cook) { if (this.cookbookDbContext.cooks.Any(c => c.Email == cook.Email)) { throw new RecordAlreadyExistException("Record already exist."); } DAL.Cook newCook = new DAL.Cook { Email = cook.Email, FirstName = cook.FirstName, LastName = cook.LastName, }; this.cookbookDbContext.cooks.Add(newCook); return(newCook.CookId); }
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); }