public int CreateOrderDetail(OrderDetail orderDetail)
        {
            List <OrderDetail> orderDetails = _context.OrderDetails.ToList();

            OrderDetail FindOrderDetail = orderDetails.Find(o =>
                                                            o.OrderId == orderDetail.OrderId &&
                                                            o.ProductId == orderDetail.ProductId);
            Product product = _context.Products.FirstOrDefault(p => p.Id == orderDetail.ProductId);

            if (orderDetails.Contains(FindOrderDetail))
            {
                FindOrderDetail.Quantity += orderDetail.Quantity;
                FindOrderDetail.Total    += Bill(product.Price, orderDetail.Quantity, orderDetail.Discount);

                _context.Update(FindOrderDetail);
            }
            else
            {
                orderDetail.Total = Bill(product.Price, orderDetail.Quantity, orderDetail.Discount);

                _context.Add(orderDetail);
            }

            return(_context.SaveChanges());
        }
Ejemplo n.º 2
0
        public ActionResult AddCart()
        {
            List <CartItem> carts = Session["giohang"] as List <CartItem>;

            if (carts == null)
            {
                return(RedirectToAction("GioHangIndex", "Home"));
            }
            if (Session["user"] as string == null)
            {
                return(RedirectToAction("Login", "Account"));
            }
            string      i  = Session["user"].ToString();
            var         id = db.Customers.FirstOrDefault(x => x.Usernames == i);
            List <Cart> l  = new List <Cart>();

            foreach (var item in carts)
            {
                Cart c = new Cart
                {
                    TotalMoney = item.TotalMoney,
                    Status     = false,
                    CustomerID = id.CustomerID,
                    DateCreate = DateTime.Now,
                    ProductID  = item.ProductID,
                    Quantity   = item.Quantity
                };
                l.Add(c);
            }
            db.Carts.AddRange(l);
            db.SaveChanges();
            carts.Clear();
            Session["message"] = "Thành công";
            return(RedirectToAction("GioHangIndex", "Home"));
        }
Ejemplo n.º 3
0
        public ActionResult Create(Product product, HttpPostedFileBase img1, HttpPostedFileBase img2, HttpPostedFileBase img3, HttpPostedFileBase img4)
        {
            //Thêm hình ảnh vào CSDL
            if (ModelState.IsValid)
            {
                string path1, path2, path3, path4;
                if (img1 == null)
                {
                    path1 = null;
                }
                else
                {
                    path1 = Server.MapPath("~/Imgs/" + Path.GetFileName(img1.FileName));
                    img1.SaveAs(path1);
                    product.Images1 = @"/Imgs/" + img1.FileName;
                }
                if (img2 == null)
                {
                    path2 = null;
                }
                else
                {
                    path2 = Server.MapPath("~/Imgs/" + Path.GetFileName(img2.FileName));
                    img2.SaveAs(path2);
                    product.Images2 = @"/Imgs/" + img2.FileName;
                }
                if (img3 == null)
                {
                    path3 = null;
                }
                else
                {
                    path3 = Server.MapPath("~/Imgs/" + Path.GetFileName(img3.FileName));
                    img3.SaveAs(path3);
                    product.Images3 = @"/Imgs/" + img3.FileName;
                }
                if (img4 == null)
                {
                    path4 = null;
                }
                else
                {
                    path4 = Server.MapPath("~/Imgs/" + Path.GetFileName(img4.FileName));
                    img4.SaveAs(path4);
                    product.Images4 = @"/Imgs/" + img4.FileName;
                }

                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            //Đưa dữ liệu vào dropdownlist
            ViewBag.ProductTypeID = new SelectList(db.ProductTypes, "ProductTypeID", "Name", product.ProductTypeID);
            ViewBag.TradeID       = new SelectList(db.Trades, "TradeID", "Name", product.TradeID);
            return(View(product));
        }
Ejemplo n.º 4
0
        public ActionResult Create([Bind(Include = "TradeID,Name")] Trade trade)
        {
            if (ModelState.IsValid)
            {
                db.Trades.Add(trade);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(trade));
        }
        public ActionResult Create([Bind(Include = "ProductTypeID,Name")] ProductType productType)
        {
            if (ModelState.IsValid)
            {
                db.ProductTypes.Add(productType);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(productType));
        }
Ejemplo n.º 6
0
        public ActionResult Create([Bind(Include = "CustomerID,Name,Address,PhoneNumbers,Usernames,Password,HashPassword,AccessManagement")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                customer.HashPassword = CreateMD5(customer.Password);
                db.Customers.Add(customer);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(customer));
        }
Ejemplo n.º 7
0
        public ActionResult SignUp(FormCollection f)
        {
            string username = f["email"].ToString();
            var    password = f["password"].ToString();

            var us = db.Customers.FirstOrDefault(x => x.Usernames == username);

            if (us != null)
            {
                ViewBag.message = "Tài khoản đã tồn tại !";
                return(RedirectToAction("SignUp"));
            }
            var      pass = CreateMD5(password);
            Customer c    = new Customer
            {
                Usernames    = f["email"].ToString(),
                Address      = f["address"].ToString(),
                Password     = f["password"].ToString(),
                Name         = f["name"].ToString(),
                PhoneNumbers = f["phone"].ToString(),
                HashPassword = pass
            };

            db.Customers.Add(c);
            var result = db.SaveChanges();

            if (result > 0)
            {
                return(RedirectToAction("ListProduct", "Home"));
            }
            else
            {
                ViewBag.message = "Fail";
                return(RedirectToAction("SignUp"));
            }
        }
 private void SeedData(WatchShopDbContext context)
 {
     context.AddRange(GetDummyData());
     context.SaveChanges();
 }
Ejemplo n.º 9
0
 public int Delete(int id)
 {
     _context.Remove(GetProduct(id));
     return(_context.SaveChanges());
 }
 public int CreateCategory(Category category)
 {
     _context.Add(category);
     return(_context.SaveChanges());
 }
 public int CreateOrder(Order order)
 {
     _context.Add(order);
     return(_context.SaveChanges());
 }