public void AddProductToDailyDietPlan(int id, int dayNumber, ProductInDietPlan productToAdd, Product product, string user, string username) { var dailyToAddTo = _dietPlanRepository.GetDailyDietPlan(id, dayNumber); var productToDb = new ProductInDietPlanDb { OrdinalNumber = _dietPlanRepository.ListProductsInDailyDietPlan(dailyToAddTo).Count + 1, ProductId = product.ProductId, PortionSize = productToAdd.PortionSize, NumberOfPortions = productToAdd.NumberOfPortions, TotalCalories = product.Energy * productToAdd.PortionSize * productToAdd.NumberOfPortions / 100, DailyDietPlanId = dailyToAddTo.Id }; _dietPlanRepository.AddProductInPlan(productToDb); var client = _httpClientFactory.CreateClient(); var action = CreateAction(ActionType.AddedProductToExistingDailyPlan, dailyToAddTo.DietPlanId, dailyToAddTo.Id, productToAdd, username); client.PostAsync("https://localhost:5001/VirtusFit/plan/productinplan", new StringContent(JsonSerializer.Serialize(action), Encoding.UTF8, "application/json")); CalculateDailyDietPlanCaloriesAndMacros(dailyToAddTo); }
public ProductInDietPlan GetProductToAdd(int id) { var product = _productRepository.GetProductById(id); var productToAdd = new ProductInDietPlan { Product = product }; return(productToAdd); }
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)); }
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); }
private CreateProductInPlanAction CreateAction(ActionType type, int planId, int dailyPlanId, ProductInDietPlan product, string username) { var action = new CreateProductInPlanAction { Username = username, ProductId = product.Product.ProductNo, ProductName = product.Product.ProductName, DailyDietPlanId = dailyPlanId, DietPlanId = planId, Action = type, Created = DateTime.UtcNow }; return(action); }