Example #1
0
        public async Task <IActionResult> Create(int?pid)
        {
            if (HttpContext.Session.GetString("username") == null)
            {
                return(RedirectToAction("Login", "Client", new { msg = "Login first!" }));
            }
            var product = await _context.Product.FindAsync(pid);

            var cartItem = await _context.Cart.Where(c => c.CustomerID == HttpContext.Session.GetInt32("userID")).FirstOrDefaultAsync(c => c.ProductID == product.ProductID);

            if (cartItem == null)
            {
                var cartitem = new Cart
                {
                    ProductID  = product.ProductID,
                    CustomerID = (int)HttpContext.Session.GetInt32("userID"),
                    Price      = product.Price,
                    Quantity   = 1
                };
                _context.Cart.Add(cartitem);
                await _context.SaveChangesAsync();
            }
            else
            {
                cartItem.Quantity += 1;
                cartItem.Price     = product.Price * cartItem.Quantity;
                _context.Cart.Update(cartItem);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Index)));
        }
Example #2
0
        public async Task <IActionResult> Signup(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _context.Customer.FirstOrDefaultAsync(u => u.UserName == model.UserName);

                if (user != null)
                {
                    return(RedirectToAction("Signup", new { msg = "Username exists" }));
                }
                else
                {
                    byte[] hash, salt;
                    PasswordHashing(model.Password, out hash, out salt);
                    var newCustomer = new Customer
                    {
                        UserName     = model.UserName,
                        PasswordHash = hash,
                        PasswordSalt = salt
                    };
                    _context.Customer.Add(newCustomer);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(model));
        }