public int ModaltoCart(string productid, string size, string color, string quantity) { var cookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) { ViewBag.IsAuthenticated = false; return(1); } var ticket = FormsAuthentication.Decrypt(cookie.Value); ViewBag.IsAuthenticated = true; ViewData["UserName"] = ticket.UserData; Procedure.Procedure procedure = new Procedure.Procedure(); var ProductFormat = procedure.GetFormatIDByProductIDCS(int.Parse(productid), size, color); foreach (var item in ProductFormat) { productformatid = item.ProductFormatID; stock = item.StockQuantity; image = item.Image; productName = item.ProductName; unitPrice = item.UnitPrice; } var repeat = procedure.SearchRepeatCart(ticket.UserData, productformatid); if (repeat != null) { var isempty = 4; return(isempty); } else { if (int.Parse(quantity) <= stock) { ShoppingCart shoppingCart = new ShoppingCart { MemberID = ticket.UserData, ProductFormatID = productformatid, ProductID = int.Parse(productid), Quantity = int.Parse(quantity), }; var repository = new ShoppingCartRepository(); repository.Create(shoppingCart); var isempty = 2; CartIconNumber(); return(isempty); } else { var isempty = 3; return(isempty); } } }
public void Creates_A_Cart() { var cartBuilder = new CartBuilder(); var address = Builders.Utility.AddressGenerate(); var shoppingCartRepository = new ShoppingCartRepository(_shoppingCartDatabaseSettings); var cartOne = cartBuilder.GenerateCart(address, CustomerType.Premium, ShippingMethod.Expedited); var cartTwo = cartBuilder.GenerateCart(address, CustomerType.Premium, ShippingMethod.Express); var cartThree = cartBuilder.GenerateCart(address, CustomerType.Premium, ShippingMethod.Priority); shoppingCartRepository.Create(cartOne); shoppingCartRepository.Create(cartTwo); shoppingCartRepository.Create(cartThree); var result = shoppingCartRepository.FindAll(); result.Should().HaveCount(3); }
public void GetById_hasThreeCartsInDB_returnReturnOnlyCartWithCorrectId() { var target = new ShoppingCartRepository(_databaseSettings); var cart1 = new CartBuilder().WithId(null).WithCustomerId("1").Build(); target.Create(cart1); var cart2 = new CartBuilder().WithId(null).WithCustomerId("2").Build(); target.Create(cart2); var cart3 = new CartBuilder().WithId(null).WithCustomerId("3").Build(); target.Create(cart3); var actual = target.FindById(cart2.Id); Assert.Equal(cart2, actual); }
public void FindAll_HasTwoCartsInDB_ReturnAllCarts() { var target = new ShoppingCartRepository(_databaseSettings); var cart1 = new CartBuilder().WithId(null).WithCustomerId("1").Build(); target.Create(cart1); var cart2 = new CartBuilder().WithId(null).WithCustomerId("2").Build(); target.Create(cart2); var actual = target.FindAll().ToList(); var expected = new List <Cart> { cart1, cart2 }; Assert.Equal(expected, actual); }
public void Finds_A_New_Cart() { var cartBuilder = new CartBuilder(); var address = Builders.Utility.AddressGenerate(); var shoppingCartRepository = new ShoppingCartRepository(_shoppingCartDatabaseSettings); var cart = cartBuilder.GenerateCart(address, CustomerType.Premium, ShippingMethod.Expedited); var storedCart = shoppingCartRepository.Create(cart); var result = shoppingCartRepository.FindById(storedCart.Id); result.Should().BeEquivalentTo(cart); }
public void GetById_CartNotFound_ReturnNull() { var target = new ShoppingCartRepository(_databaseSettings); var cart1 = new CartBuilder().WithId(null).WithCustomerId("1").Build(); target.Create(cart1); var cart2 = new CartBuilder().WithId(null).WithCustomerId("2").Build(); target.Create(cart2); var cart3 = new CartBuilder().WithId(null).WithCustomerId("3").Build(); target.Create(cart3); var actual = target.FindById(Invalid_ID); Assert.Null(actual); }
public void RemoveById_CartFound_RemoveFromDb() { var target = new ShoppingCartRepository(_databaseSettings); var cart1 = new CartBuilder().WithId(null).WithCustomerId("1").Build(); target.Create(cart1); target.Remove(cart1.Id); var result = target.FindById(cart1.Id); Assert.Null(result); }
public ActionResult <ShoppingCart> Post([FromBody] ShoppingCartModel shoppingCart) { if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } var newShoppingCart = new ShoppingCart(shoppingCart.Date, shoppingCart.Description); _shopppingCartRepository.Create(newShoppingCart); _shopppingCartRepository.Save(); return(CreatedAtRoute("Post", new { Id = newShoppingCart.Id }, newShoppingCart)); }
public void Update_CartFound_UpdateValue() { var target = new ShoppingCartRepository(_databaseSettings); var cart1 = new CartBuilder().WithId(null).WithCustomerId("1").Build(); target.Create(cart1); cart1.CustomerId = "other"; target.Update(cart1.Id, cart1); var result = target.FindById(cart1.Id); Assert.Equal("other", cart1.CustomerId); }
public void CalculateTotals_ShippingCartFound_ReturnTotals() { var repository = new ShoppingCartRepository(_databaseSettings); var cart = new CartBuilder() .WithId(null) .WithItems(new List <Item> { CreateItem() }) .Build(); repository.Create(cart); var target = CreateShoppingCartController(repository); var actual = target.CalculateTotals(cart.Id, null); Assert.NotEqual(0.0, actual.Value.Total); }
public bool CreateShoppingCart(string _account, int Product_ID, int Quantity, string Size, string Color) { //宣告 庫存,購物車,產品 的 Rpository StockRepository sizeQuantityRepository = RepositoryContainer.GetInstance <StockRepository>(); ShoppingCartRepository shoppingCart = RepositoryContainer.GetInstance <ShoppingCartRepository>(); ProductsRepository products = RepositoryContainer.GetInstance <ProductsRepository>(); //先取得那項產品庫存 放到 stock var stock = sizeQuantityRepository.GetByPK(Product_ID, Size, Color); //如果庫存量少與需求量 回傳 false if (stock.Quantity < Quantity) { return(false); } //取得 目前 Account 的 購物車 var myCart = shoppingCart.GetByAccount(_account); //查看購物車是否有那項產品 //如果有 把需要的數量 新增 進原有的購物車的裡面 var items = myCart.FirstOrDefault(x => (x.Product_ID == Product_ID) && (x.size == Size) && (x.Color == Color)); if (items != null) { //去更新購物車的那項產品 shoppingCart.Update(items.Shopping_Cart_ID, items.Quantity + Quantity); } else { //如果購物車沒有那項產品就新增一項產品進到購物車 var model = new Shopping_Cart() { Account = _account, Product_ID = Product_ID, size = Size, UnitPrice = products.GetByProduct_ID(Product_ID).UnitPrice, Quantity = (short)Quantity, Color = Color, }; shoppingCart.Create(model); } return(true); }
public void ShoppingCartRepositoryTests_Create() { ShoppingCartRepository repository = new ShoppingCartRepository(); var model = new Shopping_Cart() { Account = "Bill", Product_ID = 1, Quantity = 5, size = "M" }; repository.Create(model); var result = repository.GetAll(); var test = result.Where(x => x.Account == "Bill"); foreach (var item in test) { Assert.IsTrue(item.Account == "Bill"); } }
public void GetAll_HasOneCart_returnAllShoppingCartsInformation() { var repository = new ShoppingCartRepository(_databaseSettings); var cart = new CartBuilder() .WithId(null) .WithCustomerId("1") .WithItems(new List <Item> { CreateItem() }) .Build(); repository.Create(cart); var target = CreateShoppingCartController(repository); var actual = target.GetAll(); var cartItem = cart.Items[0]; var expected = new ShoppingCartDto { Id = cart.Id, CustomerId = cart.CustomerId, CustomerType = cart.CustomerType, ShippingAddress = cart.ShippingAddress, ShippingMethod = cart.ShippingMethod, Items = new List <ItemDto> { new(ProductId : cartItem.ProductId, ProductName : cartItem.ProductName, Price : cartItem.Price, Quantity : cartItem.Quantity ) } }; Assert.Equal(expected, actual.Single()); }
public void Delete_ValidData_RemoveShoppingCartToDB() { var cart = new CartBuilder() .WithId(null) .WithCustomerId("1") .WithItems(new List <Item> { CreateItem() }) .Build(); var repository = new ShoppingCartRepository(_databaseSettings); repository.Create(cart); var target = CreateShoppingCartController(repository); var result = target.DeleteCart(cart.Id); var value = repository.FindById(cart.Id); Assert.Null(value); }