public void Edit(int id, DietPlan dietPlan, string username) { var oldDailyDietPlanList = ListDailyDietPlans(id); dietPlan.DailyDietPlanList = new List <DailyDietPlan>(); for (var i = 0; i < dietPlan.Duration.Days; i++) { dietPlan.DailyDietPlanList.Add(new DailyDietPlan() { DayNumber = i + 1, Date = dietPlan.StartDate + new TimeSpan(i, 0, 0, 0), DietPlanId = id }); } foreach (var daily in oldDailyDietPlanList.Where(daily => !dietPlan.DailyDietPlanList.Exists(x => x.Date == daily.Date))) { _dietPlanRepository.DeleteDailyDietPlan(daily); } foreach (var daily in dietPlan.DailyDietPlanList.Where(daily => !oldDailyDietPlanList.Exists(x => x.Date == daily.Date))) { _dietPlanRepository.AddDailyDietPlan(daily); } foreach (var oldDaily in oldDailyDietPlanList) { foreach (var newDaily in dietPlan.DailyDietPlanList) { if (oldDaily.Date == newDaily.Date) { oldDaily.DayNumber = newDaily.DayNumber; _dietPlanRepository.UpdateDailyDietPlan(oldDaily); } } } var dayCount = dietPlan.DailyDietPlanList.Count; dietPlan.DailyDietPlanList = null; var client = _httpClientFactory.CreateClient(); var action = CreateAction(ActionType.EditedDietPlan, dietPlan.Id, dayCount, username, dietPlan.CaloriesPerDay); client.PostAsync("https://localhost:5001/VirtusFit/plan/dietplan", new StringContent(JsonSerializer.Serialize(action), Encoding.UTF8, "application/json")); _dietPlanRepository.UpdateDietPlan(dietPlan); }
public void CalculateDailyDietPlanCaloriesAndMacros(DailyDietPlan dailyToCalculate) { dailyToCalculate.CaloriesSum = 0; dailyToCalculate.FatSum = 0; dailyToCalculate.CarbohydratesSum = 0; dailyToCalculate.ProteinSum = 0; var productListForDaily = _dietPlanRepository.ListProductsInDailyDietPlan(dailyToCalculate); foreach (var product in productListForDaily) { dailyToCalculate.CaloriesSum += product.Product.Energy * product.PortionSize * product.NumberOfPortions / 100; dailyToCalculate.FatSum += product.Product.Fat * product.PortionSize * product.NumberOfPortions / 100; dailyToCalculate.CarbohydratesSum += product.Product.Carbohydrates * product.PortionSize * product.NumberOfPortions / 100; dailyToCalculate.ProteinSum += product.Product.Protein * product.PortionSize * product.NumberOfPortions / 100; } dailyToCalculate.FatSum = Math.Round(dailyToCalculate.FatSum, 2); dailyToCalculate.CarbohydratesSum = Math.Round(dailyToCalculate.CarbohydratesSum, 2); dailyToCalculate.ProteinSum = Math.Round(dailyToCalculate.ProteinSum, 2); _dietPlanRepository.UpdateDailyDietPlan(dailyToCalculate); }