Esempio n. 1
0
        public void DeleteDailyDietPlan(DailyDietPlan dailyDietPlan)
        {
            var productsToDelete = _context.ProductsInDietPlans.Where(x => x.DailyDietPlanId == dailyDietPlan.Id).ToList();

            foreach (var product in productsToDelete)
            {
                DeleteProductInPlan(product);
            }
            _context.DailyDietPlans.Remove(dailyDietPlan).Context.SaveChanges();
        }
        public void DeleteFromExistingPlan()
        {
            var testProduct = new Product()
            {
                ProductName = "Product1", Energy = 50, ProductId = 1
            };
            var dietPlan = new DietPlan()
            {
                Id = 1
            };
            var productInDietPlan = new ProductInDietPlan()
            {
                Product = testProduct, Id = testProduct.ProductId
            };
            var productListForDay = new List <ProductInDietPlan>();

            productListForDay.Add(productInDietPlan);
            var dailyDietPlan = new DailyDietPlan()
            {
                ProductListForDay = productListForDay
            };
            var dailyDietPlanList = new List <DailyDietPlan>()
            {
                dailyDietPlan
            };

            dietPlan.DailyDietPlanList = dailyDietPlanList;
            var listOfDietPlans = new List <DietPlan>()
            {
                dietPlan
            };
            var productFromDB = new ProductInDietPlanDb()
            {
                DailyDietPlanId = dietPlan.Id, ProductId = testProduct.ProductId
            };
            var productRepositoryMock = new Mock <IProductRepository>();

            productRepositoryMock.Setup(repository => repository.GetProductById(1)).Returns(testProduct);
            var productInPlanServiceMock = new Mock <IProductInPlanService>();
            var dietPlanRepositoryMock   = new Mock <IDietPlanRepository>();

            dietPlanRepositoryMock.Setup(repository => repository.ListAllDietPlans("DummyId"))
            .Returns(listOfDietPlans);
            dietPlanRepositoryMock.Setup(repository => repository.ListDailyDietPlans(1)).Returns(dailyDietPlanList);
            dietPlanRepositoryMock.Setup(repository => repository.ListDbProductsInDailyDietPlan(dailyDietPlan)).Returns(new List <ProductInDietPlanDb>()
            {
                productFromDB
            });

            var sut = new ProductService(productRepositoryMock.Object, dietPlanRepositoryMock.Object, productInPlanServiceMock.Object);

            sut.DeleteById(testProduct.ProductId, "DummyId");

            dietPlanRepositoryMock.Verify(repository => repository.DeleteProductInPlan(productFromDB));
        }
Esempio n. 3
0
        public List <ProductInDietPlan> ListProductsInDailyDietPlan(DailyDietPlan dailyDietPlan)
        {
            var productListFromDb = _context.ProductsInDietPlans.Where(x => x.DailyDietPlanId == dailyDietPlan.Id).ToList();

            var list = productListFromDb.Select(product => new ProductInDietPlan()
            {
                Id               = product.Id,
                Product          = _context.Products.Find(product.ProductId),
                NumberOfPortions = product.NumberOfPortions,
                OrdinalNumber    = product.OrdinalNumber,
                PortionSize      = product.PortionSize,
                TotalCalories    = product.TotalCalories
            })
                       .ToList();

            return(list);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 public void UpdateDailyDietPlan(DailyDietPlan dailyDietPlan)
 {
     _context.DailyDietPlans.Update(dailyDietPlan).Context.SaveChanges();
 }
Esempio n. 6
0
 public void AddDailyDietPlan(DailyDietPlan dailyDietPlan)
 {
     _context.DailyDietPlans.Add(dailyDietPlan).Context.SaveChanges();
 }
Esempio n. 7
0
 public List <ProductInDietPlanDb> ListDbProductsInDailyDietPlan(DailyDietPlan dailyDietPlan)
 {
     return(_context.ProductsInDietPlans.Where(x => x.DailyDietPlanId == dailyDietPlan.Id).ToList());
 }
Esempio n. 8
0
 public ProductInDietPlanDb GetProductFromDailyDietPlan(DailyDietPlan dailyDietPlan, int ordinalNumber)
 {
     return(_context.ProductsInDietPlans.FirstOrDefault(x =>
                                                        x.DailyDietPlanId == dailyDietPlan.Id && x.OrdinalNumber == ordinalNumber));
 }