public async Task GenerateEatingPlanDontGenerateMealsForUsersPreferences( params SoftGym.Data.Models.Enums.FoodPreference[] foodPreferences) { var service = await this.Before(); var user = new ApplicationUser(); this.usersService.Setup(x => x.GetUserByIdAsync(user.Id)) .Returns(Task.FromResult(user)); var inputModel = new GenerateInputModel { Activity = "light", Age = 25, HasUserActivePlan = false, DurationInDays = 30, FoodPreferences = foodPreferences, Gender = "Male", Goal = Goal.Mantain, Height = 190, Id = user.Id, Weight = 85, }; var result = await service.GenerateEatingPlanAsync(inputModel); Assert.True( result.Meals .Any(x => x.Meal.FoodPreferences .Any(y => y.Preference.Preference == SoftGym.Data.Models.Enums.FoodPreference.Egg)) == false); }
public async Task GenerateEatingPlanGeneratesRightMealTypes() { var service = await this.Before(); var user = new ApplicationUser(); this.usersService.Setup(x => x.GetUserByIdAsync(user.Id)) .Returns(Task.FromResult(user)); var inputModel = new GenerateInputModel { Activity = "light", Age = 25, HasUserActivePlan = false, DurationInDays = 30, FoodPreferences = new List <SoftGym.Data.Models.Enums.FoodPreference>(), Gender = "Male", Goal = Goal.Mantain, Height = 190, Id = user.Id, Weight = 85, }; var result = await service.GenerateEatingPlanAsync(inputModel); Assert.Equal(3, result.Meals.Where(x => x.Meal.Type == MealType.Breakfast).Count()); Assert.Equal(3, result.Meals.Where(x => x.Meal.Type == MealType.Lunch).Count()); Assert.Equal(3, result.Meals.Where(x => x.Meal.Type == MealType.Dinner).Count()); Assert.Equal(6, result.Meals.Where(x => x.Meal.Type == MealType.Snack).Count()); }
public async Task GenerateEatingPlanGeneratesPlanWithRightCalories( int height, int weight, string activity, Goal goal, double minCalories) { var service = await this.Before(); var user = new ApplicationUser(); this.usersService.Setup(x => x.GetUserByIdAsync(user.Id)) .Returns(Task.FromResult(user)); var inputModel = new GenerateInputModel { Activity = activity, Age = 25, HasUserActivePlan = false, DurationInDays = 30, FoodPreferences = new List <SoftGym.Data.Models.Enums.FoodPreference>(), Gender = "Male", Goal = goal, Height = height, Id = user.Id, Weight = weight, }; var result = await service.GenerateEatingPlanAsync(inputModel); Assert.True(result.CaloriesPerDay >= minCalories); }
public async Task GenerateEatingPlanGenerates15MealsPlanEveryTime( string activity, int age, int durationInDays, string gender, Goal goal, double height, double weight) { var service = await this.Before(); var user = new ApplicationUser(); this.usersService.Setup(x => x.GetUserByIdAsync(user.Id)) .Returns(Task.FromResult(user)); var inputModel = new GenerateInputModel { Activity = activity, Age = age, HasUserActivePlan = false, DurationInDays = durationInDays, FoodPreferences = new List <SoftGym.Data.Models.Enums.FoodPreference>(), Gender = gender, Goal = goal, Height = height, Id = user.Id, Weight = weight, }; var result = await service.GenerateEatingPlanAsync(inputModel); Assert.Equal(15, result.Meals.Count); }
public IActionResult GeneratePlan() { var viewModel = new GenerateInputModel() { Id = this.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value, HasUserActivePlan = this.eatingPlansService.HasUserActivePlan(this.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value), }; if (viewModel.HasUserActivePlan) { return(this.Redirect($"/EatingPlans?redirected=true")); } return(this.View(viewModel)); }
public async Task <IActionResult> GeneratePlan(GenerateInputModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } if (this.eatingPlansService.HasUserActivePlan(this.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(this.Redirect("/")); } var plan = await this.eatingPlansService.GenerateEatingPlanAsync(inputModel); return(this.Redirect($"/EatingPlans/MyPlans/{inputModel.Id}")); }
public async Task GetCardViewModelAsyncShouldReturnCorrectModel() { var service = await this.Before(); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase("Test Db").Options; var db = new ApplicationDbContext(options); var eatingPlansRepositoryInMemory = new EfDeletableEntityRepository <EatingPlan>(db); var user = new ApplicationUser(); this.usersService.Setup(x => x.GetUserByIdAsync(user.Id)) .Returns(Task.FromResult(user)); var inputModel = new GenerateInputModel { Activity = "light", Age = 25, HasUserActivePlan = false, DurationInDays = 30, FoodPreferences = new List <SoftGym.Data.Models.Enums.FoodPreference>(), Gender = "Male", Goal = Goal.Mantain, Height = 190, Id = user.Id, Weight = 85, }; var plan = await service.GenerateEatingPlanAsync(inputModel); await eatingPlansRepositoryInMemory.AddAsync(plan); await eatingPlansRepositoryInMemory.SaveChangesAsync(); var result = await service.GetPlanAsync <TestModel>(plan.Id); Assert.Equal(user.EatingPlans.First().Id, result.Id); Assert.Equal(15, result.MealsCount); }
public async Task <EatingPlan> GenerateEatingPlanAsync(GenerateInputModel inputModel) { double caloriesPerDay; if (inputModel.Gender == "Male") { caloriesPerDay = (10 * inputModel.Weight) + (6.25 * inputModel.Height) - (5 * inputModel.Age) + 5; } else { caloriesPerDay = (10 * inputModel.Weight) + (6.25 * inputModel.Height) - (5 * inputModel.Age) - 161; } switch (inputModel.Activity) { case "light": caloriesPerDay = 1.30 * caloriesPerDay; break; case "medium": caloriesPerDay = 1.54 * caloriesPerDay; break; case "high": caloriesPerDay = 1.76 * caloriesPerDay; break; } if (inputModel.Goal == Goal.Gain) { caloriesPerDay = 1.15 * caloriesPerDay; } else if (inputModel.Goal == Goal.Lose) { caloriesPerDay = 0.79 * caloriesPerDay; } var eatingPlan = new EatingPlan() { UserId = inputModel.Id, User = await this.usersService.GetUserByIdAsync(inputModel.Id), ExpireDate = DateTime.UtcNow.AddDays(inputModel.DurationInDays), CaloriesPerDay = caloriesPerDay, }; double caloriesForMeal = 0.25 * caloriesPerDay; await this.AddMeals(eatingPlan, inputModel, MealType.Breakfast, caloriesForMeal); await this.AddMeals(eatingPlan, inputModel, MealType.Lunch, caloriesForMeal); await this.AddMeals(eatingPlan, inputModel, MealType.Dinner, caloriesForMeal); await this.AddMeals(eatingPlan, inputModel, MealType.Snack, caloriesForMeal); await this.mealsRepository.SaveChangesAsync(); await this.notificationsService.CreateNotificationAsync( "You have sucessfully generated a eating plan.", $"/EatingPlans/Details/{eatingPlan.Id}", inputModel.Id); return(eatingPlan); }
public async Task AddMeals( EatingPlan eatingPlan, GenerateInputModel inputModel, MealType mealType, double caloriesForMeal) { var meals = await this.mealsRepository .All() .Select(x => new { x.Name, x.Type, FoodPreferences = x.FoodPreferences.Select(y => new { y.Preference, }), x.Id, x.CaloriesPer100Grams, x.Plans, }) .Where(x => x.Type == mealType) .ToArrayAsync(); if (inputModel.Goal == Goal.Gain) { meals = meals .OrderByDescending(x => x.CaloriesPer100Grams) .ToArray(); } else if (inputModel.Goal == Goal.Lose) { meals = meals .OrderBy(x => x.CaloriesPer100Grams) .ToArray(); } int[] pickedMeals; if (mealType == MealType.Snack) { pickedMeals = new int[6] { -1, -1, -1, -1, -1, -1, }; caloriesForMeal /= 2; } else { pickedMeals = new int[3] { -1, -1, -1, }; } for (int i = 0; i < pickedMeals.Length; i++) { var random = new Random(); int mealIndex = 0; while (pickedMeals.Contains(mealIndex) || inputModel?.FoodPreferences .Any(x => meals[mealIndex].FoodPreferences .Any(y => y.Preference.Preference == x)) != false) { for (int j = 1; j < meals.Length; j++) { if (pickedMeals.Contains(j) == false && inputModel?.FoodPreferences .Any(x => meals[j].FoodPreferences .Any(y => y.Preference.Preference == x)) == false) { mealIndex = j; break; } } } pickedMeals[i] = mealIndex; var currentMeal = await this.mealsRepository .All() .FirstAsync(x => x.Id == meals[mealIndex].Id); var mealPlan = new MealPlan() { EatingPlanId = eatingPlan.Id, EatingPlan = eatingPlan, MealId = currentMeal.Id, Meal = currentMeal, MealWeight = caloriesForMeal / (currentMeal.CaloriesPer100Grams / 100), TotalCalories = (caloriesForMeal / (currentMeal.CaloriesPer100Grams / 100)) * (currentMeal.CaloriesPer100Grams / 100), }; await this.mealsPlansRepository.AddAsync(mealPlan); eatingPlan.Meals.Add(mealPlan); currentMeal.Plans.Add(mealPlan); } }