public async Task <IActionResult> SaveMealPlan(int?CustomRecipeId, int?RecipeId, DateTime CookDate, MealTimeType MealTime) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var recipe = new RecipeCalendar() { UserId = userId, RecipeId = RecipeId, CustomRecipeId = CustomRecipeId, CookDate = CookDate, MealTime = MealTime.ToString() }; var isExistingMealPlan = await _repositoryClient.VerifyMealPlanStatus(recipe); if (isExistingMealPlan) { return(RedirectToAction("MealCalendarError", "MealPlanning")); } else { var isMealPlanSaved = await _repositoryClient.SaveMealPlan(recipe); if (isMealPlanSaved) { return(RedirectToAction("MealCalendar", "MealPlanning")); } else { return(RedirectToAction("Error", "Home")); } } }
//Ueed to map recipes we retrieve of type RecipeCalendar to convert to type RecipeInfoViewModel public static async Task <RecipeInfoViewModel> MealMapping(RecipeCalendar recipe, ISearchRecipe recipeClient, IRepositoryClient repositoryClient) { if (recipe == null) { return(new RecipeInfoViewModel()); } string name; if (recipe.CustomRecipeId != null) { var recipeInfo = await repositoryClient.RetrieveCustomRecipe(recipe.UserId, recipe.CustomRecipeId); name = recipeInfo.RecipeName; } else { var recipeInfo = await recipeClient.SearchForRecipeById(recipe.RecipeId); name = recipeInfo.Title; } return(new RecipeInfoViewModel() { RecipeId = recipe.RecipeId, CustomRecipeId = recipe.CustomRecipeId, RecipeName = name }); }
//Used to verify if user already has an existing meal for the time and meal type public async Task <bool> VerifyMealPlanStatus(RecipeCalendar recipe) { var result = await _context.RecipeCalendars.FirstOrDefaultAsync(r => r.UserId == recipe.UserId && r.CookDate == recipe.CookDate && r.MealTime == recipe.MealTime); if (result == null) { return(false); } else { return(true); } }
//Add meal to the RecipeCalendar table. Returns true/false on success/failure public async Task <bool> SaveMealPlan(RecipeCalendar recipe) { try { await _context.RecipeCalendars.AddAsync(recipe); _context.SaveChanges(); return(true); } catch (Exception ex) { Console.WriteLine(ex); return(false); } }