public void Calculate_Tax_for_Country() { using (var db = new Entities()) { var sut = _createSut(); using (new TransactionScope()) { // Set Tax. setTaxForCountry(db, "at", 20); var cartService = new CartService(); var cart = cartService.CreateCart(); var product = db.Products.First(p => p.ChargeTaxes); var product2 = db.Products.First(p => !p.ChargeTaxes); product.Price = 10; product2.Price = 10; db.SaveChanges(); cartService.AddProduct(cart.Id, product.Id, 2); cartService.AddProduct(cart.Id, product2.Id, 2); var result = sut.TaxForCountry(cart.Id, "at"); Assert.AreEqual(3.3f, (float)result, 0.04f); } } }
public IHttpActionResult Post(AddProductRequest request) { SetCartToken(); if (string.IsNullOrWhiteSpace(CartId) || string.IsNullOrWhiteSpace(request.ProductId) || request.ProductQty == null || request.ProductQty == 0) { return(BadRequest()); } var cart = CartService.GetCart(CartId); if (cart.Id != CartId) { return(NotFound()); } CartService.AddProduct(CartId, request.ProductId, request.ProductQty.Value); cart = CartService.GetCart(CartId); var url = string.Format("http://{0}/product", HttpContext.Current.Request.Url.Authority); var cartResponse = ToProductResponse(cart.Products.First(x => x.Id == request.ProductId)); return(Created(url, cartResponse)); }
public void GetWholeOrderTotalPrice_WithScenario_C_ValidOrderValue() { // [Scenario C] => // 3 * A 130 // 5 * B 45 + 45 + 1 * 30 // 1 * C - // 1 * D 15 // Total Order Value is: 265 // Arrange double expected = 265; ICartService _cartService = new CartService(); Product prod; // Adding 3 A Type Product. for (int i = 0; i < 3; i++) { prod = new Product { Id = "A" }; _cartService.AddProduct(prod); } // Adding 5 B Type Product. for (int i = 0; i < 5; i++) { prod = new Product { Id = "B" }; _cartService.AddProduct(prod); } // Adding 1 D Type Product. prod = new Product { Id = "D" }; _cartService.AddProduct(prod); // Act decimal actual = _cartService.GetWholeOrderTotalPrice(); // Assert Assert.AreEqual(expected, Convert.ToDouble(actual), 0.001, "Total Order Value is valid."); }
public void GetWholeOrderTotalPrice_WithScenario_B_ValidOrderValue() { // [ Scenario B ] => // 5 * A 130 + 2 * 50 // 5 * B 45 + 45 + 30 // 1 * C 20 // [ Total Order Value is: 370 ] // Arrange double expected = 370; ICartService _cartService = new CartService(); Product prod; // Adding 5 A Type Product. for (int i = 0; i < 5; i++) { prod = new Product { Id = "A" }; _cartService.AddProduct(prod); } // Adding 5 B Type Product. for (int i = 0; i < 5; i++) { prod = new Product { Id = "B" }; _cartService.AddProduct(prod); } // Adding 1 C Type Product. prod = new Product { Id = "C" }; _cartService.AddProduct(prod); // Act decimal actual = _cartService.GetWholeOrderTotalPrice(); // Assert Assert.AreEqual(expected, Convert.ToDouble(actual), 0.001, "Total Order Value is valid."); }
public void Calculate_Shipping_Costs() { using (new TransactionScope()) { using (var db = new Entities()) { var sut = _createSut(); var cartService = new CartService(); var cart = cartService.CreateCart(); var product = db.Products.First(p => p.Id == 1); cartService.AddProduct(cart.Id, product.Id, 17); // Set shipping costs. setShippingCosts(db, "at", product.ShippingCategoryId, 6.75m, 1.5m); setShippingCosts(db, "de", product.ShippingCategoryId, 8m, 2m); // Act. var resultAt = sut.CalculateShippingCosts(cart.Id, "at"); var resultDe = sut.CalculateShippingCosts(cart.Id, "de"); // Assert Assert.AreEqual(6.75m + 16 * 1.5m, resultAt); Assert.AreEqual(8m + 16 * 2.0m, resultDe); // ------------------------------------------------------ // Add another product with a different shipping category. product = db.Products.First(p => p.ShippingCategoryId != product.ShippingCategoryId); cartService.AddProduct(cart.Id, product.Id, 2); setShippingCosts(db, "at", product.ShippingCategoryId, 0m, 1.1m); setShippingCosts(db, "de", product.ShippingCategoryId, 0m, 2.2m); // Act. resultAt = sut.CalculateShippingCosts(cart.Id, "at"); resultDe = sut.CalculateShippingCosts(cart.Id, "de"); // Assert Assert.AreEqual((6.75m + 16 * 1.5m) + (1.1m * 2), resultAt); Assert.AreEqual((8m + 16 * 2.0m) + (2.2m * 2), resultDe); } } }
private bool AddToCart(string productID, string num, string productColorId, Client client) { int _productId, _num, _productColorId; if (int.TryParse(productID, out _productId) && int.TryParse(num, out _num) && int.TryParse(productColorId, out _productColorId)) { cartService.AddProduct(_productId, _num, cartService.GetCookie(client), _productColorId); return(true); } return(false); }
public IActionResult AddProduct(int cartId, [FromBody] dynamic data) { if (cartService.AddProduct(cartId, (int)data.productId)) { var cart = cartService.Get(cartId); var addedProduct = cart.Products.Find(p => p.Id == (int)data.productId); return(Ok(addedProduct)); } return(NotFound()); }
public void GetWholeOrderTotalPrice_WithScenario_A_ValidOrderValue() { // [ Scenario A ] => // 1 * A 50 // 1 * B 30 // 1 * C 20 // [ Total Order Value is: 100 ] // Arrange double expected = 100; ICartService _cartService = new CartService(); Product prod; // Adding 1 A Type Product. prod = new Product { Id = "A" }; _cartService.AddProduct(prod); // Adding 1 B Type Product. prod = new Product { Id = "B" }; _cartService.AddProduct(prod); // Adding 1 C Type Product. prod = new Product { Id = "C" }; _cartService.AddProduct(prod); // Act decimal actual = _cartService.GetWholeOrderTotalPrice(); // Assert Assert.AreEqual(expected, Convert.ToDouble(actual), 0.001, "Total Order Value is valid."); }
public async Task <IActionResult> AddToCart(MvcCartItem cartItem) { try { _ = await _cartService.AddProduct(_cartService.GetUserCart(cartItem.UserId).Result.Id, cartItem.ProductId, cartItem.Quantity); return(RedirectToAction("Index")); } catch (Exception exc) { // ignored Debug.Write(exc); return(View("Error")); } }
private void HandleAddRemove(object sender, AddRemoveClickEventArgs e) { var vm = adapter.GetItem(e.Position); if (e.IsAdd) { cartService.AddProduct(vm.Product, vm.Sale); } else { cartService.RemoveProduct(vm.Product); } vm.Units = cartService.GetUnits(vm.Product); vm.DiscountPercent = cartService.GetDiscountPercent(vm.Product); adapter.NotifyItemChanged(e.Position); UpdateTotalPrice(); }
static void Main(string[] args) { Console.WriteLine("Total number of order(s)"); int totalOreder = Convert.ToInt32(Console.ReadLine()); // Providing the valid SKU IDs to end user. Console.WriteLine("Please provide product type from the below given list"); foreach (var item in SKUID.SKUIDs) { Console.WriteLine(item); } Console.WriteLine("========================================="); // [Interface Repository pattern] Which can be provided as dependent service to client class using DI. ICartService _cart = new CartService(); for (int i = 0; i < totalOreder; i++) { Console.WriteLine("Enter the product type"); string productType = Console.ReadLine(); // SKU ID validtaion for the Product which is going to be added into the order list. if (!SKUID.CheckSkuId(productType)) { Console.WriteLine("Please provide valid product type."); return; } // Adding the Products having SKUIDs -> A, B, C, D. Product prod = new Product { Id = productType }; _cart.AddProduct(prod); } // Getting the Total Order price. decimal totalPrice = _cart.GetWholeOrderTotalPrice(); Console.WriteLine("========================================="); Console.WriteLine("Your Total Order Value: {0}", totalPrice); Console.ReadLine(); }