public CartsTest() { _cartModel = new CartsModel(); _cartsService = new CartsService(null, null); _productsInCart = new List <ProductInCartModel>(); }
public CartsModel calculate(CartsModel cart, List <ProductInCartModel> productsInCart, CouponModel coupon = null) { if (productsInCart.Count() == 0) { return(cart); } decimal subtotal = 0; decimal total = 0; decimal shippingFee = 50; string shippingMethod = "Cash on Delivery"; foreach (var productInCart in productsInCart) { subtotal += productInCart.price * productInCart.quantity; } if (coupon != null) { cart.discountCode = coupon.code; cart.discount = (subtotal <= coupon.discount)? subtotal : coupon.discount; subtotal = (subtotal <= coupon.discount)? 0 : coupon.discount - subtotal; } total = subtotal + shippingFee; cart.subtotal = subtotal; cart.total = total; cart.shippingFee = shippingFee; cart.shippingMethod = shippingMethod; return(cart); }
public void When_Calculate_Cart_With_Two_Products_Price_12_95_and_110_95_Should_Be_Total_equal_173_90() { var _cartOptions = new DbContextOptionsBuilder <CartsContext>().UseInMemoryDatabase("carts").Options; var _cartContext = new CartsContext(_cartOptions); var _productOptions = new DbContextOptionsBuilder <ProductsContext>().UseInMemoryDatabase("products").Options; var _productContext = new ProductsContext(_productOptions); CartsModel cartModel = new CartsModel { userId = 1, createDatetime = DateTime.Now, updateDatetime = DateTime.Now }; List <ProductInCartModel> productsInCart = new List <ProductInCartModel>(); productsInCart.Add(new ProductInCartModel { price = 12.95M, quantity = 1 }); productsInCart.Add(new ProductInCartModel { price = 110.95M, quantity = 1 }); CartsService cartsService = new CartsService(_cartContext, _productContext); var actualResult = cartsService.calculate(cartModel, productsInCart); Assert.Equal(123.90M, actualResult.subtotal); Assert.Equal(148.90M, actualResult.total); }
public IActionResult Register(CustomerModels newCustomer) {//works only if no customer validation lol try { CustomerModels customer = new CustomerModels() { Username = newCustomer.Username, email = newCustomer.email }; /* List<CustomerModels> getCustomersTask = _customerService.GetAllCustomers(); * foreach (var h in getCustomersTask) * { * if (newCustomer.Username.Equals(h.Username)) * { * throw new Exception("Sorry this username is already taken"); * } * else * { * if (newCustomer.email.Equals(h.email)) * { * throw new Exception("Sorry this email is already registered"); * } * } * } */ _customerService.AddCustomer(newCustomer); CartsModel cart = new CartsModel(); cart.CustomerID = newCustomer.ID; return(Ok()); } catch (Exception) { return(BadRequest()); } }
public void When_Calculate_Cart_With_One_Product_Price_119_95_Should_Be_Total_equal_169_95() { var _cartOptions = new DbContextOptionsBuilder <CartsContext>().UseInMemoryDatabase("calculate_carts").Options; var _cartContext = new CartsContext(_cartOptions); var _productOptions = new DbContextOptionsBuilder <ProductsContext>().UseInMemoryDatabase("calculate_products").Options; var _productContext = new ProductsContext(_productOptions); CartsModel cartModel = new CartsModel { userId = 1, createDatetime = DateTime.Now, updateDatetime = DateTime.Now }; List <ProductInCartModel> productsInCart = new List <ProductInCartModel>(); productsInCart.Add(new ProductInCartModel { price = (Decimal)119.95, quantity = 1 }); CartsService cartsService = new CartsService(_cartContext, _productContext); var actualResult = cartsService.calculate(cartModel, productsInCart); Assert.Equal((Decimal)119.95, actualResult.subtotal); Assert.Equal((Decimal)144.95, actualResult.total); }
private CartsModel createCartInfo(ProductsModel adding_product, int adding_quantity) { CartsModel expectedCart = new CartsModel { userId = DEFAULT_USER_ID, total = 62.95M, subtotal = 12.95M, shippingFee = 50, shippingMethod = "Cash on Delivery", createDatetime = DateTime.Now, updateDatetime = DateTime.Now }; List <ProductInCartModel> productsInCart = new List <ProductInCartModel>(); productsInCart.Add(new ProductInCartModel { id = adding_product.id, name = adding_product.name, price = adding_product.price, availability = "InStock", stockAvailability = 10, age = adding_product.age, gender = adding_product.gender, brand = adding_product.brand, quantity = adding_quantity }); expectedCart.CartProducts = productsInCart; return(expectedCart); }
private static CartsContext generateCartContext(string databaseName) { var _cartOptions = new DbContextOptionsBuilder <CartsContext>().UseInMemoryDatabase(databaseName).Options; var _cartContext = new CartsContext(_cartOptions); CartsModel cartModel = new CartsModel { id = 1, userId = 1, shippingMethod = "Cash on Delivery", subtotal = (Decimal)12.95, shippingFee = (Decimal)50.00, total = (Decimal)62.95 }; _cartContext.Carts.Add(cartModel); CartProductsModel cartProduct = new CartProductsModel { id = 1, productId = 2, cartId = 1, quantity = 1, }; _cartContext.CartProducts.Add(cartProduct); _cartContext.SaveChanges(); return(_cartContext); }
public Carts ParseCarts(CartsModel carts) { return(new Carts() { Customer = carts.CustomerID, Location = carts.LocationID }); }
public void applyCoupon(CartsModel cartModel, CouponModel coupon) { List <ProductInCartModel> productsInCart = this.getProductInCart(cartModel.id); cartModel = this.calculate(cartModel, productsInCart, coupon); _context.Carts.Update(cartModel); _context.SaveChanges(); }
public CartsModel getCart(int cartId, int userId) { CartsModel cart = _context.Carts.Where(c => c.id == cartId).Where(c => c.userId == userId).FirstOrDefault(); List <CartProductsModel> cartProducts = _context.CartProducts.Where(c => c.cartId == cartId).ToList(); List <ProductInCartModel> productsInCart = this.getProductInCart(cart.id); cart.CartProducts = productsInCart; return(cart); }
public List <CartsModel> EntityToModel(List <Order> entity) { List <CartsModel> res = new List <CartsModel>(); foreach (var item in entity) { CartsModel c = new CartsModel(); c.Id = item.Id; } return(res); }
private CartsModel createCart(int user_id) { CartsModel cartModel = new CartsModel { userId = user_id, createDatetime = DateTime.Now, updateDatetime = DateTime.Now }; _context.Carts.Add(cartModel); _context.SaveChanges(); return(cartModel); }
private CartProductsModel addProductToCart(CartsModel cartModel, ProductsModel product, int quantity) { CartProductsModel cartProductModel = new CartProductsModel { cartId = cartModel.id, productId = product.id, quantity = quantity }; _context.CartProducts.Add(cartProductModel); _context.SaveChanges(); return(cartProductModel); }
public AddCartOutputModel add(ProductsModel product, int quantity) { CartsModel cartModel = this.createCart(1); CartProductsModel cartProductsModel = this.addProductToCart(cartModel, product, quantity); List <ProductInCartModel> productsInCart = this.getProductInCart(cartModel.id); cartModel = this.calculate(cartModel, productsInCart); _context.Carts.Update(cartModel); _context.SaveChanges(); return(new AddCartOutputModel { id = cartModel.id }); }
public CreateOrderOutputModel create(int cartId, int userId = 1) { CartsService cartsService = new CartsService(_cartContext, _productContext); CartsModel cart = cartsService.getCart(cartId, userId); OrdersModel order = new OrdersModel { cartId = cart.id, userId = userId, subtotal = cart.subtotal, total = cart.total, shippingMethod = cart.shippingMethod, shippingFee = cart.shippingFee, fullname = "Chonnikan Tobunrueang", address1 = "3 อาคารพร้อมพันธ์ 3 ห้อง 1001", address2 = "จอมพล", city = "จตุจักร", province = "กรุงเทพ", postcode = "10900", createDatetime = DateTime.Now, }; _orderContext.Orders.Add(order); _orderContext.SaveChanges(); List <CartProductsModel> cartProducts = _cartContext.CartProducts.Where(c => c.cartId == cartId).ToList(); List <OrderProductsModel> orderProducts = new List <OrderProductsModel>(); foreach (CartProductsModel cartProduct in cartProducts) { ProductsModel product = _productContext.Products.Where(p => p.id == cartProduct.productId).FirstOrDefault(); _orderContext.OrderProducts.Add(new OrderProductsModel { orderId = order.id, productId = product.id, quantity = cartProduct.quantity, name = product.name, price = product.price, availability = product.availability, stockAvailability = product.stockAvailability, age = product.age, gender = product.gender, brand = product.brand }); } _orderContext.SaveChanges(); return(new CreateOrderOutputModel { id = order.id }); }
public IActionResult Register(CustomerModels customer) { try { CustomerModels newCustomer = customerService1.AddCustomer(customer); CustomerModels customers = customerService1.GetCustomerByName(newCustomer.Username); CartsModel cart = new CartsModel(); cart.CustomerID = customers.ID; return(Ok()); } catch (Exception) { return(BadRequest()); } }
public void When_Get_Cart_By_Id_1_And_Should_Be_Get_This_Cart_Detail() { var _cartOptions = new DbContextOptionsBuilder <CartsContext>().UseInMemoryDatabase("get_carts_carts_detail").Options; var _cartContext = new CartsContext(_cartOptions); var _productOptions = new DbContextOptionsBuilder <ProductsContext>().UseInMemoryDatabase("get_carts_carts_products_detail").Options; var _productContext = new ProductsContext(_productOptions); CartsModel cartModel = new CartsModel { id = 1, userId = 1, subtotal = (Decimal)12.95, shippingFee = (Decimal)50.00, total = (Decimal)62.95 }; _cartContext.Carts.Add(cartModel); CartProductsModel cartProduct = new CartProductsModel { id = 1, productId = 2, cartId = 1, quantity = 1, }; _cartContext.CartProducts.Add(cartProduct); _cartContext.SaveChanges(); ProductsModel productData = new ProductsModel { id = 2, name = "43 Piece dinner Set", price = (Decimal)12.95, availability = "InStock", stockAvailability = 10, age = "3_to_5", gender = "FEMALE", brand = "CoolKidz" }; _productContext.Products.Add(productData); _productContext.SaveChanges(); CartsService cartsService = new CartsService(_cartContext, _productContext); var actualResult = cartsService.getCart(1, 1); Assert.Equal(cartModel, actualResult); }
public void When_Create_New_Cart_With_43_Piece_Dinner_Set_With_1_Qty_Should_Return_Cart_Detail_With_43_Piece_Dinner_Set() { ProductsModel adding_product = _fixture._2_43_Piece_dinner_Set; int adding_quantity = 1; CartsContext _cartContext = createCartsContext("create_new_cart_carts2"); CartsModel expectedCart = createCartInfo(adding_product, adding_quantity); CartsService cartService = new CartsService(_cartContext, _productContext); AddCartOutputModel cartOutput = cartService.add(adding_product, adding_quantity); CartsModel actualCart = cartService.getCart(cartOutput.id, DEFAULT_USER_ID); Assert.Equal(expectedCart.CartProducts, actualCart.CartProducts); Assert.Equal(expectedCart.userId, actualCart.userId); Assert.Equal(expectedCart.total, actualCart.total); Assert.Equal(expectedCart.subtotal, actualCart.subtotal); Assert.Equal(expectedCart.shippingFee, actualCart.shippingFee); Assert.Equal(expectedCart.shippingMethod, actualCart.shippingMethod); }
public void When_Calculate_Cart_With_Zero_Product_Should_Be_Total_equal_0() { var _cartOptions = new DbContextOptionsBuilder <CartsContext>().UseInMemoryDatabase("carts").Options; var _cartContext = new CartsContext(_cartOptions); var _productOptions = new DbContextOptionsBuilder <ProductsContext>().UseInMemoryDatabase("products").Options; var _productContext = new ProductsContext(_productOptions); CartsModel cartModel = new CartsModel { userId = 1, createDatetime = DateTime.Now, updateDatetime = DateTime.Now }; List <ProductInCartModel> productsInCart = new List <ProductInCartModel>(); CartsService cartsService = new CartsService(_cartContext, _productContext); var actualResult = cartsService.calculate(cartModel, productsInCart); Assert.Equal(0.00M, actualResult.subtotal); Assert.Equal(0.00M, actualResult.total); }
public CartsModel calculate(CartsModel cart, List <ProductInCartModel> productsInCart) { if (productsInCart.Count() == 0) { return(cart); } decimal subtotal = 0; decimal total = 0; decimal shippingFee = 25; string shippingMethod = "Cash on Delivery"; foreach (var productInCart in productsInCart) { subtotal += productInCart.price * productInCart.quantity; } total = subtotal + shippingFee; cart.subtotal = subtotal; cart.total = total; cart.shippingFee = shippingFee; cart.shippingMethod = shippingMethod; return(cart); }
public ProductsModel GetInventoryByCartOrderID() { List <ProductsModel> products = new List <ProductsModel>(); ProductsModel product = new ProductsModel(); List <CartsModel> carts = new List <CartsModel>(); CartsModel cart = new CartsModel(); // Console.WriteLine(" +----------------------------------------------------------------+"); Console.WriteLine(" Enter your Order ID Number "); // Console.WriteLine(" +----------------------------------------------------------------+"); // long productID = Convert.ToInt64(Console.ReadLine()); Console.WriteLine(" +----------------------------------------------------------------+"); Console.WriteLine(" Enter your Cart Order ID Number "); Console.WriteLine(" +----------------------------------------------------------------+"); int cartOrderID = Convert.ToInt32(Console.ReadLine()); //string sqlQuerys = "Select * from locations where locationID=@ID"; //"SELECT * FROM Products INNER JOIN Locations ON Locations.locationID = Products.productLocationID" + "WHERE locationID=@locationID" + "AND productID=@productID"; string sqlQuery = "SELECT * FROM Carts INNER JOIN Products ON Carts.cartProductID = Products.productID WHERE cartOrderID=@cartOrderID"; //Using SqlConnection to establish connection to database //using SqlCommand passing in the SqlConnection and the sqlQuery using (SqlConnection con = new SqlConnection(CONNECTION)) using (SqlCommand cmd = new SqlCommand(sqlQuery, con)) { con.Open(); cmd.Parameters.Add("@cartOrderID", SqlDbType.Int).Value = cartOrderID; //cmd.Parameters.Add("@productID", SqlDbType.Int).Value = productID; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { ProductsModel temp = new ProductsModel(); { //cartOrderID = Convert.ToInt32(reader["cartOrderID"]); //cart.cartProductID = Convert.ToInt32(reader["cartProductID"]); // cart.cartQuantity = Convert.ToInt32(reader["cartQuantity"]); product.productID = Convert.ToInt32(reader["productID"]); product.productName = reader["productName"].ToString(); product.productPrice = Convert.ToDouble(reader["productPrice"]); product.productQuantity = Convert.ToInt32(reader["productQuantity"]); product.productLocationID = Convert.ToInt32(reader["productLocationID"]); } products.Add(temp); Console.WriteLine(); // Console.WriteLine(" +================================================================+"); Console.WriteLine(" +----------------------------------------------------------------+"); Console.WriteLine(); Console.WriteLine(" PRODUCT NAME " + " | " + product.productName); Console.WriteLine(" PRODUCT QUANTITY " + " | " + product.productQuantity); Console.WriteLine(" PRODUCT PRICE " + " | " + product.productPrice); Console.WriteLine(); Console.WriteLine(" PRODUCT ID " + " | " + product.productID); Console.WriteLine(" PRODUCT LOCATION ID" + " | " + product.productLocationID); Console.WriteLine(" +----------------------------------------------------------------+"); Console.WriteLine(); } } return(product); } }
public void AddCart(CartsModel cart) { dBRepo.AddCart(cart); }
public void DeleteCart(CartsModel cart) { dBRepo.DeleteCart(cart); }
public void When_Create_Order_With_Cart_Should_Be_Return_Order() { var _orderOptions = new DbContextOptionsBuilder <OrdersContext>().UseInMemoryDatabase("create_order_orders_1").Options; var _orderContext = new OrdersContext(_orderOptions); var _cartOptions = new DbContextOptionsBuilder <CartsContext>().UseInMemoryDatabase("create_order_carts_detail_1").Options; var _cartContext = new CartsContext(_cartOptions); var _productOptions = new DbContextOptionsBuilder <ProductsContext>().UseInMemoryDatabase("create_order_carts_products_detail_1").Options; var _productContext = new ProductsContext(_productOptions); ProductsModel productData = new ProductsModel { id = 2, name = "43 Piece dinner Set", price = (Decimal)12.95, availability = "InStock", stockAvailability = 10, age = "3_to_5", gender = "FEMALE", brand = "CoolKidz" }; _productContext.Products.Add(productData); _productContext.SaveChanges(); CartsModel cartModel = new CartsModel { id = 1, userId = 1, subtotal = (Decimal)12.95, shippingFee = (Decimal)50.00, total = (Decimal)62.95 }; _cartContext.Carts.Add(cartModel); CartProductsModel cartProduct = new CartProductsModel { id = 1, productId = 2, cartId = 1, quantity = 1, }; _cartContext.CartProducts.Add(cartProduct); _cartContext.SaveChanges(); OrdersService ordersService = new OrdersService(_cartContext, _productContext, _orderContext); var actualResult = ordersService.create(1, 1); // CreateOrderOutputModel actualResult = new CreateOrderOutputModel{ // id = 1 // }; Assert.Equal(2, actualResult.id); using (var context = new ProductsContext(_productOptions)) { _productContext.Database.EnsureDeleted(); } using (var context = new CartsContext(_cartOptions)) { _cartContext.Database.EnsureDeleted(); } using (var context = new OrdersContext(_orderOptions)) { _orderContext.Database.EnsureDeleted(); } }
public void AddCart(CartsModel cartsModel) { context.Carts.Add(mapper.ParseCarts(cartsModel)); context.SaveChanges(); }
public void DeleteCart(CartsModel carts) { context.Carts.Remove(mapper.ParseCarts(carts)); context.SaveChanges(); }