public ActionResult Create([Bind(Include = "BorrowHistoryId,BookId,UserId,BorrowDate,ReturnDate")] BorrowHistory borrowHistory)
        {
            if (ModelState.IsValid)
            {
                string   name = User.Identity.Name;
                BooksDb2 db2  = new BooksDb2();
                var      xx   = from i in db2.Users
                                where i.Name == name
                                select i.UserId;


                int           userid = xx.ToList().FirstOrDefault();
                BorrowHistory bh     = new BorrowHistory();
                bh.UserId     = userid;
                bh.BookId     = borrowHistory.BookId;
                bh.BorrowDate = borrowHistory.BorrowDate;
                bh.ReturnDate = borrowHistory.ReturnDate;

                db.BorrowHistories.Add(bh);
                db.SaveChanges();
                //  db.BorrowHistories.Add(borrowHistory);
                // db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.BookId = new SelectList(db.Mybooks, "BookId", "BookId", borrowHistory.BookId);
            ViewBag.UserId = new SelectList(db.Users, "UserId", "UserId", borrowHistory.UserId);
            return(View(borrowHistory));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);



                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");


                    BooksDb2 db = new BooksDb2();

                    User u = new User();
                    u.Name = user.Email;

                    db.Users.Add(u);
                    db.SaveChanges();



                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        // GET: BorrowHistories/Create
        public ActionResult Create(int?id)
        {
            string   name = User.Identity.Name;//asp-net user table
            BooksDb2 db2  = new BooksDb2();
            var      xx   = from i in db2.Users
                            where i.Name == name
                            select i.UserId;


            int userid = xx.ToList().FirstOrDefault();

            ViewBag.userid1 = userid;
            Debug.WriteLine("userid is " + userid);
            ViewBag.BookId = new SelectList(db.Mybooks.Where(n => n.BookId == id), "BookId", "BookId");
            ViewBag.UserId = new SelectList(db.Users, "UserId", "UserId");
            //ViewBag.BookId = new SelectList(db.Mybooks.Where(n => n.BookId == id), "BookId", "BookId");

            (from p in db2.Mybooks
             where p.BookId == id
             select p).ToList().ForEach(x => x.qty = x.qty - 1);
            db2.SaveChanges();
            return(View());
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            //if (ModelState.IsValid)
            //{
            //    var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            //    var result = await UserManager.CreateAsync(user, model.Password);
            //    if (result.Succeeded)
            //    {
            //        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            //        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            //        // Send an email with this link
            //        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            //        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            //        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            //        BooksDb2 db = new BooksDb2();
            //        User u = new User();


            //         u.Name = model.Email;


            //        //db.Users.
            //        db.Users.Add(u);//Database user table
            //        //db.Users.Name = db.Users.Email;
            //        db.SaveChanges();


            //        //var userid = (from p in db.Users
            //        //              where p.Name == User.Identity.Name
            //        //              select p.UserId).ToString();
            //        //System.Diagnostics.Debug.WriteLine(userid);
            //        ApplicationDbContext context = new ApplicationDbContext();
            //        var userStore = new UserStore<ApplicationUser>(context);
            //        UserManager.AddToRole(user.Id,"User");

            //        return RedirectToAction("Index2", "Books");
            //    }
            //    AddErrors(result);

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    ApplicationDbContext context = new ApplicationDbContext();
                    var userStore   = new UserStore <ApplicationUser>(context);
                    var UserManager = new UserManager <ApplicationUser>(userStore);

                    User c = new User();
                    c.Name = model.Email;

                    BooksDb2 m1 = new BooksDb2();
                    m1.Users.Add(c);
                    m1.SaveChanges();
                    UserManager.AddToRole(user.Id, "User");
                    return(RedirectToAction("Login", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #5
0
        // GET: Books
        public ActionResult Index2(string bkname, string authname, string sortOrder, int?id)
        {
            //Session["userid"] = db.Users.Where(n=>n.UserId);
            ViewBag.Auth = isAdminUser();
            //Session["Userid"]= Convert.ToInt32(from p in db.Users
            //                    where p.Name == User.Identity.Name
            //                    select p.UserId);
            // var bh = db.Users.Where(x => x.Name == User.Identity.Name).Select(x=> x.UserId).ToString();
            //int bh2 = int.Parse(bh);
            //Session["Userid"] = bh2;
            //System.Diagnostics.Debug.WriteLine(Session["Userid"]);
            BooksDb2 Db2 = new BooksDb2();

            //(from p in Db2.Mybooks
            // where p.BookId == id
            // select p).ToList().ForEach(x => x.qty = x.qty + 1);

            //var userId = User.Identity.GetUserId();
            //Session["Userid"] = userId;
            //System.Diagnostics.Debug.WriteLine(Session["Userid"]);
            //BorrowHistory borrowHistory = db.BorrowHistories.Find(id);
            //db.BorrowHistories.Remove(borrowHistory);
            //db.SaveChanges();
            //return RedirectToAction("Index");
            Db2.SaveChanges();

            if (!ViewBag.Auth)//For user
            {
                var books = from b in db.Mybooks
                            where b.qty > 0
                            select b;


                if (!string.IsNullOrEmpty(bkname))
                {
                    books = books.Where(x => x.Title.Contains(bkname));
                }

                if (!string.IsNullOrEmpty(authname))
                {
                    books = books.Where(x => x.Author.Contains(authname));
                }

                ViewBag.TitleSortParam = sortOrder == "Title" ? "Title_desc" : "Title";
                ViewBag.AuthorParam    = sortOrder == "Author" ? "Author_desc" : "Author";
                switch (sortOrder)
                {
                case "Title_desc":
                    books = books.OrderByDescending(x => x.Title);
                    break;

                case "Title":
                    books = books.OrderBy(x => x.Title);
                    break;

                case "Author":
                    books = books.OrderBy(x => x.Author);
                    break;

                case "Author_desc":
                    books = books.OrderByDescending(x => x.Author);
                    break;
                }

                return(View(books));
            }
            else//For admin
            {
                var books = from b in db.Mybooks
                            select b;


                if (!string.IsNullOrEmpty(bkname))
                {
                    books = books.Where(x => x.Title.Contains(bkname));
                }

                if (!string.IsNullOrEmpty(authname))
                {
                    books = books.Where(x => x.Author.Contains(authname));
                }

                ViewBag.TitleSortParam = sortOrder == "Title" ? "Title_desc" : "Title";
                ViewBag.AuthorParam    = sortOrder == "Author" ? "Author_desc" : "Author";
                switch (sortOrder)
                {
                case "Title_desc":
                    books = books.OrderByDescending(x => x.Title);
                    break;

                case "Title":
                    books = books.OrderBy(x => x.Title);
                    break;

                case "Author":
                    books = books.OrderBy(x => x.Author);
                    break;

                case "Author_desc":
                    books = books.OrderByDescending(x => x.Author);
                    break;
                }

                return(View(books));
            }
        }