public ActionResult AddBorrow(FormCollection date)
        {
            if (MemeberCheck())
            {
                // return RedirectToAction("Login", "Account");
                return(View("Error"));
            }

            string userId = User.Identity.GetUserId().ToString();
            var    member = db.Members.Find(userId);

            if (member.CanBorrow)
            {
                var book = db.Books.Find(int.Parse(date["book_id"]));
                int numOfAvalibelBook = book.Copies - (book.NumOfbrrow + book.NumOfread);

                if (book.Aviable && numOfAvalibelBook > 1)
                {
                    var borrowBook = new BorrowMember()
                    {
                        BookId     = int.Parse(date["book_id"]),
                        MemberId   = userId,
                        Duration   = int.Parse(date["Duration"]),
                        DateBorrow = DateTime.Now,
                        ReturnBook = false,
                        returnDate = DateTime.Today
                    };
                    var BorrowMembersCheck = db.BorrowMembers.Where(mb => mb.BookId == borrowBook.BookId && mb.MemberId == borrowBook.MemberId).FirstOrDefault();

                    //  Session["id"] = 1;
                    //var member = db.Members.Where(m => m.User.Id == userId).FirstOrDefault();
                    //  var book = db.Books.Find(book_id);
                    // var member = db.Members.Find(userId);
                    //var mm = db.Members.ToList();
                    if (BorrowMembersCheck == null)
                    {
                        db.BorrowMembers.Add(borrowBook);
                    }
                    else if (BorrowMembersCheck.ReturnBook)
                    {
                        BorrowMembersCheck.Duration     = int.Parse(date["Duration"]);
                        BorrowMembersCheck.DateBorrow   = DateTime.Now;
                        BorrowMembersCheck.ReturnBook   = false;
                        BorrowMembersCheck.returnDate   = DateTime.Today;
                        BorrowMembersCheck.NumoFBorrow += 1;
                    }
                    else
                    {
                        return(View("Error"));

                        // the book is still with member
                    }
                    //var book = db.Books.Find(int.Parse(date["book_id"]));
                    book.NumOfbrrow   = book.NumOfbrrow + 1;
                    numOfAvalibelBook = book.Copies - (book.NumOfbrrow + book.NumOfread);

                    if (numOfAvalibelBook < 1)
                    {
                        book.Aviable = false;
                    }

                    db.SaveChanges();
                    //  return View(db.ReadMembers.Where(a => a.MemberId == member_id));

                    return(RedirectToAction("borrowdbooks"));
                }
                //book is not  Aviable
                else
                {
                    return(View("Error"));
                }
            }

            else
            {
                return(View("Error"));
            }
        }
Exemple #2
0
        public ActionResult addBorrowBook([Bind(Include = "Duration,MemberId,BookId")] BorrowMember item)
        {
            if (EmployeeCheck())
            {
                return(View("Error"));
            }
            if (ModelState.IsValid)
            {
                var book   = db.Books.Find(item.BookId);
                var member = db.Members.Find(item.MemberId);
                var check  = db.BorrowMembers.Where(mb => mb.BookId == item.BookId && mb.MemberId == item.MemberId).FirstOrDefault();
                if (member.CanBorrow)
                {
                    int numOfAvalibelBook = book.Copies - (book.NumOfbrrow + book.NumOfread);

                    if (book.Aviable && numOfAvalibelBook > 1)
                    {
                        if (check == null || check.ReturnBook)
                        {
                            //memeber naver borrow the book before
                            // or borrow but return
                            item.DateBorrow  = DateTime.Now;
                            item.returnDate  = DateTime.Now; //for ignoer error
                            item.ReturnBook  = false;
                            item.NumoFBorrow = 1;
                            book.NumOfbrrow += 1;
                            db.BorrowMembers.Add(item);

                            book.NumOfbrrow   = book.NumOfbrrow + 1;
                            numOfAvalibelBook = book.Copies - (book.NumOfbrrow + book.NumOfread);

                            if (numOfAvalibelBook < 1)
                            {
                                book.Aviable = false;
                            }
                            else
                            {
                                book.Aviable = true;
                            }

                            db.SaveChanges();

                            return(RedirectToAction("borrowdbooks"));
                            // return View();
                        }
                        else
                        {
                            ViewBag.error = "the member stil borrow the book ";
                            // return View();

                            //  return RedirectToAction("addBorrowBook");
                        } // if book in borrow
                    }     // book is avalive
                    else
                    {
                        ViewBag.error = "book not avalible";
                    }
                }//if memeber can borrow
                else
                {
                    ViewBag.error = "the member cant borrow now ";
                    //        return View();
                }
            }



            /*   else
             * {
             *
             *     ViewBag.MemberId = new SelectList(db.Members.Include(m => m.User), "MemeberId", "User.UserName", item.MemberId);
             *     ViewBag.BookId = new SelectList(db.Books, "id", "name", item.BookId);
             *     // return View();
             *
             * }
             */
            ViewBag.MemberId = new SelectList(db.Members.Include(m => m.User), "MemeberId", "User.UserName");
            ViewBag.BookId   = new SelectList(db.Books, "id", "name");
            return(View());
        }