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);
                    }
                }
            }
        }
示例#2
0
        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);
        }