Exemple #1
0
        public ActionResult DatHang()
        {
            if (Session["TaiKhoan"] == null || Session["TaiKhoan"].ToString() == "")
            {
                return(RedirectToAction("dangNhap", "User"));
            }
            if (Session["GioHang"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            DonHang        ddh = new DonHang();
            KhachHang      kh  = (KhachHang)Session["TaiKhoan"];
            List <GioHang> gh  = LayGioHang();

            ddh.MaKH    = kh.MaKH;
            ddh.NgayDat = DateTime.Now;
            db.DonHangs.Add(ddh);
            db.SaveChanges();
            foreach (var item in gh)
            {
                ChiTietDonHang ctdh = new ChiTietDonHang();
                ctdh.MaDonHang = ddh.MaDonHang;
                ctdh.MaSach    = item.iMaSach;
                ctdh.SoLuong   = item.iSoLuong;
                ctdh.DonGia    = (decimal)item.dDonGia;
                db.ChiTietDonHangs.Add(ctdh);
            }
            db.SaveChanges();
            return(RedirectToAction("XacNhanDonHang", "GioHang"));
        }
Exemple #2
0
 public ActionResult ThemMoi(Sach sach, HttpPostedFileBase fileUpload)
 {
     ViewBag.MaChude = new SelectList(db.ChuDes.ToList(), "MaChuDe", "TenChuDe");
     ViewBag.MaNXB   = new SelectList(db.NhaXuatBans.ToList(), "MaNXB", "TenNXB");
     if (fileUpload == null)
     {
         ViewBag.ThongBao = "Chon hinh anh";
         return(View());
     }
     if (ModelState.IsValid)
     {
         //Luu ten file
         var fileName = Path.GetFileName(fileUpload.FileName);
         //Luu duong dan file
         var path = Path.Combine(Server.MapPath("~/HASP"), fileName);
         //Kiem tra hinh anh da ton tai chua
         if (System.IO.File.Exists(path))
         {
             ViewBag.ThongBao = "Hinh ảnh đã tồn tại";
         }
         else
         {
             fileUpload.SaveAs(path);
         }
         sach.AnhBia = fileUpload.FileName;
         db.Saches.Add(sach);
         db.SaveChanges();
     }
     return(View());
 }
Exemple #3
0
        public ActionResult ProductOrder(int Distance, int PaymentId, string ProductId, int Quantity, double UnitPrice, string PlaceOfDelivery)
        {
            if (ModelState.IsValid)
            {
                Order order = new Order();
                order.OrderId         = Convert.ToString(db.Orders.Count() + 1).PadLeft(8, '0');
                order.CustomerId      = Convert.ToString(Session["LoginUser"]);
                order.OrderDate       = DateTime.Now;
                order.PlaceOfDelivery = PlaceOfDelivery;
                order.Distance        = Distance;
                order.PaymentId       = PaymentId;
                order.OrderStatus     = "Waiting";
                db.Orders.Add(order);

                OrderDetail orderDetail = new OrderDetail();
                orderDetail.Order     = order;
                orderDetail.ProductId = ProductId;
                orderDetail.Quantity  = Quantity;
                orderDetail.UnitPrice = UnitPrice;
                db.OrderDetails.Add(orderDetail);

                db.SaveChanges();
                return(RedirectToAction("Order"));
            }
            return(View());
        }
Exemple #4
0
        // POST api/<controller>
        public Book Post([FromBody] Book book)
        {
            if (book == null)
            {
                throw new ArgumentNullException("book");
            }

            Book newBook = new Book();

            try
            {
                newBook.Name     = book.Name;
                newBook.Category = book.Category;
                newBook.Author   = book.Author;
                newBook.Price    = book.Price;
                _context.Books.Add(newBook);
                int rowsAffected = _context.SaveChanges();

                return(rowsAffected > 0 ? book : null);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemple #5
0
 public ActionResult Edit([Bind(Include = "CustomerID,FirstName,LastName,Username,Password,Gender,AccessLevel")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
Exemple #6
0
        public ActionResult Create([Bind(Include = "UserId,Email,UserName,UserPassword,PhoneNo,UserRole")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(user));
        }
        public ActionResult Create([Bind(Include = "MagazineID,MagazineTitle,IssuesPerYear,PricePerYear,Category,Circulation,PublishRate")] Magazine magazine)
        {
            if (ModelState.IsValid)
            {
                db.Magazines.Add(magazine);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(magazine));
        }
 public ActionResult Edit([Bind(Include = "ShipID,CustomerID,Street,City,State,Zip")] Shipping shipping)
 {
     if (ModelState.IsValid)
     {
         shipping.CustomerID      = db.Customers.Where(x => x.Username == User.Identity.Name).First().CustomerID;
         db.Entry(shipping).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("YourAccount", "Account", null));
     }
     return(View(shipping));
 }
Exemple #9
0
        public ActionResult Create([Bind(Include = "CategoryId,CategoryName")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
Exemple #10
0
        public ActionResult Create([Bind(Include = "ManufactureId,ManufactureName,ManufactureAddress,PhoneNo,Note")] Manufacture manufacture)
        {
            if (ModelState.IsValid)
            {
                db.Manufactures.Add(manufacture);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(manufacture));
        }
Exemple #11
0
        public ActionResult Create([Bind(Include = "CustomerId,CustomerName,CustomerAddress,PhoneNo,CustomerPassword,CustomerStatus")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(customer);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(customer));
        }
        public ActionResult Create([Bind(Include = "PaymentId,PaymentName")] Payment payment)
        {
            if (ModelState.IsValid)
            {
                db.Payments.Add(payment);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(payment));
        }
Exemple #13
0
        public ActionResult Create([Bind(Include = "id,name,publisher,publisherYear,price,author")] Books books)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(books);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(books));
        }
Exemple #14
0
        public ActionResult Create([Bind(Include = "AuthorID,FirstName,LastName,City,State,ZipCode,Country")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
        public ActionResult Create([Bind(Include = "GenreID,GenreName")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                db.Genres.Add(genre);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(genre));
        }
Exemple #16
0
        public ActionResult Create([Bind(Include = "PublisherID,Name,FirstName,LastName,Street,City,State,Zip,Phone,Email")] Publisher publisher)
        {
            if (ModelState.IsValid)
            {
                db.Publishers.Add(publisher);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(publisher));
        }
Exemple #17
0
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(user));
        }
Exemple #18
0
        public ActionResult Create([Bind(Include = "BookID,PublisherID,ISBN,Title,PublishYear,PageNum,Language,Edition,CoverType,Inventory,Cost")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.PublisherID = new SelectList(db.Publishers, "PublisherID", "Name", book.PublisherID);
            return(View(book));
        }
        public ActionResult Create([Bind(Include = "book_id,book_title,book_author,book_descreption,price,book_img,category_id")] Book_Inventory book_Inventory)
        {
            if (ModelState.IsValid)
            {
                db.Book_Inventory.Add(book_Inventory);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.category_id = new SelectList(db.categories, "category_id", "category_name", book_Inventory.category_id);
            return(View(book_Inventory));
        }
        public ActionResult Create([Bind(Include = "CartID,CustomerID,TotalPrice")] Shopping shopping)
        {
            if (ModelState.IsValid)
            {
                db.Shoppings.Add(shopping);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "FirstName", shopping.CustomerID);
            return(View(shopping));
        }
Exemple #21
0
        public ActionResult Create([Bind(Include = "BookID,StockCount,LoanLength,StockID")] Stock stock)
        {
            if (ModelState.IsValid)
            {
                db.Stocks.Add(stock);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.BookID = new SelectList(db.Books, "BookID", "BookName", stock.BookID);
            return(View(stock));
        }
        public ActionResult Create([Bind(Include = "id,name,gender,tel")] member member)
        {
            if (ModelState.IsValid)
            {
                db.member.Add(member);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.id = new SelectList(db.bookDetail, "id", "id", member.id);
            return(View(member));
        }
        public ActionResult Create([Bind(Include = "id,name,author,status")] book book)
        {
            if (ModelState.IsValid)
            {
                db.book.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.id = new SelectList(db.bookDetail, "id", "id", book.id);
            return(View(book));
        }
Exemple #24
0
        public ActionResult Create([Bind(Include = "id,member_id,book_id,date")] bookDetail bookDetail)
        {
            if (ModelState.IsValid)
            {
                db.bookDetail.Add(bookDetail);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.id = new SelectList(db.book, "id", "name", bookDetail.id);
            ViewBag.id = new SelectList(db.member, "id", "name", bookDetail.id);
            return(View(bookDetail));
        }
Exemple #25
0
        public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.GenreId  = new SelectList(db.Genres, "GenreId", "Name", book.GenreId);
            ViewBag.AuthorId = new SelectList(db.Author, "AuthorId", "Name", book.AuthorId);
            return(View(book));
        }
Exemple #26
0
        public ActionResult Create([Bind(Include = "OrderId,CustomerId,OrderDate,PlaceOfDelivery,Distance,PaymentId,DeliveryDate,OrderStatus")] Order order)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "CustomerName", order.CustomerId);
            ViewBag.PaymentId  = new SelectList(db.Payments, "PaymentId", "PaymentName", order.PaymentId);
            return(View(order));
        }
Exemple #27
0
 public ActionResult Edit([Bind(Include = "BillID,CustomerID,Street,City,State,Zip")] Billing billing)
 {
     if (ModelState.IsValid)
     {
         Customer customer = db.Customers.Where(x => x.Username == User.Identity.Name).First();
         billing.CustomerID      = customer.CustomerID;
         db.Entry(billing).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("YourAccount", "Account", null));
     }
     ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "FirstName", billing.CustomerID);
     return(View(billing));
 }
        public ActionResult Create([Bind(Include = "CategoryID,CategoryName,CategoryDescription,CategoryActive,CategoryCreateDateTime,CategoryUpdateDateTime")] Category category)
        {
            category.CategoryCreateDateTime = DateTime.Now;
            category.CategoryUpdateDateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
        public ActionResult Create([Bind(Include = "ProductImageID,ImageDescription,ImageURL,ProductID,ImageActive,ImageCreateDateTime,ImageUpdateTime")] ProductImage productImage)
        {
            productImage.ImageCreateDateTime = DateTime.Now;
            productImage.ImageUpdateDateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.ProductImages.Add(productImage);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", productImage.ProductID);
            return(View(productImage));
        }
Exemple #30
0
        public ActionResult Create([Bind(Include = "ProductID,ProductName,ProductDescription,ProductModel,ProductPrice,ProductKeywords,CategoryID,ProductActive,ProductCreateDateTime,ProductUpdateDateTime")] Product product)
        {
            product.ProductCreateDateTime = DateTime.Now;
            product.ProductUpdateDateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
            return(View(product));
        }
        public ActionResult BookDetails(FormCollection formCollection, string wishlist, string addtocart, string continuetoshopping, string removewishlist)
        {
            var ID = formCollection["txtID"];
            var QtyInStore = formCollection["txtQtyInStore"];
            string userSettings="0";
            if (Request.Cookies["UserSettings"] != null)
            {

                if (Request.Cookies["UserSettings"]["UserID"] != null)
                { userSettings = Request.Cookies["UserSettings"]["UserID"]; }
            }

            if (wishlist != null)
            {
                using (BookStoreEntities book = new BookStoreEntities())
                {
                    var alreadyexisting = book.Carts.Where(b => b.bid.ToString() == ID && b.cid.ToString() == userSettings).ToList();
                    if(alreadyexisting.Count() == 0)
                    {
                        var _bookCart = book.books.Where(b => b.bid.ToString() == ID).ToList();
                        foreach (var b in _bookCart)
                        {
                            //Add customerid as well
                            book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = b.bid, title = b.title, author = b.author, edition = b.edition, genre = b.genre, price = b.price, publisher = b.publisher, inWishList = true, orderStatus = "WishList", year = b.year, qtyinstore = b.qtyinstore });
                            book.SaveChanges();
                        }
                    }

                }
                return RedirectToAction("Index", "Home");
            }
            if (addtocart != null)
            {

                using (BookStoreEntities book = new BookStoreEntities())
                {
                    var alreadyexisting = book.Carts.SingleOrDefault(b => b.bid.ToString() == ID && b.cid.ToString() == userSettings);
                    if (alreadyexisting == null)
                    {
                        var _bookCart = book.books.Where(b => b.bid.ToString() == ID).ToList();
                        foreach (var b in _bookCart)
                        {
                            //Add customerid as well
                            book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = b.bid, title = b.title, author = b.author, edition = b.edition, genre = b.genre, price = b.price, publisher = b.publisher, inWishList = false, orderStatus = "Placed", year = b.year, qtyinstore = b.qtyinstore });
                            book.SaveChanges();
                        }
                    }
                    else
                    {
                        if (alreadyexisting.inWishList == true)
                        {
                            var cid = Convert.ToInt32(userSettings);
                            var bid = Convert.ToInt32(ID);
                            var _bookCart = book.books.Where(b => b.bid.ToString() == ID).ToList();

                            var bookToRemove = book.Carts.Where(b => b.bid.ToString() == ID && b.cid.ToString() == userSettings);
                            book.Carts.RemoveRange(bookToRemove);
                            book.SaveChanges();

                            foreach (var b in _bookCart)
                            {
                                book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = b.bid, title = b.title, author = b.author, edition = b.edition, genre = b.genre, price = b.price, publisher = b.publisher, inWishList = false, orderStatus = "Placed", year = b.year, qtyinstore = b.qtyinstore });
                                book.SaveChanges();
                            }
                        }
                    }

                }
                return RedirectToAction("Index", "Home");
            }
            if(removewishlist != null)
            {
                using (BookStoreEntities book = new BookStoreEntities())
                {
                    var bookToRemove = book.Carts.Where(b => b.bid.ToString() == ID && b.cid.ToString() == userSettings);
                    book.Carts.RemoveRange(bookToRemove);
                    book.SaveChanges();
                }
                return RedirectToAction("WishList", "Home");
            }
            if (continuetoshopping != null)
            {
            }
               return RedirectToAction("Index", "Home");
        }
        public ActionResult Cart(FormCollection formCollection)
        {
            int n = 0;
            var Name = formCollection["Name"];
            var ShippingAddress = formCollection["ShippingAddress"];
            var BillingAddress = formCollection["BillingAddress"];
            var Card = formCollection["Card"];
            var CVV = formCollection["CVV"];
            long cardInt;
            bool isCardNumerical =  Int64.TryParse(Card, out cardInt);
            //string error = "";
            int cvvInt;
            bool isCVVNumerical = Int32.TryParse(CVV, out cvvInt);
            var error = new StringBuilder();

            if (string.IsNullOrEmpty(Name))
            {
                error.AppendLine("Please enter a Name");
                error.AppendLine();
                //error = error + "Please enter a Name" + "\n" ;
            }
            if (string.IsNullOrEmpty(ShippingAddress))
            {
                //error = error  + "Please enter Shipping Address" + "\n";
                error.AppendLine("Please enter Shipping Address");
                error.AppendLine();
            }
            if (string.IsNullOrEmpty(BillingAddress))
            {
                //error = error + "Please enter Billing Address" + "\n";
                error.Append("Please enter Billing Address");
                error.AppendLine();
            }
            if (string.IsNullOrEmpty(Card))
            {
                //error = error + "Please enter Credit Card Number" + "\n";
                error.Append("Please enter Credit Card Number");
                error.AppendLine();
            }
            if (string.IsNullOrEmpty(CVV))
            {
                //error = error + "Please enter CVV Number" + "\n";
                error.Append("Please enter CVV Number");
                error.AppendLine();
            }
            if (!isCardNumerical)
            {
                //error = error + "Please enter valid Credit Card Number" + "\n";
                error.Append("Please enter valid Credit Card Number");
                error.AppendLine();
            }
            if (!isCVVNumerical)
            {
                //error = error + "Please enter valid CVV Number" + "\n";
                error.Append("Please enter valid CVV Number");
                error.AppendLine();
            }
            error = error.Replace("\n", Environment.NewLine);
            ViewBag.SummaryDiv = error.ToString();
            List<_671Books.Models.Book> _books = new List<_671Books.Models.Book>();
            if (error.Length == 0)
            {
                List<string> ID = new List<string>();
                List<string> a = new List<string>();
                int count = 0;
                string value = "";
                decimal total = 0;
                //string[] a = new string[100];
                string userSettings = "0";
                if (Request.Cookies["UserSettings"] != null)
                {
                    if (Request.Cookies["UserSettings"]["UserID"] != null)
                    { userSettings = Request.Cookies["UserSettings"]["UserID"]; }
                }
                using (BookStoreEntities book = new BookStoreEntities())
                {
                    foreach (var key in formCollection.AllKeys)
                    {
                        if (key != "Name" && key != "ShippingAddress" && key != "BillingAddress" && key != "Card" && key != "MM" && key != "YY" && key != "CVV" && key != "placeOrder")
                        {
                            if (key.Contains("txt"))
                            {
                                value = formCollection["txt"];
                                //a = value.Split(',');

                            }
                            else
                            {
                                var v = key;
                                ID.Add(v);
                            }
                        }

                        //var value = formCollection[key];
                        //var ID = key.Substring(2, key.Length - 3);
                        //if (key.Contains("txt"))
                        //{
                        //    var value = formCollection[key];
                        //    var ID = key.Substring(3, key.Length - 3);
                        //
                    }
                    Random r = new Random();
                    n = r.Next(1000001, 9999999);

                    string[] b = value.Split(',');
                    for (int j = 0; j < b.Count(); j++)
                    {
                        a.Add(b[j]);
                    }
                    var bookToRemove = book.Carts.Where(_book => _book.cid.ToString() == userSettings && _book.orderStatus == "Placed");
                    book.Carts.RemoveRange(bookToRemove);
                    book.SaveChanges();

                    decimal qty = 0;
                    for (int i = 0; i < a.Count(); i++)
                    {
                        var id = ID[i];
                        var originalRecord = book.books.FirstOrDefault(x => x.bid.ToString() == id);
                        if (originalRecord != null)
                        {
                            var num = Convert.ToDecimal(a[i]);
                            total = total + num;
                            var den = Convert.ToDecimal(originalRecord.price);
                            qty = num / den;
                            //book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = originalRecord.bid, price = Convert.ToDecimal(a[i]), title = originalRecord.title, qty =  (int)qty, edition = originalRecord.edition, year = originalRecord.year, author = originalRecord.author, publisher = originalRecord.publisher, genre = originalRecord.genre,inWishList = false, orderStatus = "Ordered", qtyinstore = originalRecord.qtyinstore});
                            //book.SaveChanges();
                            book.Orders.Add(new Order { cid = Convert.ToInt32(userSettings), bid=originalRecord.bid, orderdate = DateTime.Now, shippingaddress = ShippingAddress, Status = "Ordered", totalprice = num, orderid = n, qtysold = Convert.ToInt32(qty) });
                            book.SaveChanges();
                        }
                        // book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = Convert.ToInt32(ID[i]), });
                    }
                    //uncomment following if the above logic fails
                    //book.Orders.Add(new Order { cid = Convert.ToInt32(userSettings), orderdate = DateTime.Now, shippingaddress = ShippingAddress, Status = "Ordered", totalprice = total, orderid = n, qtysold = Convert.ToInt32(qty) });
                    //book.SaveChanges();
                }
                return RedirectToAction("Confirmation", "Home", new {orderNumber = n});
            }
            else {
                string userSettings = "0";
                if (Request.Cookies["UserSettings"] != null)
                {
                    if (Request.Cookies["UserSettings"]["UserID"] != null)
                    { userSettings = Request.Cookies["UserSettings"]["UserID"]; }
                }
                using (BookStoreEntities _book = new BookStoreEntities())
                {
                    var userBooks = _book.Carts.Where(b => b.cid.ToString() == userSettings && b.inWishList == false && b.orderStatus == "Placed").ToList();
                    foreach (var b in userBooks)
                    {
                        _671Books.Models.Book book = new _671Books.Models.Book();
                        book.Author = b.author;
                        //book.BookCover
                        book.Edition = b.edition;
                        book.Genre = b.genre;
                        book.ID = b.bid;
                        //book.ISBN = b.i
                        book.Name = b.title;
                        book.Price = b.price;
                        book.Publisher = b.publisher;
                        book.QuantityInStore = b.qtyinstore;
                        book.Year = b.year;
                        _books.Add(book);
                    }

                }
                return View(_books);
            }
        }
        public ActionResult Checkout(FormCollection formCollection)
        {
            List<string> ID = new List<string>();
            List<string> a = new List<string>();
            int count = 0;
            string value = "";
            decimal total = 0;
            //string[] a = new string[100];
            string userSettings = "0";
            if (Request.Cookies["UserSettings"] != null)
            {
                if (Request.Cookies["UserSettings"]["UserID"] != null)
                { userSettings = Request.Cookies["UserSettings"]["UserID"]; }
            }
            using(BookStoreEntities book = new BookStoreEntities())
            {
                foreach (var key in formCollection.AllKeys)
                {

                    if (key.Contains("txt"))
                    {
                         value = formCollection["txt"];
                        //a = value.Split(',');

                    }
                    else
                    {
                        var v = key;
                        ID.Add(v);
                    }

                    //var value = formCollection[key];
                    //var ID = key.Substring(2, key.Length - 3);
                    //if (key.Contains("txt"))
                    //{
                    //    var value = formCollection[key];
                    //    var ID = key.Substring(3, key.Length - 3);
                    //
                }
                 Random r = new Random();
                int n = r.Next(1000001, 9999999);

                string[] b = value.Split(',');
                for (int j = 0; j < b.Count(); j++)
                {
                    a.Add(b[j]);
                }
                var bookToRemove = book.Carts.Where(_book => _book.cid.ToString() == userSettings && _book.orderStatus == "Placed");
                book.Carts.RemoveRange(bookToRemove);
                book.SaveChanges();

                    for (int i = 0; i < a.Count(); i++)
                    {
                        var id = ID[i];
                        var originalRecord = book.books.FirstOrDefault(x => x.bid.ToString() ==  id);
                        if (originalRecord != null)
                        {
                            var num = Convert.ToDecimal(a[i]);
                            total = total + num;
                            var den = Convert.ToDecimal(originalRecord.price);
                            var qty = num / den;
                            //book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = originalRecord.bid, price = Convert.ToDecimal(a[i]), title = originalRecord.title, qty =  (int)qty, edition = originalRecord.edition, year = originalRecord.year, author = originalRecord.author, publisher = originalRecord.publisher, genre = originalRecord.genre,inWishList = false, orderStatus = "Ordered", qtyinstore = originalRecord.qtyinstore});
                            //book.SaveChanges();
                        }
                        // book.Carts.Add(new Cart { cid = Convert.ToInt32(userSettings), bid = Convert.ToInt32(ID[i]), });
                    }
                    book.Orders.Add(new Order { cid = Convert.ToInt32(userSettings), orderdate = DateTime.Now, shippingaddress = "123 Shipping Address", Status = "Ordered", totalprice = total, orderid = n });
                    book.SaveChanges();
            }

            return View();
        }
        public ActionResult NewBook(FormCollection formCollection)
        {
            //var ID = Convert.ToInt32(formCollection["ID"]);

            var TitleBook = formCollection["TitleBook"];
            var Edition = formCollection["Edition"];
            var Year = formCollection["Year"];
            var Price = formCollection["Price"];
            var Author = formCollection["Author"];
            var Publisher = formCollection["Publisher"];
            var Genre = formCollection["Genre"];
            var QtyInStore = formCollection["QtyInStore"];
            decimal outPrice = 0;
            bool isPriceDecimal = Decimal.TryParse(Price, out outPrice);
            int outYear = 0;
            bool isYearNumerical = Int32.TryParse(Year, out outYear);
            int outQtyInStore = 0;
            bool isQtyInStoreNumerical = Int32.TryParse(QtyInStore, out outQtyInStore);
            int outEdition = 0;
            bool isEditionNumerical = Int32.TryParse(Edition, out outEdition);
            bool flag = true;
            bool numflag = true;
            string error = "";
            if (string.IsNullOrEmpty(TitleBook))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Edition))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Year))
            {
                flag = false;
            }
            if(string.IsNullOrEmpty(Author))
            {
                flag = false;
            }
             if(string.IsNullOrEmpty(Publisher))
            {
                flag = false;
            }
             if(string.IsNullOrEmpty(Genre))
            {
                flag = false;
            }
            if(string.IsNullOrEmpty(Price))
            {
                flag = false;
            }
             if(string.IsNullOrEmpty(QtyInStore))
            {
                flag = false;
            }
             if (!isPriceDecimal)
             {
                 numflag = false;
             }
             if (!isYearNumerical)
             {
                 numflag = false;
             }
             if (!isEditionNumerical)
             {
                 numflag = false;
             }
             if (!isQtyInStoreNumerical)
             {
                 numflag = false;
             }
             if (flag)
             {
                 if (numflag)
                 {
                     using (BookStoreEntities book = new BookStoreEntities())
                     {
                         var ID = book.books.Max(b => b.bid);
                         book.books.Add(new book { bid = ID + 1, title = TitleBook, edition = outEdition, author = Author, genre = Genre, price = outPrice, publisher = Publisher, year = outYear, qtyinstore = outQtyInStore});
                         book.SaveChanges();
                     }
                 }
                 else
                 {
                     ViewBag.SummaryDiv = "Please enter valid values";
                     return View();
                 }
             }
             else
             {
                 ViewBag.SummaryDiv = "Please enter all values";
                 return View();
             }
             return RedirectToAction("Admin", "Home");
        }
        public ActionResult RemoveCart(string ID)
        {
            string userSettings = "0";
            if (Request.Cookies["UserSettings"] != null)
            {

                if (Request.Cookies["UserSettings"]["UserID"] != null)
                { userSettings = Request.Cookies["UserSettings"]["UserID"]; }
            }
            using (BookStoreEntities book = new BookStoreEntities())
            {
                var bookToRemove = book.Carts.Where(b => b.bid.ToString() == ID && b.cid.ToString() == userSettings);
                book.Carts.RemoveRange(bookToRemove);
                book.SaveChanges();
            }
            return RedirectToAction("Cart", "Home");
        }
        public ActionResult Register(FormCollection formCollection)
        {
            // Attempt to register the user
               using(BookStoreEntities book = new BookStoreEntities())
               {
                    var username = formCollection["UserName"];
                    var password = formCollection["Password"];
                    var phone = formCollection["Phone"];
                    var name = formCollection["Name"];
                    var address = formCollection["Address"];
                   var person = book.people.Where(x => x.username == username);
                   if(person == null)
                   {
                       book.people.Add(new person { username = username, password = password, phone = Convert.ToInt64(phone), name = name, address= address });
                       book.SaveChanges();
                   }

                    return RedirectToAction("Index", "Home");

               }

                // If we got this far, something failed, redisplay form
            return View();
        }
        public ActionResult Register(FormCollection formCollection)
        {
            // Attempt to register the user
            try
            {
                using (BookStoreEntities book = new BookStoreEntities())
                {
                    string error = "";
                    var username = formCollection["UserName"];
                    var password = formCollection["Password"];
                    var phone = formCollection["Phone"];
                    var name = formCollection["Name"];
                    var address = formCollection["Address"];
                    var confirmpassword = formCollection["ConfirmPassword"];
                    if (string.IsNullOrEmpty(username))
                    {
                        error = error + "Please enter Username." + "<br>";
                    }
                    if (string.IsNullOrEmpty(password))
                    {
                        error = error + "Please enter Password." + "<br>";
                    }
                    if (string.Compare(password, confirmpassword) != 0)
                    {
                        error = error + "Password's did not match." + "<br>";
                    }
                    if (string.IsNullOrEmpty(phone))
                    {
                        error = error + "Please enter Phone." + "<br>";
                    }
                    if (string.IsNullOrEmpty(address))
                    {
                        error = error + "Please enter Address." + "<br>";
                    }
                    if (string.IsNullOrEmpty(address))
                    {
                        error = error + "Please enter Address." + "<br>";
                    }
                    if (string.IsNullOrEmpty(error))
                    {
                        var person = book.people.Where(x => x.username.ToLower() == username.ToLower()).ToList();
                        if (person.Count == 0)
                        {
                            var id = book.people.Max(b => b.cid);
                            book.people.Add(new person {cid = id+1, username = username, password = password, phone = Convert.ToInt64(phone), name = name, address = address, admin_status = "no" });
                            book.SaveChanges();
                        }
                        else
                        {
                            ViewBag.ExistingUser = "******";
                            return View();
                        }
                        return RedirectToAction("Login", "User");
                    }
                    else
                    {
                        ViewBag.SummaryDiv = error;
                        return View();
                    }

                }
            }
            catch (Exception ex)
            {

                //return to error page
            }

            // If we got this far, something failed, redisplay form
            return View();
        }
        public ActionResult BookDetailsAdmin(FormCollection formCollection)
        {
            var ID = Convert.ToInt32(formCollection["txtID"]);

            var TitleBook = formCollection["TitleBook"];
            var Edition = formCollection["Edition"];
            var Year = formCollection["Year"];
            var Price = formCollection["Price"];
            var Author = formCollection["Author"];
            var Publisher = formCollection["Publisher"];
            var Genre = formCollection["Genre"];
            var QtyInStore = formCollection["QtyInStore"];
            decimal outPrice = 0;
            bool isPriceDecimal = Decimal.TryParse(Price, out outPrice);
            int outYear = 0;
            bool isYearNumerical = Int32.TryParse(Year, out outYear);
            int outQtyInStore = 0;
            bool isQtyInStoreNumerical = Int32.TryParse(QtyInStore, out outQtyInStore);
            int outEdition = 0;
            bool isEditionNumerical = Int32.TryParse(Edition, out outEdition);
            bool flag = true;
            bool numflag = true;
            string error = "";
            if (string.IsNullOrEmpty(TitleBook))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Edition))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Year))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Author))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Publisher))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Genre))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(Price))
            {
                flag = false;
            }
            if (string.IsNullOrEmpty(QtyInStore))
            {
                flag = false;
            }
            if (!isPriceDecimal)
            {
                numflag = false;
            }
            if (!isYearNumerical)
            {
                numflag = false;
            }
            if (!isEditionNumerical)
            {
                numflag = false;
            }
            if (!isQtyInStoreNumerical)
            {
                numflag = false;
            }
            if (flag)
            {
                if (numflag)
                {
                    using (BookStoreEntities book = new BookStoreEntities())
                    {
                        //var bookToRemove = book.books.Where(b => b.bid == ID);
                        //book.books.RemoveRange(bookToRemove);
                        //book.SaveChanges();

                        //book.books.Add(new book { bid = ID, title = TitleBook, edition = Edition, author = Author, genre = Genre, price = Price, publisher = Publisher, year = Year, qtyinstore = QtyInStore, qtysold = 0 });
                        //book.SaveChanges();
                        var bookToFind = book.books.Find(ID);
                        bookToFind.author = Author;
                        bookToFind.edition = outEdition;
                        bookToFind.year = outYear;
                        bookToFind.publisher = Publisher;
                        bookToFind.genre = Genre;
                        bookToFind.price = outPrice;
                        bookToFind.qtyinstore = outQtyInStore;
                        bookToFind.title = TitleBook;
                        book.SaveChanges();
                    }
                }
                else
                {
                    ViewBag.SummaryDiv = "Please enter valid values";
                    _671Books.Models.Book _book = new _671Books.Models.Book();
                    BookStoreEntities book = new BookStoreEntities();
                    var books = book.books.ToList();
                    books = books.Where(b => b.bid == ID).ToList();

                    foreach (var b in books)
                    {
                        _book.ID = b.bid;
                        _book.Name = b.title;
                        _book.Author = b.author;
                        _book.Genre = b.genre;
                        _book.Year = b.year;
                        _book.Edition = b.edition;
                        _book.Publisher = b.publisher;
                        _book.Price = b.price;
                        _book.QuantityInStore = b.qtyinstore;
                        //_book.QuantitySold = b.qtysold;
                        // _book.BookCover = b.BookCover;
                    }
                    return View(_book);
                }
            }
            else
            {
                ViewBag.SummaryDiv = "Please enter all values";
                _671Books.Models.Book _book = new _671Books.Models.Book();
                BookStoreEntities book = new BookStoreEntities();
                var books = book.books.ToList();
                books = books.Where(b => b.bid == ID).ToList();

                foreach (var b in books)
                {
                    _book.ID = b.bid;
                    _book.Name = b.title;
                    _book.Author = b.author;
                    _book.Genre = b.genre;
                    _book.Year = b.year;
                    _book.Edition = b.edition;
                    _book.Publisher = b.publisher;
                    _book.Price = b.price;
                    _book.QuantityInStore = b.qtyinstore;
                    //_book.QuantitySold = b.qtysold;
                    // _book.BookCover = b.BookCover;
                }
                return View(_book);
            }

            return RedirectToAction("Admin", "Home");
        }