public IActionResult AddToCart(int id) { ShopingCart shopingCart = HttpContext.Session.GetObjectFromJson <ShopingCart>("Cart"); if (shopingCart == null) { shopingCart = new ShopingCart(); } Items Item = oItemservice.GetbyId(id); ShopingCartItem shopingItem = shopingCart.shopingCartItems.Where(a => a.ItemId == id).FirstOrDefault(); if (shopingItem != null)// check if this item in cart or not { shopingItem.Qty++; shopingItem.Total = shopingItem.Price * shopingItem.Qty; } else { shopingCart.shopingCartItems.Add(new ShopingCartItem { ItemId = Item.Id, ItemName = Item.Name, Price = Item.SalesPrice, ImageName = Item.ImageName, Qty = 1, Total = Item.SalesPrice }); } shopingCart.Total = shopingCart.shopingCartItems.Sum(a => a.Total); HttpContext.Session.SetObjectAsJson("Cart", shopingCart); return(Redirect("/Home/Shop")); }
private static void MigrateShopingCart() { var uow = new SbsData(); const string ConnectionString = "Data Source=.;Initial Catalog=BooksDBSQL;Integrated Security=True"; const string queryString = "SELECT * FROM dbo.ShopCart"; using (SqlConnection connection = new SqlConnection(ConnectionString)) { SqlCommand command = new SqlCommand(queryString, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { var bookId = reader.GetString(1); var book = uow.Books.All().FirstOrDefault(x => x.BookId == bookId); if (book != null) { var cartItem = new ShopingCartItem() { OrderId = reader.GetString(0), BookId = book.Id, }; uow.ShopingCart.Add(cartItem); uow.SaveChanges(); } } Console.WriteLine("Done"); reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }