Ejemplo n.º 1
0
 public static void RemoveBookCopies(Book book, int number)
 {
     if (number < 0)
     {
         MessageBox.Show("Please enter a positive integer.");
         return;
     }
     if (book.NumberOfCopies < number)
     {
         MessageBox.Show("Number of copies cannot be less than zero.");
         return;
     }
     if (API.GetNumberAvailableBookCopies(book) < number)
     {
         MessageBox.Show("Cannot remove books that are currently checked out.");
         return;
     }
     using (var db = new LibraryInfoEntities())
     {
         var bookContext = db.Books.FirstOrDefault(b => b.ISBN == book.ISBN);
         bookContext.NumberOfCopies -= number;
         db.SaveChanges();
         MessageBox.Show($"Removed {number} copies from '{book.Title}'.");
     }
 }
Ejemplo n.º 2
0
        public static Author CreateAuthor(string firstname, string lastname, string bio)
        {
            using (var db = new LibraryInfoEntities())
            {
                var foundAuthor = db.Authors.FirstOrDefault(a => a.Person.Firstname == firstname &&
                                                            a.Person.Lastname == lastname);
                var foundPerson = db.People.FirstOrDefault(p => p.Firstname == firstname &&
                                                           p.Lastname == lastname);

                if (foundAuthor == null)
                {
                    Person person;
                    if (foundPerson == null)
                    {
                        person = new Person()
                        {
                            Firstname = firstname,
                            Lastname  = lastname
                        };
                        db.People.Add(person);
                        db.SaveChanges();
                    }
                    else
                    {
                        person = foundPerson;
                    }

                    Author author = new Author
                    {
                        ID  = person.PersonID,
                        Bio = bio
                    };
                    db.Authors.Add(author);
                    db.SaveChanges();

                    return(author);
                }

                else
                {
                    MessageBox.Show("An author with that name already exists.");
                    return(null);
                }
            }
        }
Ejemplo n.º 3
0
 public static void AddBookCopies(Book book, int number)
 {
     if (number < 0)
     {
         MessageBox.Show("Please enter a positive integer.");
         return;
     }
     using (var db = new LibraryInfoEntities())
     {
         var bookContext = db.Books.FirstOrDefault(b => b.ISBN == book.ISBN);
         bookContext.NumberOfCopies += number;
         db.SaveChanges();
         MessageBox.Show($"Added {number} copies to '{book.Title}'.");
     }
 }
Ejemplo n.º 4
0
 public static void CheckOutBook(Book book, Cardholder cardholder)
 {
     using (var db = new LibraryInfoEntities())
     {
         CheckOutLog checkOutLog = new CheckOutLog
         {
             BookID       = book.BookID,
             CardholderID = cardholder.ID,
             CheckOutDate = DateTime.Now
         };
         db.CheckOutLogs.Add(checkOutLog);
         db.SaveChanges();
         MessageBox.Show($"{cardholder.LibraryCardID} has checked out '{book.Title}'");
     }
 }
Ejemplo n.º 5
0
 public static void CheckInBook(Book book, Cardholder cardholder)
 {
     using (var db = new LibraryInfoEntities())
     {
         CheckOutLog checkOutLog = db.CheckOutLogs.FirstOrDefault(c => c.BookID == book.BookID && c.CardholderID == cardholder.ID);
         if (checkOutLog != null)
         {
             db.CheckOutLogs.Remove(checkOutLog);
             db.SaveChanges();
             MessageBox.Show($"{cardholder.LibraryCardID} has checked in '{book.Title}'");
         }
         else
         {
             MessageBox.Show($"{cardholder.LibraryCardID} and '{book.Title}' do not exist together in the checkout log.");
         }
     }
 }
Ejemplo n.º 6
0
        public static Book UpdateBook(Book book, string isbn, string title, Author author, int numPages, string subject, string description,
                                      string publisher, string yearPublished, string language, int numCopies)
        {
            if (author == null)
            {
                throw new ArgumentNullException(nameof(author));
            }

            using (var db = new LibraryInfoEntities())
            {
                if (book.ISBN == isbn || !IsBookInDB(isbn))
                {
                    int numCopiesCheckedOut = book.NumberOfCopies - GetNumberAvailableBookCopies(book);
                    if (numCopiesCheckedOut <= numCopies)
                    {
                        book                = db.Books.FirstOrDefault(b => b.BookID == book.BookID);
                        book.AuthorID       = author.ID;
                        book.Description    = description;
                        book.ISBN           = isbn;
                        book.Language       = language;
                        book.NumberOfCopies = numCopies;
                        book.NumberPages    = numPages;
                        book.Publisher      = publisher;
                        book.Subject        = subject;
                        book.Title          = title;
                        book.YearPublished  = yearPublished;

                        db.SaveChanges();
                        return(book);
                    }
                    else
                    {
                        MessageBox.Show($"There are currently {numCopiesCheckedOut} copies checked out. " +
                                        $"New number of total copies cannot be less than this.");
                        return(null);
                    }
                }
                else
                {
                    MessageBox.Show("A book with that ISBN already exists");
                    return(null);
                }
            }
        }
Ejemplo n.º 7
0
        public static Book CreateBook(string isbn, string title, Author author, int numPages, string subject, string description,
                                      string publisher, string yearPublished, string language, int numCopies)
        {
            if (author == null)
            {
                throw new ArgumentNullException(nameof(author));
            }

            using (var db = new LibraryInfoEntities())
            {
                if (!IsBookInDB(isbn))
                {
                    Book b = new Book()
                    {
                        AuthorID       = author.ID,
                        Description    = description,
                        ISBN           = isbn,
                        Language       = language,
                        NumberOfCopies = numCopies,
                        NumberPages    = numPages,
                        Publisher      = publisher,
                        Subject        = subject,
                        Title          = title,
                        YearPublished  = yearPublished
                    };

                    db.Books.Add(b);
                    db.SaveChanges();
                    return(b);
                }
                else
                {
                    MessageBox.Show("A book with that ISBN already exists");
                    return(null);
                }
            }
        }