private void UpdateProductInExistingPlan(Product productToBeUpdated, string userId) { var plans = _dietPlanRepository.ListAllDietPlans(userId); var dailyList = new List <DailyDietPlan>(); foreach (var plan in plans) { var dailyListInPlan = _dietPlanRepository.ListDailyDietPlans(plan.Id); foreach (var daily in dailyListInPlan) { dailyList.Add(daily); } } foreach (var daily in dailyList) { var listOfProductsInDailyPlans = _dietPlanRepository.ListDbProductsInDailyDietPlan(daily); foreach (var item in listOfProductsInDailyPlans) { if (item.ProductId == productToBeUpdated.ProductId) { var oldProduct = _dietPlanRepository.GetProductFromDailyDietPlan(daily, item.OrdinalNumber); oldProduct.TotalCalories = productToBeUpdated.Energy * oldProduct.PortionSize * oldProduct.NumberOfPortions / 100; _dietPlanRepository.UpdateProductInPlan(oldProduct); _productInPlanService.CalculateDailyDietPlanCaloriesAndMacros(daily); } } } }
public void EditProductInDailyDietPlan(int id, int dayNumber, ProductInDietPlan editedProduct, int currentProductOrdinalNumber, string user, string username) { var dailyDietPlan = _dietPlanRepository.GetDailyDietPlan(id, dayNumber); var oldProduct = _dietPlanRepository.GetProductFromDailyDietPlan(dailyDietPlan, currentProductOrdinalNumber); oldProduct.PortionSize = editedProduct.PortionSize; oldProduct.NumberOfPortions = editedProduct.NumberOfPortions; oldProduct.TotalCalories = editedProduct.Product.Energy * editedProduct.PortionSize * editedProduct.NumberOfPortions / 100; _dietPlanRepository.UpdateProductInPlan(oldProduct); var client = _httpClientFactory.CreateClient(); var action = CreateAction(ActionType.EditedProductInExistingDailyPlan, id, dailyDietPlan.DietPlanId, editedProduct, username); client.PostAsync("https://localhost:5001/VirtusFit/plan/productinplan", new StringContent(JsonSerializer.Serialize(action), Encoding.UTF8, "application/json")); CalculateDailyDietPlanCaloriesAndMacros(dailyDietPlan); }