/// <summary>
        /// Checks in a book.  Makes appropriate changes to the database and local memory collections
        /// </summary>
        /// <param name="log">CheckOut log to be used for the check in operations</param>
        /// <returns></returns>
        public bool CheckIn(CheckOutLog log)
        {
            if (!IsLibrarian) //if not a librarian
            {
                return(false);
            }

            try
            {
                //remove log from context and save changes
                context.CheckOutLogs.Remove(log);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    ex = ex.InnerException;
                }
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);

                return(false);
            }

            //remove log from memory collection
            checkoutLogs.Remove(log);
            return(true);
        }
        /// <summary>
        /// Check Out Book, make changes to the database and local collections
        /// </summary>
        /// <param name="book">Book to be checked out</param>
        /// <param name="cardHolder">MCardholder that is checking out the book</param>
        /// <param name="error">an error string to be outputted should a check out error occur</param>
        /// <returns></returns>
        public bool CheckOut(Book book, MCardholder cardHolder, out string error)
        {
            if (!IsLibrarian)  //if not a librarian
            {
                error = "You are not authorized to check out a book.";
                return(false);
            }

            if (cardHolder.HasOverdueBooks)
            {
                error = "User has Overdue books, cannot check out additional books.";
                return(false);
            }

            if (book.CopiesAvailable > 0)  //Make sure that there is a copy available
            {
                //Create a checkout log
                CheckOutLog log = new CheckOutLog
                {
                    Book                = book,
                    Book_BookId         = book.BookId,
                    CheckOutDate        = DateTime.Now,
                    Cardholder_PersonId = cardHolder.ID,
                    Cardholder          = cardHolder.Cardholder
                };


                try
                {
                    //Add log to context and save
                    context.CheckOutLogs.Add(log);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    while (ex != null)
                    {
                        ex = ex.InnerException;
                    }
                    DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                    OnDatabaseErrorRaised(e);

                    error = "Cannot check out book.  Please try again later.";
                    return(false);
                }

                //add log to memory collection
                checkoutLogs.Add(log);

                error = "";

                return(true);
            }

            error = string.Format("{0} could not be checked out, no copies available", book.Title);
            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks in the selected CheckOutLog
        /// </summary>
        private void LogCheckIn()
        {
            CheckOutLog log = CheckOutListView.SelectedItem as CheckOutLog;

            if (log != null)                 //ensure the log is not null
            {
                if (controller.CheckIn(log)) //if check in is successful
                {
                    SearchCheckOutLogs();    //repopulate the CheckOutListView with updated search query result
                }
            }
        }