Ejemplo n.º 1
0
 public ActionResult PackageGame(int id)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         return(View(db.Packages.Where(x => x.gameid == id).ToList()));
     }
 }
Ejemplo n.º 2
0
        private int IsValid(UserLoginModels user)
        {
            int flag = 0;

            using (JShopVNEntities db1 = new JShopVNEntities())
            {
                var uLogin = new Account();
                if (db1.Accounts
                    .Where(b => b.username.Equals(user.username) && b.password.Equals(user.password))
                    .FirstOrDefault() != null)
                {
                    uLogin = db1.Accounts.Where(b => b.username.Equals(user.username) && b.password.Equals(user.password)).FirstOrDefault();
                }

                if (uLogin != null)
                {
                    if (uLogin.roleid == 1)
                    {
                        flag = 1;
                    }
                    else if (uLogin.roleid == 2)
                    {
                        flag = 2;
                    }
                }
                else
                {
                    flag = 0;
                }
            }
            return(flag);
        }
Ejemplo n.º 3
0
 public ActionResult ListCart(int id)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         return(View(db.Carts.Where(x => x.userid == id).ToList()));
     }
 }
Ejemplo n.º 4
0
 public ActionResult CancelOrder(int id)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         Order od = db.Orders.Where(x => x.id == id).FirstOrDefault();
         od.status = "Đã hủy";
         var local = db.Set <Order>()
                     .Local
                     .FirstOrDefault(f => f.id == id);
         if (local != null)
         {
             db.Entry(local).State = EntityState.Detached;
         }
         db.Entry(od).State = EntityState.Modified;
         db.SaveChanges();
         User us = db.Users.Where(x => x.id == od.userid).FirstOrDefault();
         us.balance += od.totalprice;
         var local2 = db.Set <User>()
                      .Local
                      .FirstOrDefault(f => f.id == us.id);
         if (local2 != null)
         {
             db.Entry(local2).State = EntityState.Detached;
         }
         db.Entry(us).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("ListOrder", "Customer", new { id = od.userid }));
     }
 }
Ejemplo n.º 5
0
 // GET: Customer
 public ActionResult Index()
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         return(View(db.Games.ToList()));
     }
 }
Ejemplo n.º 6
0
 public ActionResult Decrease(int id, Cart cart)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         if (db.Carts.Where(x => x.id == id).FirstOrDefault().quantity == 1)
         {
             TempData["ErrorMes"] = "<script>alert('Số lượng mặt hàng không thể bé hơn 1');</script>";
             return(RedirectToAction("ListCart", "Customer", new { id = db.Carts.Where(x => x.id == id).FirstOrDefault().userid }));
         }
         else
         {
             cart             = db.Carts.Where(x => x.id == id).FirstOrDefault();
             cart.quantity   -= 1;
             cart.totalprice -= db.Packages.Where(x => x.id == cart.packid).FirstOrDefault().price;
             var local = db.Set <Cart>()
                         .Local
                         .FirstOrDefault(f => f.id == id);
             if (local != null)
             {
                 db.Entry(local).State = EntityState.Detached;
             }
             db.Entry(cart).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("ListCart", "Customer", new { id = cart.userid }));
         }
     }
 }
Ejemplo n.º 7
0
        public ActionResult EditProfile()
        {
            var username = Session["username"].ToString();

            using (JShopVNEntities db = new JShopVNEntities())
            {
                return(View(db.Users.Where(x => x.accid == db.Accounts.Where(y => y.username.Equals(username)).FirstOrDefault().id).FirstOrDefault()));
            }
        }
Ejemplo n.º 8
0
 public ActionResult Delete(int id, Cart cart)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         cart = db.Carts.Where(x => x.id == id).FirstOrDefault();
         var uid = cart.userid;
         db.Carts.Remove(cart);
         db.SaveChanges();
         return(RedirectToAction("ListCart", "Customer", new { id = uid }));
     }
 }
Ejemplo n.º 9
0
 public ActionResult ChangePassword(Account acc)
 {
     try
     {
         if (Session["username"] != null)
         {
             using (JShopVNEntities db = new JShopVNEntities())
             {
                 var username = Session["username"].ToString();
                 var opass    = Request["oldpass"].ToString();
                 var npass    = Request["newpass"].ToString();
                 var cnpass   = Request["confirmnewpass"].ToString();
                 acc = db.Accounts.Where(x => x.username.Equals(username)).FirstOrDefault();
                 if (acc.password.Equals(opass))
                 {
                     if (npass.Equals(cnpass))
                     {
                         acc.password = npass;
                         var local = db.Set <Account>()
                                     .Local
                                     .FirstOrDefault(f => f.username.Equals(username));
                         if (local != null)
                         {
                             db.Entry(local).State = EntityState.Detached;
                         }
                         db.Entry(acc).State = EntityState.Modified;
                         db.SaveChanges();
                         return(RedirectToAction("ViewProfile"));
                     }
                     else
                     {
                         ViewBag.ErrorMessege = "<script>alert('Confirm không trùng với password mới');</script>";
                         return(View());
                     }
                 }
                 else
                 {
                     ViewBag.ErrorMessege = "<script>alert('Password cũ sai');</script>";
                     return(View());
                 }
             }
         }
         else
         {
             return(RedirectToAction("LoginA", "Login"));
         }
     }
     catch
     {
         ViewBag.ErrorMessege = "<script>alert('Exception');</script>";
         return(View());
     }
 }
Ejemplo n.º 10
0
 public ActionResult EditProfile(int id, User us)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         var local = db.Set <User>().Local.FirstOrDefault(f => f.id == id);
         if (local != null)
         {
             db.Entry(local).State = EntityState.Detached;
         }
         db.Entry(us).State = EntityState.Modified;
         db.SaveChanges();
         TempData["msg"] = "<script>alert('Cập nhật thông tin người dùng thành công');</script>";
         return(RedirectToAction("viewProfile"));
     }
 }
Ejemplo n.º 11
0
 public ActionResult Increase(int id, Cart cart)
 {
     using (JShopVNEntities db = new JShopVNEntities())
     {
         cart             = db.Carts.Where(x => x.id == id).FirstOrDefault();
         cart.quantity   += 1;
         cart.totalprice += db.Packages.Where(x => x.id == cart.packid).FirstOrDefault().price;
         var local = db.Set <Cart>()
                     .Local
                     .FirstOrDefault(f => f.id == id);
         if (local != null)
         {
             db.Entry(local).State = EntityState.Detached;
         }
         db.Entry(cart).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("ListCart", "Customer", new { id = cart.userid }));
     }
 }
Ejemplo n.º 12
0
        public ActionResult ResetPassword(EmailModel model)
        {
            string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
            string senderPass  = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();

            using (JShopVNEntities db = new JShopVNEntities())
            {
                if (db.Accounts.Where(x => x.username.Equals(model.Username)).FirstOrDefault() != null)
                {
                    if (db.Users.Where(x => x.accid == (db.Accounts.Where(y => y.username.Equals(model.Username)).FirstOrDefault().id)).FirstOrDefault().email.Equals(model.Email))
                    {
                        using (MailMessage mm = new MailMessage(senderEmail, model.Email))
                        {
                            mm.Subject = "Reset password";
                            Random rnd = new Random();

                            string pass = "";
                            for (int x = 0; x < 6; x += 1)
                            {
                                char randomChar = (char)rnd.Next('a', 'z');
                                pass += randomChar;
                            }
                            Account us = db.Accounts.Where(x => x.username.Equals(model.Username)).FirstOrDefault();
                            us.password = pass;
                            var local = db.Set <Account>()
                                        .Local
                                        .FirstOrDefault(f => f.username.Equals(model.Username));
                            if (local != null)
                            {
                                db.Entry(local).State = EntityState.Detached;
                            }
                            db.Entry(us).State = EntityState.Modified;
                            db.SaveChanges();
                            mm.Body       = "reset lại password cho nè:  " + pass;
                            mm.IsBodyHtml = false;
                            using (SmtpClient smtp = new SmtpClient())
                            {
                                smtp.Host      = "smtp.gmail.com";
                                smtp.EnableSsl = true;
                                NetworkCredential NetworkCred = new NetworkCredential(senderEmail, senderPass);
                                smtp.UseDefaultCredentials = true;
                                smtp.Credentials           = NetworkCred;
                                smtp.Port = 587;
                                smtp.Send(mm);
                                ViewBag.Message = "Email sent.";
                            }
                        }

                        return(View());
                    }
                    else
                    {
                        ViewBag.ErrorMessege = "<script>alert('Email đăng ký không trùng khớp');</script>";
                        return(View());
                    }
                }
                else
                {
                    ViewBag.ErrorMessege = "<script>alert('Không có user');</script>";
                    return(View());
                }
            }
        }
Ejemplo n.º 13
0
 public ActionResult Register(RegisterUserModel user)
 {
     if (ModelState.IsValid)
     {
         using (JShopVNEntities db = new JShopVNEntities())
         {
             bool           flag = true;
             Account        acc  = new Account();
             User           us   = new User();
             List <Account> la   = db.Accounts.ToList();
             List <User>    lu   = db.Users.ToList();
             foreach (Account a in la)
             {
                 if (user.username.ToLower().Equals(a.username.ToLower()))
                 {
                     flag = false;
                 }
             }
             if (flag)
             {
                 if (la.Count == 0)
                 {
                     acc.id       = 1;
                     acc.username = user.username;
                     acc.password = user.password;
                     acc.roleid   = 2;
                     db.Accounts.Add(acc);
                     db.SaveChanges();
                     if (lu.Count == 0)
                     {
                         us.id       = 1;
                         us.name     = user.name;
                         us.phone    = user.phone;
                         us.email    = user.email;
                         us.facebook = user.facebook;
                         us.balance  = 0;
                         us.accid    = acc.id;
                         db.Users.Add(us);
                         db.SaveChanges();
                     }
                     else
                     {
                         us.id       = lu[lu.Count - 1].id + 1;
                         us.name     = user.name;
                         us.phone    = user.phone;
                         us.email    = user.email;
                         us.facebook = user.facebook;
                         us.balance  = 0;
                         us.accid    = acc.id;
                         db.Users.Add(us);
                         db.SaveChanges();
                     }
                     TempData["msg"] = "<script>alert('Tạo tài khoản thành công');</script>";
                     return(RedirectToAction("Login", "Login"));
                 }
                 else
                 {
                     acc.id       = la[la.Count - 1].id + 1;
                     acc.username = user.username;
                     acc.password = user.password;
                     acc.roleid   = 2;
                     db.Accounts.Add(acc);
                     db.SaveChanges();
                     if (lu.Count == 0)
                     {
                         us.id       = 1;
                         us.name     = user.name;
                         us.phone    = user.phone;
                         us.email    = user.email;
                         us.facebook = user.facebook;
                         us.balance  = 0;
                         us.accid    = acc.id;
                         db.Users.Add(us);
                         db.SaveChanges();
                     }
                     else
                     {
                         us.id       = lu[lu.Count - 1].id + 1;
                         us.name     = user.name;
                         us.phone    = user.phone;
                         us.email    = user.email;
                         us.facebook = user.facebook;
                         us.balance  = 0;
                         us.accid    = acc.id;
                         db.Users.Add(us);
                         db.SaveChanges();
                     }
                     TempData["msg"] = "<script>alert('Tạo tài khoản thành công');</script>";
                     return(RedirectToAction("Login", "Login"));
                 }
             }
             else
             {
                 ViewBag.ErrorMessege = "<script>alert('Tài khoản được tạo đã có, tạo lại tài khoản khác');</script>";
                 return(View());
             }
         }
     }
     else
     {
         return(View());
     }
 }
Ejemplo n.º 14
0
 public ActionResult AddCart(int id)
 {
     if (Session["username"] != null)
     {
         using (JShopVNEntities db = new JShopVNEntities())
         {
             string      user = Session["username"].ToString();
             Cart        ca   = new Cart();
             List <Cart> lc   = db.Carts.ToList();
             if (lc.Count == 0)
             {
                 ca.id         = 1;
                 ca.packid     = id;
                 ca.quantity   = 1;
                 ca.userid     = db.Accounts.Where(x => x.username.Equals(user)).FirstOrDefault().id;
                 ca.totalprice = db.Packages.Where(x => x.id == id).FirstOrDefault().price;
                 db.Carts.Add(ca);
                 db.SaveChanges();
                 TempData["msg"] = "<script>alert('Thêm vào giỏ hàng thành công');</script>";
                 return(RedirectToAction("PackageGame", "Customer", new { id = db.Games.Where(x => x.id == db.Packages.Where(y => y.id == ca.packid).FirstOrDefault().gameid).FirstOrDefault().id }));
             }
             else
             {
                 List <Cart> lc2  = db.Carts.Where(x => x.userid == db.Accounts.Where(y => y.username.Equals(user)).FirstOrDefault().id).ToList();
                 bool        flag = false;
                 foreach (Cart c in lc2)
                 {
                     if (c.packid == id)
                     {
                         flag = true;
                     }
                 }
                 if (flag == true)
                 {
                     Cart ca2 = db.Carts.Where(x => x.packid == id && x.userid == db.Accounts.Where(y => y.username.Equals(user)).FirstOrDefault().id).FirstOrDefault();
                     ca2.quantity   += 1;
                     ca2.totalprice += db.Packages.Where(x => x.id == id).FirstOrDefault().price;
                     var local = db.Set <Cart>().Local.FirstOrDefault(f => f.id == ca2.id);
                     if (local != null)
                     {
                         db.Entry(local).State = EntityState.Detached;
                     }
                     db.Entry(ca2).State = EntityState.Modified;
                     db.SaveChanges();
                     TempData["msg"] = "<script>alert('Thêm vào giỏ hàng thành công');</script>";
                     return(RedirectToAction("PackageGame", "Customer", new { id = db.Games.Where(x => x.id == db.Packages.Where(y => y.id == ca2.packid).FirstOrDefault().gameid).FirstOrDefault().id }));
                 }
                 else
                 {
                     ca.id         = lc[lc.Count - 1].id + 1;
                     ca.packid     = id;
                     ca.quantity   = 1;
                     ca.userid     = db.Accounts.Where(x => x.username.Equals(user)).FirstOrDefault().id;
                     ca.totalprice = db.Packages.Where(x => x.id == id).FirstOrDefault().price;
                     db.Carts.Add(ca);
                     db.SaveChanges();
                     TempData["msg"] = "<script>alert('Thêm vào giỏ hàng thành công');</script>";
                     return(RedirectToAction("PackageGame", "Customer", new { id = db.Games.Where(x => x.id == db.Packages.Where(y => y.id == ca.packid).FirstOrDefault().gameid).FirstOrDefault().id }));
                 }
             }
         }
     }
     else
     {
         return(RedirectToAction("Login", "Login"));
     }
 }
Ejemplo n.º 15
0
        public ActionResult Checkout()
        {
            decimal totalPrice = 0;
            string  user       = Session["username"].ToString();

            using (JShopVNEntities db = new JShopVNEntities())
            {
                int         userid   = db.Users.Where(x => x.accid == db.Accounts.Where(y => y.username.Equals(user)).FirstOrDefault().id).FirstOrDefault().id;
                List <Cart> listCart = db.Carts.Where(x => x.userid == userid).ToList();
                foreach (Cart c in listCart)
                {
                    totalPrice += c.totalprice;
                }
                if (totalPrice > db.Users.Where(x => x.id == userid).FirstOrDefault().balance)
                {
                    TempData["ErrorMes"] = "<script>alert('Tài khoản của bạn không đủ để xác nhận đơn hàng');</script>";
                    return(RedirectToAction("ListCart", "Customer", new { id = userid }));
                }
                else
                {
                    Order              od  = new Order();
                    List <Order>       lo  = db.Orders.ToList();
                    List <OrderDetail> lod = db.OrderDetails.ToList();
                    if (lo.Count == 0)
                    {
                        od.id          = 1;
                        od.status      = "Đang chờ xử lí";
                        od.userid      = userid;
                        od.totalprice  = totalPrice;
                        od.datecreated = DateTime.Now;
                        db.Orders.Add(od);
                        db.SaveChanges();
                        User us = db.Users.Where(x => x.id == userid).FirstOrDefault();
                        us.balance -= totalPrice;
                        var local = db.Set <User>()
                                    .Local
                                    .FirstOrDefault(f => f.id == us.id);
                        if (local != null)
                        {
                            db.Entry(local).State = EntityState.Detached;
                        }
                        db.Entry(us).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        od.id          = lo[lo.Count - 1].id + 1;
                        od.status      = "Đang chờ xử lí";
                        od.userid      = userid;
                        od.totalprice  = totalPrice;
                        od.datecreated = DateTime.Now;
                        db.Orders.Add(od);
                        db.SaveChanges();
                        User us = db.Users.Where(x => x.id == userid).FirstOrDefault();
                        us.balance -= totalPrice;
                        var local = db.Set <User>()
                                    .Local
                                    .FirstOrDefault(f => f.id == us.id);
                        if (local != null)
                        {
                            db.Entry(local).State = EntityState.Detached;
                        }
                        db.Entry(us).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    if (lod.Count == 0)
                    {
                        int odtid = 1;
                        foreach (Cart c in listCart)
                        {
                            OrderDetail odt = new OrderDetail();
                            odt.id       = odtid;
                            odt.orderid  = od.id;
                            odt.packid   = c.packid;
                            odt.quantity = c.quantity;
                            odt.price    = c.totalprice;
                            db.OrderDetails.Add(odt);
                            db.SaveChanges();
                            odtid += 1;
                        }
                        return(RedirectToAction("ListOrder", "Customer", new { id = userid }));
                    }
                    else
                    {
                        int odtid = lod[lod.Count - 1].id + 1;
                        foreach (Cart c in listCart)
                        {
                            OrderDetail odt = new OrderDetail();
                            odt.id       = odtid;
                            odt.orderid  = od.id;
                            odt.packid   = c.packid;
                            odt.quantity = c.quantity;
                            odt.price    = c.totalprice;
                            db.OrderDetails.Add(odt);
                            db.SaveChanges();
                            odtid += 1;
                        }
                        return(RedirectToAction("ListOrder", "Customer", new { id = userid }));
                    }
                }
            }
        }