/// <summary>
        /// Adds a new Book to database context and to local memory collection.
        /// Raises <event>OnDatabaseError</event> when exception is thrown from changing the database context.
        /// </summary>
        /// <param name="book">Book object to be added</param>
        /// <returns>Returns true if operation is successful, false otherwise</returns>
        public bool AddBook(Book book)
        {
            if (!IsLibrarian)
            {
                return(false);               //if not a librarian return false
            }
            try
            {
                //add book to database context
                context.Books.Add(book);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                //send OnDatabaseError event
                while (ex != null)
                {
                    ex = ex.InnerException;
                }
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);
                return(false);
            }

            bookInventory.Add(book);

            return(true);
        }
        /// <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);
        }
        //Returns the newly created Author object when opration is successful, returns null otherwise.
        /// <summary>
        /// Creates a new Author and MAuthor object with the properties set to the arguments passed.
        /// Author and MAuthor are added to the database context and local collection respectively.
        /// Raises <event>OnDatabaseError</event> when exception is caught from attempting to change database context.
        /// </summary>
        /// <param name="firstName">First name of author</param>
        /// <param name="lastName">Last name of author</param>
        /// <param name="bio">Bio of author</param>
        /// <returns>Returns true if operation is successful, false otherwise</returns>
        public Author CreateAuthor(string firstName, string lastName, string bio)
        {
            try
            {
                //create person
                Person addPerson = new Person();
                addPerson.FirstName = firstName;
                addPerson.LastName  = lastName;

                //add to database to get personID
                context.People.Add(addPerson);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                //Rase OnDatabaseError event
                while (ex != null)
                {
                    ex = ex.InnerException;
                }
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);
                return(null);
            }

            try
            {
                //retrieve Person from database with assigned ID
                Author result    = new Author();
                Person addPerson = (from p in context.People
                                    where p.FirstName == firstName && p.LastName == lastName
                                    select p).First();

                result.Person   = addPerson;
                result.PersonId = addPerson.PersonId;
                result.Bio      = bio;

                //Add author to people list
                MAuthor ma = new MAuthor(result);
                peopleList.Add(ma);

                //Add author to database
                context.Authors.Add(result);
                context.SaveChanges();

                return(result);
            }
            catch (Exception ex)
            {
                //Raise OnDatabaseError event
                while (ex != null)
                {
                    ex = ex.InnerException;
                }
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);
                return(null);
            }
        }
        /// <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);
        }
        /// <summary>
        /// Broadcasts OnDatabaseError events, takes a <c>DatabaseEventArgs</c> as argument.
        /// </summary>
        /// <param name="e"></param>
        private void OnDatabaseErrorRaised(DatabaseEventArgs e)
        {
            DatabaseErrorHandler handler = OnDatabaseError;

            if (handler != null)  //make sure that there are subscribers to the event
            {
                handler(this, e);
            }
        }
 /// <summary>
 /// Saves changes to database context.
 /// Raises <event>OnDatabaseError</event> when exception occurs
 /// </summary>
 public void Save()
 {
     try
     {
         context.SaveChanges();
     }
     catch (Exception ex)
     {
         while (ex != null)
         {
             ex = ex.InnerException;
         }
         DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
         OnDatabaseErrorRaised(e);
     }
 }
        public event DatabaseErrorHandler OnDatabaseError; //event to broadcast if a database error has occurred

        #endregion

        /// <summary>
        /// Constructor.  Opens a connection to the database using Entity Framework
        /// and loads all database entities into local collections and classes.
        /// </summary>
        public LibraryClientController()
        {
            context = new LibraryDB();

            try
            {
                //If the context collections are null or contains no elements, load from XML using XMLSaverLoader
                //if (context.Books == null || context.Books.Count() <= 0 || context.People == null || context.People.Count() == 0)
                //{
                //    XMLSaverLoader xmlSaverLoader = new XMLSaverLoader();
                //    xmlSaverLoader.LoadFromLocalXML();
                //    Message = "Database Loaded";
                //}
                //else
                //{
                //    Message = "Database Already Loaded";
                //}

                //Add to collections
                LoadPeopleList();
                LoadToBookInventory();
                LoadCheckOutLogList();

                //Set isLibrarian
                IsLibrarian = false;
            }
            catch (Exception ex)
            {
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                //Message = ex.Message;
                DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                OnDatabaseErrorRaised(e);
            }
        }
        /// <summary>
        /// Removes Book object in the argument from database context and local memory collection.
        /// Raises <event>OnDatabaseError</event> when exception is caught from attempting to change the database context.
        /// </summary>
        /// <param name="b">Book object to be removed</param>
        /// <returns>returns true if operation is successful, false otherwise.</returns>
        public bool RemoveBook(Book b)
        {
            if (!IsLibrarian)
            {
                return(false);   //return false if IsLibrarian is false.
            }
            if (b.IsCheckedOut)  //check if books are checked out or not
            {
                OnDatabaseErrorRaised(new DatabaseEventArgs("Cannot remove book, all copies must be checked in"));
                return(false);
            }
            else  //books are all checked in
            {
                try
                {
                    //remove from database
                    context.Books.Remove(b);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    while (ex != null)
                    {
                        ex = ex.InnerException;
                    }
                    DatabaseEventArgs e = new DatabaseEventArgs(ex.Message);
                    OnDatabaseErrorRaised(e);
                    return(false);
                }

                //remove from memory
                bookInventory.Remove(b);

                return(true);
            }
        }
 /// <summary>
 /// Event handler for OnDatabaseError event
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OnDatabaseErrorHandler(object sender, DatabaseEventArgs e)
 {
     MessageBox.Show(e.Error, "Database Error", MessageBoxButton.OK, MessageBoxImage.Error);
 }
Beispiel #10
0
 /// <summary>
 /// Event handler for OnDatabaseError events
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OnDatabaseErrorHandler(object sender, DatabaseEventArgs e)
 {
     MessageBox.Show(e.Error);
 }