public void DeleteProductFromPlan(int id, int dayNumber, int ordinalNumber, string user, string username) { var currentDailyDietPlan = _dietPlanRepository.GetDailyDietPlan(id, dayNumber); var productToDelete = _dietPlanRepository.GetProductFromDailyDietPlan(currentDailyDietPlan, ordinalNumber); var productToAddToApi = GetProductFromDietPlan(id, dayNumber, ordinalNumber); _dietPlanRepository.DeleteProductInPlan(productToDelete); var newProductList = _dietPlanRepository.ListDbProductsInDailyDietPlan(currentDailyDietPlan); var newOrdinalNumber = 1; foreach (var product in newProductList) { product.OrdinalNumber = newOrdinalNumber; _dietPlanRepository.UpdateProductInPlan(product); newOrdinalNumber++; } var client = _httpClientFactory.CreateClient(); var action = CreateAction(ActionType.RemovedProductFromExistingDailyPlan, id, currentDailyDietPlan.DietPlanId, productToAddToApi, username); client.PostAsync("https://localhost:5001/VirtusFit/plan/productinplan", new StringContent(JsonSerializer.Serialize(action), Encoding.UTF8, "application/json")); CalculateDailyDietPlanCaloriesAndMacros(currentDailyDietPlan); }
private void DeleteFromExistingPlan(Product productToBeDeleted, 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 == productToBeDeleted.ProductId) { _dietPlanRepository.DeleteProductInPlan(item); _productInPlanService.CalculateDailyDietPlanCaloriesAndMacros(daily); } } } }