public ActionResult MyBorrowedBooks()
        {
            try
            {
                using (Context ctx = new Context())
                {
                    // look at the GetCurrentUser method in Authentication class
                    // see the method signature

                    // notice the 'this' modifier on the method signature
                    // GetCurrentUser 'extends' the context class
                    // when Authetication class is in scope, then the
                    // GetCurrentUser method will 'appear' inside any instances
                    // dot into the ctx variable and look at the icon in front of
                    // Get Current User.  this is how an extention is identified
                    // notice that the 'this' parameter is actually infront of the dot
                    // used to invoke GetCurrentUser.  It is NOT inside the parenthesis
                    var borrower = ctx.GetCurrentUser();
                    if (null == borrower)
                    {
                        return(View("NotFound"));
                    }
                    var items = ctx.RatedBorrowingGetBooksRelatedToBorrowerCheckedOutOnly(borrower.BorrowerID);
                    return(View(VMBorrowing.ToList(items)));
                }
            }
            catch (Exception ex)
            {
                return(View("Exception", ex));
            }
            return(View());
        }
 public ActionResult AllBorrowedBooks()
 {
     try
     {
         using (Context ctx = new Context())
         {
             var items = ctx.RatedBorrowingsGetAllCheckedOut();
             return(View(VMBorrowing.ToList(items)));
         }
     }
     catch (Exception ex)
     {
         return(View("Exception", ex));
     }
 }
        public ActionResult EditBorrowing(VMBorrowing data)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    ctx.RatedBorrowingUpdateJust(data);

                    return(RedirectToAction("AllBorrowedBooks"));
                }
            }
            catch (Exception ex)
            {
                return(View("Exception", ex));
            }
        }
        public ActionResult ViewBooksBy(int id /* borrowerid */)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    string   BorrowerName = User.Identity.Name;
                    Borrower thisUser     = ctx.BorrowerSecuredFindByName(BorrowerName);
                    if (null == thisUser)
                    {
                        throw new Exception($"Borrower Not Found: '{BorrowerName}'");
                    }
                    if (!User.IsInRole("View:AllBorrowings"))
                    {
                        if (thisUser.BorrowerID == id)
                        {
                            if (User.IsInRole("View:MyBorrowings"))
                            {
                                // success
                            }
                            else
                            {
                                // failure
                                return(View("NotAllowed"));
                            }
                        }
                        else
                        {
                            // failure
                            return(View("NotAllowed"));
                        }
                    }

                    var rv = ctx.RatedBorrowingGetBooksRelatedToBorrower(id);
                    var vd = VMBorrowing.ToList(rv);
                    return(View("bookborrowing", vd));
                }
            }
            catch (Exception ex)
            {
                return(View("Exception", ex));
            }
        }
 public ActionResult ForceReturn(int id)
 {
     try
     {
         using (Context ctx = new Context())
         {
             var data = ctx.RatedBorrowingFindByID(id);
             if (null == data)
             {
                 return(View("NotFound"));
             }
             return(View(VMBorrowing.MakeNew(data)));
         }
     }
     catch (Exception ex)
     {
         return(View("Exception", ex));
     }
 }
 public ActionResult Borrow(VMBorrowing borrowing)
 {
     try
     {
         using (Context ctx = new Context())
         {
             if (!ModelState.IsValid)
             {
                 return(View(borrowing));
             }
             ctx.RatedBorrowingCreate(borrowing);
             return(RedirectToAction("Index"));
         }
     }
     catch (Exception ex)
     {
         return(View("Exception", ex));
     }
 }
        public ActionResult History(int id /* bookid*/)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    if (!User.IsInRole("View:AllUserBorrowing"))
                    {
                        // failure
                        return(View("NotAllowed"));
                    }

                    var rv = ctx.RatedBorrowingGetBorrowersRelatedToBook(id);
                    var vd = VMBorrowing.ToList(rv);
                    return(View("bookborrowing", vd));
                }
            }
            catch (Exception ex)
            {
                return(View("Exception", ex));
            }
        }
        public ActionResult Borrow(int id /* bookid */)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    string   BorrowerName = User.Identity.Name;
                    Borrower thisUser     = ctx.BorrowerSecuredFindByName(BorrowerName);
                    if (null == thisUser)
                    {
                        throw new Exception($"Borrower Not Found: '{BorrowerName}'");
                    }
                    Book thisBook = ctx.BookFindByID(id);
                    if (null == thisBook)
                    {
                        throw new Exception($"Book Not Found, id:'{id}'");
                    }
                    // see if the book is checked out already
                    var history    = ctx.RatedBorrowingGetBorrowersRelatedToBook(id);
                    var checkedout = (history.Where(b => b.isCheckedOut == true)).ToList();
                    if (checkedout.Count > 0)
                    {
                        return(View("CheckedOut", checkedout));
                    }

                    // this is the Junction to a Many-Many.
                    // This is duplicatable, and has extra data.
                    VMBorrowing Borrowing = new VMBorrowing(thisBook, thisUser);
                    Borrowing.DueDate = DateTime.Now.AddDays(21);

                    return(View(Borrowing));
                }
            }
            catch (Exception ex)
            {
                return(View("Exception", ex));
            }
        }