public EatingPlanController() { ep = new EatingPlan(); ic = this; ic.InstantiateMasterController(); ic.Start(); }
public async Task AddEatingPlantToClient(EatingPlanInputModel input) { var client = await this.db.Users.FirstOrDefaultAsync(u => u.Id == input.ClientId); if (client == null) { throw new ArgumentException("Невалиден клиент."); } var eatingPlan = new EatingPlan() { User = client, UserId = client.Id, }; var mealPlans = new List <MealPlan>(); foreach (var plan in input.MealPlans.Where(d => d.Meals != null).ToList()) { var mealPlan = new MealPlan() { Name = plan.Name, Time = plan.Time, EatingPlan = eatingPlan, EatingPlanId = eatingPlan.EatingPlanId, }; foreach (var inputMeal in plan.Meals) { var meal = new Meal() { Name = inputMeal.Name, GramsOrMil = inputMeal.GramsOrMil, MealPlan = mealPlan, MealPlanId = mealPlan.MealPlanId, }; mealPlan.Meals.Add(meal); } mealPlans.Add(mealPlan); } eatingPlan.MealPlans = mealPlans; await this.db.EatingPlan.AddAsync(eatingPlan); await this.db.SaveChangesAsync(); }
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); } }