public Guid AddToBasket(Guid?sessionId, Guid productId) { var newSessionId = sessionId ?? Guid.NewGuid(); var product = _productContext.Products.First(x => x.Id == productId); var session = GetSession(newSessionId); var cartDetails = _cartContext.CartSessionDetails.FirstOrDefault(x => x.ProductId == productId && x.CartSessionId == newSessionId); if (cartDetails == null) { var detail = new CartSessionDetail() { CartSessionId = newSessionId, Id = Guid.NewGuid(), Price = product.Price, ProductId = productId, ProductName = product.Name, Quantity = 1 }; _cartContext.Add(detail); } else { cartDetails.Quantity++; } _cartContext.SaveChanges(); return(newSessionId); }
public ActionResult Index(Models.DB.Models.OrderInfo postback) { if (this.ModelState.IsValid) { using (CartContext db = new CartContext()) { var currCart = cartFunction.GetCurrCart(); //var currCart = (CartItem)System.Web.HttpContext.Current.Session["Cart"]; var newOrder = new OrderInfo() { CusName = postback.CusName, CusPhone = postback.CusPhone, TableNo = postback.TableNo, Total = currCart.TotalAmount, OrderStatus = "Payment" }; //int currOID = newOrder.OrderID; db.OrderInfos.Add(newOrder); db.SaveChanges(); var Order = currCart.ToOrderDetailList(newOrder.OrderID); db.OILs.AddRange(Order); db.SaveChanges(); currCart.ClearCart(); return(RedirectToAction("Info", new { id = newOrder.OrderID })); //return PartialView("_CartPartial"); } //return RedirectToAction("Action", new { id = currOID }); } //return RedirectToAction("Action", new { id = currOID }); return(View()); }
/// Register public IActionResult Register([Bind("Id,Username,Password,IsAdmin,IsGuest")] User user) { byte[] data = System.Text.Encoding.ASCII.GetBytes(user.Password); data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data); user.Password = System.Text.Encoding.ASCII.GetString(data); _context.Add(user); _context.SaveChanges(); return(View("RegisterLoginView")); }
public IActionResult BuyAll() { int cartId = _cartContext.Carts.Where(o => o.UserId == _userManager.GetUserId(User)).FirstOrDefault().Id; List <CartItem> list = _cartContext.CartItems.Where(o => o.CartId == cartId).ToList(); _cartContext.CartItems.RemoveRange(list); _cartContext.SaveChanges(); return(RedirectToAction("Cart", "Profile")); }
public int MakeOrder(string userid) { Order order = new Order(); order.CustomerId = userid; order.OrderDate = DateTime.Now; dbcontext.Add(order); int count = dbcontext.SaveChanges(); return(count); }
public ActionResult Create([Bind(Include = "ProductID,Name,Price,Quantity")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public IHttpActionResult PostProductOrder(int Id) { var product = db.Products.Where(x => x.Id == Id).FirstOrDefault(); if (product == null) { return(NotFound()); } var order = db.Orders.Where(x => x.CustomerId == 1).FirstOrDefault(); var testUser = db.Customers.Where(x => x.Id == 1).FirstOrDefault(); if (order == null) { order = new Models.Order(); order.OrderDate = DateTime.Now; order.Status = "Open"; order.CustomerId = testUser.Id; order.Customer = testUser; } OrderDetail ordDet = db.OrderDetails.Where(x => x.ProductId == Id).FirstOrDefault(); if (ordDet == null) { ordDet = new OrderDetail(); ordDet.Amount = product.Price; // ordDet.OrderId = order.Id; // ordDet.ProductId = product.Id; ordDet.Quantity = 1; ordDet.UnitCost = product.Price; ordDet.Order = order; ordDet.Product = product; db.OrderDetails.Add(ordDet); } else { ordDet.Quantity = ordDet.Quantity + 1; ordDet.Amount = ordDet.Quantity * ordDet.UnitCost; } db.SaveChanges(); order.OrderDetails = null; return(CreatedAtRoute("DefaultApi", new { controller = "products", id = order.Id }, ordDet)); }
////////////////////////////////////////////////////////////// /// Client Calls ////////////////////////////////////////////////////////////// public void AddEntry([Bind("Id,EntryItemId,UserId,Quantity")] CartEntry cartEntry) { if (cartEntry != null) { var dupEntry = _context.CartEntries.SingleOrDefault(e => (e.UserId == cartEntry.UserId && e.EntryItemId == cartEntry.EntryItemId)); if (dupEntry == null) { _context.Add(cartEntry); _context.SaveChanges(); } } }
public ActionResult AddToCart(int itemId, string itemName, string itemImageUrl, double itemPrice, String from) { UserEntity currentUser = (UserEntity)Session["CurrentUser"]; Cart cart = new Cart(); cart.itemId = itemId; cart.itemName = itemName; cart.itemImageUrl = itemImageUrl; cart.itemPrice = itemPrice; cart.UserName = currentUser.UserName; db.CartSet.Add(cart); db.SaveChanges(); return(RedirectToAction("Index", from)); }
public int AddCart(int ItemId) { string username = Session["username"].ToString(); Cart objcart = new Cart() { UserName = username, ItemId = ItemId }; dbObj.Cart.Add(objcart); dbObj.SaveChanges(); int count = dbObj.Cart.Where(s => s.UserName == username).Count(); return(count); }
public IActionResult Logout() { //save user cart into cart entities HttpContext.Session.Clear(); dbcontext.SaveChanges(); return(RedirectToAction("ViewProductsWithoutLogin", "Product")); }
// Removes one product type from the cart public void Remove(int productId, string userId) { using (CartContext ctx = new CartContext()) { var cartItems = ctx.CartItems.Where(x => x.ProductId == productId && x.UserId == userId); ctx.CartItems.RemoveRange(cartItems); ctx.SaveChanges(); } }
// Changes the quantity of a product in the cart public void Update(int productId, int quantity, string userId) { using (var ctx = new CartContext()) { var item = ctx.CartItems.SingleOrDefault(i => i.ProductId == productId && i.UserId == userId); item.Quantity = quantity; ctx.SaveChanges(); } }
public ActionResult Delete(int id) { int currID = id; CartContext db = new CartContext(); var OInfoList = (from s in db.OrderInfos where s.OrderID == currID select s); var OItemList = (from s in db.OILs where s.OID == currID select s); db.OILs.RemoveRange(OItemList.ToList()); db.OrderInfos.RemoveRange(OInfoList.ToList()); db.SaveChanges(); var currCart = cartFunction.GetCurrCart(); currCart.ClearCart(); db.SaveChanges(); return(RedirectToAction("Deleted", new { id = currID })); }
public CartM getCart([FromRoute] int id) { var cart = _context.Carts.Where(c => c.BuyerId == id) .Include(i => i.Items) .FirstOrDefault(); //If no cart yet if (cart == null) { var newCart = new CartM(); newCart.BuyerId = id; _context.Carts.Add(newCart); _context.SaveChanges(); return(newCart); } return(cart); }
public int Post(Cart cart) { // todo some more validation and error handling would be nice! if (cart != null) { _cartDbContext.Carts.Add(cart); _cartDbContext.SaveChanges(); } return(cart.Id); }
public CartController(CartContext context) { // DI to inject the database context _context = context; if (_context.CartItems.Count() == 0) {// Adds an item to the DB if the DB is empty _context.CartItems.Add(new CartItem { productID = 2125, productName = "Cheese", quantity = 5, shoppingCartID = 1 }); _context.SaveChanges(); } }
public async Task <IActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { User user = new User { Email = model.Email, UserName = model.Email, Year = model.Year }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action( "ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //EmailService emailService = new EmailService(); await _emailService.SendEmailAsync(model.Email, "Confirm your account", $"Confirm your email address: <a href='{callbackUrl}'>link</a>"); Cart new_cart = new Cart { UserId = user.Id }; _cartContext.Carts.Add(new_cart); _cartContext.SaveChanges(); var Role = _roleManager.Roles.FirstOrDefault(o => o.Name == "user"); if (Role != null) { await _userManager.AddToRoleAsync(user, "user"); } _logger.LogInformation($"Processing request {this.Request.Path} at {DateTime.Now:hh:mm:ss}"); return(Content("Follow the link from email message to finish your registration.")); //await _signInManager.SignInAsync(user, false); //return RedirectToAction("Index", "Home"); } else { _logger.LogError($"Error in {this.Request.Path} at {DateTime.Now:hh:mm:ss}"); foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } } return(View(model)); }
public IHttpActionResult PutCarts(int id, Carts carts) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != carts.Id) { return(BadRequest()); } var userId = User.Identity.GetUserId(); if (userId != carts.UserId) { return(StatusCode(HttpStatusCode.Conflict)); } db.Entry(carts).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CartsExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Register(Register userObj) { if (ModelState.IsValid) { Session["userID"] = userObj.Username; dbObj.Users.Add(userObj); dbObj.SaveChanges(); return(RedirectToAction("Index")); } else { return(View("Register")); } }
public IActionResult WriteReview(IFormCollection form) { var ReviewerId = TempData["userid"]; Review r = new Review(); r.Id = Guid.NewGuid().ToString(); r.ProductId = form["ProductId"]; r.UserId = form["ReviewerId"]; r.ReviewText = form["ReviewText"]; dbcontext.Add(r); dbcontext.SaveChanges(); return(RedirectToAction("ViewPurchase", "MyPurchase", new { userId = r.UserId })); }
// Adds an entity public void Save(CartItemEntity itemValidate, bool addOne) { using (CartContext ctx = new CartContext()) { var cartItem = ctx.CartItems.SingleOrDefault(x => x.ProductId == itemValidate.ProductId && x.UserId == itemValidate.UserId); if (cartItem == null) { ctx.CartItems.Add(itemValidate); } else { cartItem.Quantity = addOne ? cartItem.Quantity + 1: itemValidate.Quantity; } ctx.SaveChanges(); } }
private ShippingDetails AddShippingDetails(ShippingDetailsModel Model) { ShippingDetails order = null; using (var db = new CartContext()) { var ShippingDetails = new ShippingDetails() { Name = Model.Name, Address = Model.Address, City = Model.City, Country = Model.Country, Username = Session["Username"].ToString() }; order = db.ShippingDetails.Add(ShippingDetails); db.SaveChanges(); } return(order); }
public async Task <ActionResult> Delete(string id) { User user = await _userManager.FindByIdAsync(id); if (user != null) { Cart del_cart = _cartContext.Carts.FirstOrDefault(o => o.UserId == id); if (del_cart != null) { _cartContext.Carts.Remove(del_cart); _cartContext.SaveChanges(); } IdentityResult result = await _userManager.DeleteAsync(user); } _logger.LogInformation($"Processing request {this.Request.Path} at {DateTime.Now:hh:mm:ss}"); return(RedirectToAction("Index")); }
private void AddFruits(ShippingDetailsModel Model, ShippingDetails details) { using (var db = new CartContext()) { var fruits = (List <Fruit>)Session["cart"]; foreach (var f in fruits) { var fruit = new Fruit() { FruitType = f.FruitType, Amount = f.Amount, Price = f.Price, Weight = f.Weight, ShippingDetails = details }; db.Fruit.Add(fruit); db.SaveChanges(); } } }
public static void Registration() { User newUser = new User() { Login = SetInformations.SetLogin(), Password = SetInformations.SetPassword() }; Cart newCart = new Cart() { User = newUser }; using (var context = new CartContext()) { if (context.Users.All(user => user.Login != newUser.Login)) { context.Carts.Add(newCart); context.SaveChanges(); } } }
public string receivecartfromlogout([FromBody] Result result) { dbcontext.Database.EnsureDeleted(); //clears old cart records dbcontext.Database.EnsureCreated(); List <Cart> receivedcart = new List <Cart>(); receivedcart = JsonConvert.DeserializeObject <List <Cart> >(result.Value.ToString()); Cart newcart = new Cart(); foreach (Cart cart in receivedcart) // this adds new data { newcart.Id = Guid.NewGuid().ToString(); newcart.productId = cart.productId; newcart.userId = cart.userId; dbcontext.Add(newcart); dbcontext.SaveChanges(); } return(System.Text.Json.JsonSerializer.Serialize(result)); }
public ActionResult Create(FormCollection collection) { try { var x = collection.Count; Product p = new Product(); p.Code = collection["Code"]; p.Description = collection["Description"]; p.Price = Double.Parse(collection["Price"]); ctx.ProductSet.Add(p); ctx.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public void GetProduct_PageSizeIsThree_ReturnsThreeProducts() { // Arrange var options = new DbContextOptionsBuilder <CartContext>() .UseInMemoryDatabase($"CartDatabaseForTesting{Guid.NewGuid()}") .Options; using (var context = new CartContext(options)) { context.Products.Add(new Entities.Product() { Description = "book", Price = 120.3 }); context.Products.Add(new Entities.Product() { Description = "pencil", Price = 0.5 }); context.Products.Add(new Entities.Product() { Description = "erase", Price = 0.3 }); context.Products.Add(new Entities.Product() { Description = "pen", Price = 10.3 }); context.SaveChanges(); } using (var context = new CartContext(options)) { var productRepository = new ProductRepository(context); // Act var products = productRepository.GetProducts(1, 3); // Assert Assert.Equal(3, products.Count()); } }
public ActionResult Update(int id) { CartContext db = new CartContext(); var OInfoList = (from s in db.OrderInfos where s.OrderID == id select s); var OItemList = (from s in db.OILs where s.OID == id select s); // var OInfoList = (from s in db.OrderInfos select s); //var OItemList = (from s in db.OILs select s); int currID = id; var currCart = cartFunction.GetCurrCart(); var Order = currCart.ToOrderDetailList(id); db.OILs.AddRange(Order); db.SaveChanges(); currCart.ClearCart(); var model = new OrderViewModel() { OrderinfoList = OInfoList, OrderItemList = OItemList }; return(RedirectToAction("Updated", new { id = currID })); }