Example #1
0
 public ActionResult Register(User aUser)
 {
     if (ModelState.IsValid)
     {
         if (_context.Users.Where(m => m.UserName == aUser.UserName).FirstOrDefault() == null)
         {
             User objuser = new User();
             objuser.CreatedOn = DateTime.Now;
             objuser.Email     = aUser.Email;
             objuser.UserName  = aUser.UserName;
             objuser.Password  = aUser.Password;
             objuser.Phone     = aUser.Phone;
             objuser.userType  = 0;
             _context.Users.Add(objuser);
             _context.SaveChanges();
             return(RedirectToAction("Login", "Users"));
         }
         else
         {
             ModelState.AddModelError("Error", "User Name is already exists!");
             return(View());
         }
     }
     return(View());
 }
Example #2
0
        public ActionResult emptyCart()
        {
            var userID = HttpContext.Session.GetString("UserID");

            if (userID != null)
            {
                var cart      = ShoppingCart.GetCart(userID.ToString());
                var cartItems = cart.GetCartItems();
                foreach (var cartItem in cartItems)
                {
                    cart.RemoveFromCart(cartItem);
                }
                db.SaveChanges();



                return(RedirectToAction("Index", "ShoppingCart"));
            }
            else
            {
                return(RedirectToAction("Login", "Users"));
            }
        }
        // GET: Transactions/AddressAndPayment/
        public ActionResult AddressAndPayment()
        {
            var userID = HttpContext.Session.GetInt32("UserID");

            if (userID != null)
            {
                User user = db.Users.Single(user => user.UserId == userID);

                if (user.Transactions == null)
                {
                    user.Transactions = new List <Transaction>();
                }



                if (user.Transactions.Count != 0 && user.Transactions.Last() != null && user.Transactions.Last().Paid == false)
                {
                    var transId = user.Transactions.Last().TransactionId;
                    user.Transactions.Remove(user.Transactions.Last());
                    db.Transactions.Remove(db.Transactions.Single(T => T.TransactionId == transId));
                }
                ShoppingCart shoppingCart = new ShoppingCart
                {
                    ShoppingCartId = HttpContext.Session.GetString("UserID").ToString()
                                     //ShoppingCartId = user.UserId.ToString()
                };
                var viewModel = new ShoppingCartViewModel
                {
                    CartItems            = shoppingCart.GetCartItems(),
                    CartTotal            = shoppingCart.GetTotal(),
                    Amounts              = shoppingCart.GetAmount(),
                    TotalPricePerProduct = shoppingCart.GetProductTotal()
                };
                List <Product>        pro     = shoppingCart.GetCartItems();
                Dictionary <int, int> amonuts = viewModel.Amounts;
                foreach (Product p in pro)
                {
                    // db.Products.Where(product => product.ProductId == p.ProductId).Select(product => product.Quantity)++;
                    p.Quantity += amonuts[p.ProductId];
                }

                Transaction transaction = shoppingCart.CreateTransaction(shoppingCart);
                if (transaction.Amount != 0)
                {
                    //user.Transactions = new List<Transaction>();
                    user.Transactions.Add(transaction);
                    // shoppingCart.emptyCart();

                    db.SaveChanges();
                    if (transaction != null)
                    {
                        TransactionViewModel transactionView = new TransactionViewModel
                        {
                            CartItems = db.Products.Where(product => product.CartId == user.UserId.ToString()).ToList(),
                            amount    = transaction.Amount
                        };



                        //return View(transactionView);    // We can redirect to wherever we want..
                        return(RedirectToAction("emptyCart", "ShoppingCart"));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Transactions"));
                    }
                }
            }
            return(RedirectToAction("Index", "Products"));
        }